Question

Given the following program segment….             for (num = 10; num >= 1; num--)            cout...

Given the following program segment….

            for (num = 10; num >= 1; num--)

           cout << setw(3) << num;

Write a while loop that will have the same output.

C++

Homework Answers

Answer #1

Code:

int num = 10; //Initialization of loop variable
while(num >= 1) //Condition of loop
{
   cout << setw(3) << num; // loop statement
   num--; //Decrement or Increment
}

Explanation: In for loop there is Initialization, Condition and Increment or Decrement. Convert for loop into while loop we need to take care of Initialization, Condition and Increment or Decrement.

Screenshot:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a C++ program that processes orders for our company Widgets and More. Your program will...
Write a C++ program that processes orders for our company Widgets and More. Your program will read the product code and quantity for all the products in an order from a file, order.txt. You will be given a sample order.txt with the starter code. There are two items on each line of the file the first is a letter that represents the product and the second is an integer representing the number bought. Based on the product and quantity, you...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath>...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath> using namespace std; void divisors(int num); int main () {    char repeat;    int num;       while (repeat !='n')    {        cout << "Enter a number: ";        cin >> num;        divisors(num);        cout << "Continue? (y or n): ";        cin >> repeat;    }    return 0; } void divisors(int num) {   ...
When the user enters 2 the following program segment should print the message “You’ve selected to...
When the user enters 2 the following program segment should print the message “You’ve selected to get an A”, however it always prints “You’ve selected to get a C”. Rewrite the code segment to behave properly, i.e., print the proper message: int selection; cout << “Please enter the grade you would like to receive “ << endl; cout << “1 = C, 2 = A”; cin >> selection; if (selection = 1) cout << “You’ve selected to get a C“...
(C++ program) Use the code segment below to answer the two questions that follow. … int...
(C++ program) Use the code segment below to answer the two questions that follow. … int size = 0; cout << “Enter size: “; cin >> size; int sizeCode = size / 10; if (sizeCode == 0)    cout << “extra small”; else if(sizeCode == 1 )    cout << “small”; else if (sizeCode == 2)    cout << “medium”; else if (sizeCode == 3)    cout << “large”; else    cout << “extra large”; //end if … What would...
debug the following 1) // The main program calls a method that // prompts for and...
debug the following 1) // The main program calls a method that // prompts for and returns a name start Declarations string usersName name = askUserForName() output "Your name is ", usersName stop string askUserForName() Declarations string name output "Please type your name " input usersName return 2) // The main program passes a user’s entry to // a method that displays a multiplication table // using the entered value. The table includes the // value multiplied by every factor...
C# code required Write two code segments that print the integers 1 through 10. One segment...
C# code required Write two code segments that print the integers 1 through 10. One segment should use a while loop and the other should use a for loop.
Using a while loop, write a program that reads 10 integer numbers. The program shall count...
Using a while loop, write a program that reads 10 integer numbers. The program shall count how many of them are multiples of 2, how many are multiples of 3, and how many are NOT multiples of either 2 or 3. The output should be similar to the one shown below. c++ language
Use a loop invariant to prove that the following program segment for computing the nth power,...
Use a loop invariant to prove that the following program segment for computing the nth power, where n is a positive integer, of a real number x is correct. power := 1 i := 1 while i <= n power := power*x i := i+1
Do While loop on C++ I need to program a do while loop in which the...
Do While loop on C++ I need to program a do while loop in which the user have to choose between option A,a, B,b or C,c. I need a do while loop that if other letter (different from options is choose) the loop print a cout saying "Invalid selection. Plese, enter the selection again.   I have one, but isn't working well int main() { char option; do { cout<<"Please choose one of the following options: "<<endl<<endl; cout<<"Option A - Julian...
Write the output of the following cout statements, showing the steps of your work. auto value_1...
Write the output of the following cout statements, showing the steps of your work. auto value_1 = -1, value_2 = -4, value_3 = 9; if (value_1 == value_2 || value_3 / 3 > value_2) { if (value_1 - 2 * value_2 == 1) cout << "A"; else if (value_3 * -1 > 0) cout << "B"; } else { cout << "C"; }
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT