Input number and print factors using for loop in C++
for Loop - Question 9
In this question, we will see how to input a number and print its factors in C++ programming using for loop. To know more about for loop click on the for loop lesson.
Q9) Write a program in C++ to input a number and print its factors using for loop.
Program
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int i,n;
cout<<"Enter a number ";
cin>>n;
cout<<"Factors are\n";
for(i=1; i<=n; i=i+1)
{
if(n%i==0)
{
cout<<i<<endl;
}
}
return 0;
}
Output
Enter a number 12 Factors are 1 2 3 4 6 12