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:
Related Articles:
0 Questions:
Post a Comment