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]
2 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]2 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
1. Would it be possible for two people to have the same email address? Explain.
Goshia [24]

Answer: No

Explanation:

For the same reason that two people cannot have the same address, it's just a virtual address.

6 0
2 years ago
Read 2 more answers
How many mb are in a gb?
frosja888 [35]

Answer:

1000 megabytes are in a gigabyte

Explanation:

3 0
2 years ago
A common and extremely useful feature of most online dictionaries is
choli [55]
A common and extremely useful feature of most online dictionaries is <em />interoperable browser.
4 0
3 years ago
Blender questions
Tamiku [17]
<span>14. A mesh represents a(n) _____ object if its faces enclose a positive and finite amount of space. (1 point)

odd

connected

simple

convex



15. Which of the following is the 3-D view port? (1 point)

the standard layout used for new files

the polygon viewing on the default screen

straight line segments connecting two vertices

a single static image in 3-D

The answer for number 1, should be:
SOLID
</span><span>A mesh represents a solid object if its faces enclose a positive and finite amount of space
</span>
The answer for the second question is:
a single static image in 3-D
4 0
2 years ago
Read 2 more answers
A computer's ____ is a collection of programs that manage and coordinate the activities taking place within the computer. It pro
V125BC [204]

Answer: Operating system

Explanation:

  A computer's OS (operating system) is one of the type of computer software and the main function of an operating system is that it helps in managing the hardware, processes, memory and the software of the computer application.

 An operating system basically providing the set of instruction to the computer for the communication purpose and perform various types of task in the computer system.

According to the given question, an operating system is refers to the software program that helps in coordinating all the task and the activities in the computer system.        

 Therefore, Operating system is the correct answer.

3 0
2 years ago
Other questions:
  • What is the primary criticism against a national identification system based on biometric data?
    13·1 answer
  • If you don't know whether to write too, two, or to, you should use the spell-checker to help you decide. true or false?
    7·1 answer
  • A broadband router is used to do which of the following? run the programs on a computer locate lost files on a computer manage a
    6·1 answer
  • Implement a java program to find the smallest distance between two neighbouring numbers in an array.
    7·1 answer
  • Which is said to be ‘computer on a chip’
    8·1 answer
  • 1. The global economy involves trading between people from different _____
    8·1 answer
  • There are two main advantages to using multiple threads in a process: 1) Less work involved in creating a new thread rather than
    7·1 answer
  • Set methods are also commonly called _____ methods, and get methods are also commonly called _____ methods.
    6·1 answer
  • Besides earning money why do people work​
    6·2 answers
  • What are the advantages of saving files in a cloud? <br>Please help!! ​
    11·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!