1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Serggg [28]
3 years ago
12

Write a while loop that prints userNum divided by 4 (integer division) until reaching 2. Follow each number by a space. Example

output for userNum = 160:
40 10 2

Note: These activities may test code with different test values. This activity will perform four tests, with userNum = 160, then with userNum = 8, then with userNum = 0, then with userNum = -1. See "How to Use zyBooks".

Also note: If the submitted code has an infinite loop, the system will stop running the code after a few seconds, and report "Programend never reached." The system doesn't print the test case that caused the reported message.

#include
using namespace std;

int main() {
int userNum;

cin >> userNum;

/* Your solution goes here */

cout << endl;

return 0;
}
Computers and Technology
1 answer:
Slav-nsk [51]3 years ago
4 0

Answer / Explanation:

#include <iostream>  

using namespace std;

int main()  

{    

 int userNum = 0;  

  userNum = 20;    

  cout << userNum << " ";

  while (userNum > 1)    

  {

     userNum = userNum/2;

     cout << userNum << " ";    

  }    

  cout << endl;  

  return 0;  

}

However, we should note that the above codes divides properly but when it gets to 0, it will always give output as 0 instead of terminating the program.

Hence to make it terminate, we include:

while (userNum > 1)    

{

  cout << userNum << " ";    

  userNum = userNum/2;

}    

The above code alternatively should be replaced with int userNum = 0;  .

Also, for the sake of industry best standard and the general principle, we can say:

The general principle is:

while ( <conditional> )

{

  // Use the data

  // Change the data as the last operation in the loop.

}

A for loop provides natural placeholders for these.

for ( <initialize data>; <conditional>; <update data for next iteration> )

{

  // Use the data

}  

If you were to switch to using a for loop, which I recommend, your code would be:

for ( userNum = 20; userNum > 0; userNum /= 2 )

{

  cout << userNum << " ";

You might be interested in
Five aplications of ict​
Irina18 [472]

Answer:

MS Word, MS Excel, MS PowerPoint, Adobe Photoshop, Adobe Reader

Explanation:

8 0
3 years ago
What a true portrait reveal
Snowcat [4.5K]
Is simple likeness. Historically, in fact, artists used self-portraits as a kind of calling card, attesting to their ability to capture a likeness and giving a sense of their capabilities. And, yes, self-portraits are convenient exercises because the model is always available and works for free
5 0
3 years ago
Melissa and Sue want to distribute a document to be discussed at a meeting starting in about thirty minutes. People will be atte
Anettt [7]
The answer is (c) fax the document to each location


Since fax machines are all connected to the old telephone lines, when used, they place a phone call to the number you have specified. This machine at the destination number on the other end receives and the transmission of documents is done over a telephone call. 

Melissa and Sue will also be in a position to send this fax using a computer if the computer will be connected to the phone line and a presence of a dial-up fax modem.




3 0
3 years ago
SOMEONE PLEASE HELP ME OUT WITH THIS!!!!!!
mario62 [17]

Answer:

C.

Explanation:

8 0
3 years ago
Read 2 more answers
Which of the following can a cell contain?
Ksju [112]
The answer is choice D-All of these :)
8 0
3 years ago
Read 2 more answers
Other questions:
  • Julian is adding a column of numbers as he inputs expenses from his receipts. Holly wants to create a simple comparison between
    13·1 answer
  • A special symbol that is used as an underlining structure to an agenda
    5·1 answer
  • Which of the following is not a responsibility of a member of society?
    14·1 answer
  • What are the coordinates of the origin point? (Select the best answer.)
    10·1 answer
  • Which options can be adjusted in the print settings?
    8·2 answers
  • give the difference between functional and functional tools in the middle of to the circle give importance​
    10·1 answer
  • java Two smallest numbers Write a program that reads a list of integers, and outputs the two smallest integers in the list, in a
    6·1 answer
  • Select the correct answer.
    13·1 answer
  • Information that's been collected to help solve a problem is called
    10·1 answer
  • you're building a new network for a small financial services startup company. security is paramount, so each organization within
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!