Concept explainers
Rewrite the following code, converting the while loop to a do-while loop:
char doAgain = 'y';
int sum = 0;
cout << "This code will increment sum 1 or more times.\n";
while ((doAgain == 'y') || (doAgain == 'Y'))
{ sum++;
cout << "Sum has been incremented. Increment it again(y/n)? ";
cin >> doAgain;
}
cout << "Sum was incremented " << sum << " times.\n";
Want to see the full answer?
Check out a sample textbook solutionChapter 5 Solutions
STARTING OUT WITH C++ MPL
Additional Engineering Textbook Solutions
Starting Out With Visual Basic (7th Edition)
Starting Out with C++ from Control Structures to Objects (9th Edition)
Starting Out with Java: From Control Structures through Objects (6th Edition)
Objects First with Java: A Practical Introduction Using BlueJ (6th Edition)
Starting Out with C++ from Control Structures to Objects (8th Edition)
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
- CENGAGE MINDTAP 4-Accumulating Totals in a Loop in Java Accumulating Totals in a Loop f Ⓡ Summary In this lab, you add a loop and the statements that make up the loop body to a Java program that is provided. When completed, the program should calculate two totals: the number of left-handed people and the number of right-handed people in your class. Your loop should execute until the user enters the character X instead of L for left-handed or R for right-handed. The inputs for this program are as follows: R, R, R, L.L.L. R. L, R, R, L, X Variables have been declared for you, and the input and output statements have been written. Instructions 1. Ensure the file named LeftOrRight.java is open. 2. Write a loop and a loop body that allows you to calculate a total of left-handed and right-handed people in your class. 3. Execute the program by clicking Run and using the data listed above and verify that the output is correct. Grading When you have completed your program, click the Submit…arrow_forwardConvert the following for loop to a while loop: for (int x = 50; x > 0; x--){ cout << x << " seconds to go.\n"; }arrow_forwardLooping Construct with Floating Point Numbers Write a program that utilizes a while-loop to read a set of five floating-point values from user input. Include code to prevent an endless loop. Ask the user to enter the values, then print the following data: Total Average Maximum Minimum Interest on total at 20% Answer:arrow_forward
- Complete the following nested for loop. The loop should display the result of i times j. i should be incremented until it reaches 10, and į should be decremented. for (int i = 0; { for (int j= 10; j> 0; { %3D } }arrow_forwardRewrite the following code, converting the while loop to a do-while loop: char doAgain = 'y';int sum = 0;cout << "This code will increment sum 1 or more times.\n"; while ((doAgain == 'y') || (doAgain == 'Y')){ sum++;cout << "Sum has been incremented. Increment it again(y/n)? "; cin >> doAgain;}cout << "Sum was incremented " << sum << " times.\n";arrow_forwardJava program | nested looparrow_forward
- Class Exercise Write a program that asks the user to enter how many students they have. Use a for loop to iterate through each student. For each student, allow the user to enter as many scores as they want and let them know that entering -1 will indicate the end of entering scores (note: use a while loop with sentinel value). Sum all the scores for each student and display the total on the screen. For example: Student 1 Total score is 340 Student 2 Total score is 200arrow_forward1.Convert the while loop in the following program to for loop. #include int main() { Int num rem,reverse_num3%3; printf("nEnter any number:"); scanf("%d",&num); while(num>=1) { rem=num%10; reverse_num=reverse num*10+rem; num=num/10; printf(nReverse of input number is:%d",reverse_num); return 0:arrow_forwardQ3: Convert the following while loop into a do-while loop. int number; int sum = 0; count<< "Enter an integer, the input ends if it is 0"; cin >> number; cout<<endl; while (number != 0) { sum += number; cout<< "Enter an integer, the input ends if it is 0"<<endl; cin >> number; }arrow_forward
- No written by hand solution IN JAVA - NEEDS DRIVER AND CLASS PROGRAM Average Rainfall Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask for the number of years. The outer loop will iterate once for each year. The inner loop will iterate twelve times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month.arrow_forwardQ/Rewrite the following loops as for loops. a- int m = 10; do { cout 0) { if (n == 2) break; cout << n<< " } cout << "End of Loop.";arrow_forwardPressure Unit ConversionsWrite a program that converts a value in Torr into Kilopascal. Both of these are units used to measure pressure. Use the following formula to covert Torr to Kilopascal: 1 Torr = 132/1000 KiloPascalsYour Program must use a loop to display a table of the quantities in Torr from 1 to 10 and their KiloPascal equivalents.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning