This program takes a positive integer from an user and displays all the factors of that number.
Source Code to Display Factors of a Number
/* C++ to find and display all the factors of a number entered by an user.. */
using namespace std;
cout << "Enter a positive integer: ";
cin >> n;
cout << "Factors of " << n << " are: " << endl;
cout << i << endl;
Enter a positive integer: 60
Factors of 60 are: 1 2 3 4 5 6 12 15 20 30 60
In this program, an integer entered by user is stored in variable n. Then, for loop is executed with initial condition
i=1 and checked whether n is perfectly divisible by i or not. If n is perfectly divisible by i then, i will be the factor of n. In each iteration, the value of i is updated(increased by 1). This process goes until test condition
i<=n becomes false,i.e., this program checks whether number entered by user n is perfectly divisible by all numbers from 1 to n and all displays factors of that number.