168k views
2 votes
Assume the int variable value has been initialized to a positive integer. Write a while loop that prints all of the positive divisors of value. For example,if values is 28, it prints divisors of28: 1 2 4 7 14 28

User Shinjuo
by
7.0k points

1 Answer

4 votes

Answer:

The program to this question can be given as:

Program:

#include <iostream>//header file.

using namespace std; //using namespace

int main() //main function.

{

int value=28, x=1; //define variable.

cout<<"divisors of "<< value <<":"; //print value.

while (x <= value) //loop

{

if ((value % x) ==0) //if block

cout<<" "<<x; //print value

x++; //increment value.

}

return 0;

}

Output:

divisors of 28: 1 2 4 7 14 28

Explanation:

The description of the above c++ program can be given as:

  • In this program firstly we include a header file. Then we define a main method. In this method we define a variables that is "value and x" and assign a value that is "28 and 1".
  • The variable "x" is used to calculate the value of divisors and print its values and then we print the value variable value.
  • Then we define a while loop. It is an entry control loop in this loop, we check the condition that the variable x is less than equal to value. In this loop, we use a conditional statement.
  • In the if block we define condition that is variable value modular variable x is equal to 0. if this condition is true it will print the value and increment value of variable x. When variable x is greater than and equal to value variable it will terminate the loop and print the value.

So, the output of this program is "1 2 4 7 14 28".

User Arnold Daniels
by
7.2k points