While Loop Question
While Loop Question
1. Write a program that asks the user to enter a positive number. If the user enters a negative number
or zero, ask them again until they enter a valid positive number.
2. Write a program that asks the user for a username and password. Keep asking until the user enters
the correct credentials (e.g., username: "admin", password: "1234"). Display a success message after
a successful login.
3. Write a program that repeatedly asks a customer to enter the price of an item. Add the prices
together to get a total. Stop when the user enters 0 and display the total bill.
4. A coffee shop gives a free coffee after every 5 purchases. Write a program that tracks the number of
coffees bought by a customer and tells them how many more purchases are needed for a free
coffee. Exit when the customer gets a free coffee.
5. Design a program in C++ for tracking the number of steps taken by a person each day. Prompt the
user (e.g., an individual tracking their fitness) to enter the number of steps taken daily. The program
should calculate and display the total number of steps taken. Use a while loop to continue prompting
the user to enter the number of steps until they decide to stop.
6. A fitness tracker company wants to implement a rewards program that rewards users with a badge
after every 10,000 steps. The program should keep track of the number of steps taken by the user
and notify them when they have earned a badge. Write a C++ program that continuously prompts
the user to enter the number of steps they took, keeps a running total of the number of steps, and
notifies the user every time they have earned a badge. Allow the user to exit the program by
entering a negative number.
7. Write a program that reverses the digits of a positive integer entered by the user using a while loop.
For example, if the user enters 1234, the program should output 4321. Ensure that the program
continues until the user enters a valid positive integer.
8. Design a program in C++ for tracking fuel consumption of vehicles. Prompt the user (e.g., a fleet
manager) to enter the amount of fuel consumed by each vehicle during their trip. The program
should calculate and display the total fuel consumption. Use a while loop to continue prompting the
user to enter fuel amounts until they decide to stop.
9. Design a program in C++ for tracking temperature readings throughout the day. Prompt the user
(e.g., a weather station operator) to enter the temperature reading at different times of the day. The
program should calculate and display the average temperature. Use a while loop to continue
prompting the user to enter temperature readings until they decide to stop.
10. An online education platform wants to implement a rewards program that rewards students with a
free course after completing every 5 courses. The program should keep track of the number of
courses completed by the student and notify them when they have earned a free course. Write a C+
+ program that continuously prompts the user to enter the number of courses they completed,
keeps a running total of the number of completions, and notifies the user every time they have
earned a free course. Allow the user to exit the program by entering a negative number.