C++ program to find the factors of a number
Program to find Factors of a number
Here we will discuss how to find the factors of a number using C++ programming language.
Factor is a number that when multiplied with another number produces a given number.
To find the Factor of a number we will use a loop in which we start dividing the number with 1 up to the number itself and the numbers which perfectly divides the number are the factors.
For Example: 21
Factors are: 1, 3, 7, 21
Let’s see how the code works:
- User gives an input.
- Input is stored in an int type variable say num.
- A loop is started
- num is divided by other numbers starting from 1 upto num itself
- if the number properly divides num then the number is a factor of num
- otherwise the number is not a factor of num.
- the loop will run until all the factors are not found.
//Factors of a number
using namespace std;
cout << "Enter a positive number: ";
cin >> num;
cout << "Factors of " << num << " are: " << endl;
//finding and printing factors
for(int i = 1; i <= num; i++)
if(num % i == 0)
cout << i << "\t";
Enter a positive number: 36
Factors of 36 are:
1 2 3 4 6 9 12 18 36