131k views
3 votes
Write a while loop that prints userNum divided by 2 (integer division) until reaching 1. Follow each number by a space. Example output for userNum = 20:

20 10 5 2 1
existing code:

#include
using namespace std;

int main() {
int userNum = 0;

userNum = 20;

/*Your solution goes here*/

cout << endl;

return 0;
}

User Ruju
by
7.7k points

1 Answer

5 votes

Answer:

while(userNum>0)//while loop.

{

cout<<userNum<<" ";//printing the userNum.

userNum/=2;//dividing the userNum.

}

Explanation:

The above-written code should be inserted in the place of the comment your solution should be placed here. In my code I have used a while loop that will run till the userNum is greater than 0. In the loop, I am constantly printing the userNum and dividing it by 2 after printing. This loop will print the same output as written in the question.

User Churro
by
8.0k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.