Thursday 8 February 2018

Write a program that asks the user for an integer and then prints out all its factors in increasing order. Example input us 150, it should print 2 3 5 5

Leave a Comment
Question:

                Write a program that asks the user for an integer and then prints out all its factors in increasing                  order. Example input us 150, it should print 
                2
                3
                5
                5

Explanation:
Below mention code is compiled in Visual Studio 2015 and Code Blocks 13.12,output snap is attached.. If any problem you feel and you want some explanation feel free to contact us.

Code:

/**************************************************|
/*************C++ Programs And Projects************|
***************************************************/
#include<iostream>
using namespace std;
void main() {
       int number=0;
       cin >> number;
       while (number > 1) {
              if (number % 2 == 0) {
                     number = number / 2;
                     cout << 2<<endl;
              }
              else if (number % 3 == 0) {
                     number = number / 3;
                     cout << 3 << endl;
              }

              else if (number % 4 == 0) {
                     number = number / 4;
                     cout << 4 << endl;
              }

              else if (number % 5 == 0) {
                     number = number / 5;
                     cout << 5 << endl;
              }
              else if (number % 6 == 0) {
                     number = number / 6;
                     cout << 6 << endl;
              }

              else if (number % 7 == 0) {
                     number = number / 7;
                     cout << 7 << endl;
              }
              else if (number % 8 == 0) {
                     number = number / 8;
                     cout << 8 << endl;
              }

              else if (number % 9 == 0) {
                     number = number / 9;
                     cout << 9 << endl;
              }
              else {
                     cout << number << endl;
                     number = number / number;
              }
       }

}


Output:
Write a program that asks the user for an integer and then prints out all its factors in increasing order. Example input us 150, it should print                   2                  3                  5                  5

Write a program that asks the user for an integer and then prints out all its factors in increasing order. Example input us 150, it should print                   2                  3                  5                  5




Related Articles:

C++ Primer Plus Sixth Edition Complete Solution Manual 

C++ Books Solution

If You Enjoyed This, Take 5 Seconds To Share It

0 Questions: