C++ program to print factors of a number using loop.
C++ Program to find all positive factors of a number
#include <iostream>
using namespace std;
int main() {
int i, N;
cout << "Enter a Number\n";
cin >> N;
cout << "Factors of " << N << endl;
// Check for every number between 1 to N,
// whether it divides N
for(i = 1; i <= N; i++) {
if(N%i == 0) {
cout << i << " ";
}
}
return 0;
}
Output
Enter a Number 50 Factors of 50 1 2 5 10 25 50
Recommended Posts