# C++ Program to Find Factors/Divisors of A Given Number

Finding divisors of a given number in C++ is a frequently asked interview question.

In today’s post, I will try to reveal that how can we find factors of a given number.

## Program Logic:

Before starting, we should know that what is a divisor? A divisor is a number that divides another number completely. For example D is the divisor of N if N%D=0.

For example, 5 is a divisor of 20 because when we divide 20 by 5 then modulus is zero i.e. 20%5=0.

To find the divisors of a number, we have to divide this number with all numbers less than or equal to desired number. If the mod of a given division is equal to zero then it is considered that denumerator is a factor of given number.

In C++, % operator is used to find the modulus of a division.

## C++ Code For Finding Factors of A Number:

``````#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"ENTER THE NUMBER: ";
cin>>num;
cout<<"THE FACTORS OF "<<num<<" ARE:"<<endl;
for(int i=1;i<=num;++i)
{
if(num%i==0)
cout<<i<<"	";
}
return 0;
}
``````

## Code Explanation:

At the start of this code, we declared one int type variable named num. The number whose factors are required is stored in this variable.

We started FOR loop with control variable named i. We initialized this loop with value 1 and continued it until the i becomes equal to num. What this condition does is that it makes sure all the numbers would be divided with given number.