Using C++, write a program to answer the following. Write code that lets the user enter a
Question:
Using C++, write a program to answer the following.
- Write code that lets the user enter a number. The number should be multiplied by 4 and printed until the number exceeds 60. Use a while loop.
2. Write a do-while loop that asks the user to enter three numbers. The numbers should be added and the sum displayed. The user should be asked if he or she wishes to perform the operation again. If so, the loop should repeat; otherwise, it should terminate.
3. Write a for loop that displays the following set of numbers:
0, 10, 20, 30, 40, 50 . . . 1000
4. Write a loop that asks the user to enter a number. The loop should iterate 10 times and keep a running total of the numbers entered
5. 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.";
while ((doAgain == 'y') || (doAgain == 'Y'))
{
sum++;
cout << "Sum has been incremented. Increment it again(y/n)? ";
cin >> doAgain;
}
cout << "Sum was incremented " << sum << " times.";
6. Rewrite the following code, replacing the do-while loop with a while loop. When you do this you will no longer need an if statement:
int number;
cout << "Enter an even number: ";
do
{ cin >> number;
if (number % 2 != 0)
cout << "Number must be even. Reenter number: ";
} while (number % 2 != 0);
7. Convert the following while loop to a for loop:
int count = 0;
while (count < 50)
{
cout << "count is " << count << endl;
count++;
}
8. Convert the following for loop to a while loop:
for (int x = 50; x > 0; x--)
{
cout << x << " seconds to go.";
}
9. What will the following program segment display?
int x = 1;
while (x < 10);
x++;
cout << x;
32. int x = 1;
while (x < 10)
x++;
cout << x;
10. What will the following program segment display?
for (int count = 1; count <= 10; count++)
{
cout << ++count << " "; // This is a bad thing to do!
}
34. for (int row = 1; row <= 3; row++)
{
cout << "$";
for (int digit = 1; digit <= 4; digit++)
cout << '9';
}
11. Write a do-while Loop that prints the odd integers from 1 – 30. Display the value of Output: 1 3 5 7 9 etc.
12. Write code, using a do-while loop, that takes two integers input by the user, multiplies them, and prints the answer. The program will ask the user if they want to enter two new integers to multiply until the user replies N or n.
Quantitative Analysis for Management
ISBN: 978-0132149112
11th Edition
Authors: Barry render, Ralph m. stair, Michael e. Hanna