We assume you have a basic knowledge of c++ programming language, c++ loop, and C++ if, if…else and Nested if…else.
In the below program we execute the for loop and if statement to get the factors of a number entered by the user during the program execution.
Copy the below code execute it with the CPP compiler.
using namespace std;
int n, i;
cout << "Enter a positive integer: ";
cin >> n;
cout << "Factors of " << n << " are: " << endl;
for(i = 1; i <= n; ++i)
if(n % i == 0)
cout << i << endl;
Enter a positive integer: 50
1, 2, 5, 10, 25, 50
Prem Tiwari is the founder of FreeWebMentor.com and also a professional developer who has vast experience in PHP and open source technologies. Apart from this, he is a blogger by hobby and also he has been a regular speaker of WordPress sessions in various IT Companies. View all posts by Prem Tiwari