C++ code to print factors of a given number

This program will print all the factors of a given number. For example, if you have entered 12 then it will give all tha factors of 12 that is 1,2,3,4,6,12.

#include
#include
void main()
{
int i,n;
clrscr();
cout<<"Enter a number:";
cin>>n;
cout<<"nThe factors of given number are:n";
for(i=1;i<=n;i++)
{
if(n%i==0)
{
cout<}
}
getch();
}

Output:

Enter a number:6
The factors of given number are:
1 2 3 6

Add Comment