Loading [MathJax]/jax/input/TeX/config.js

C++ Program to Print Factors of a Number using While Loop

C++ Program to Print Factors of a Number using While Loop

Write C++ Program to Print Factors of a Number using While Loop

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// CPP Program to Print Factors of a Number using While Loop
#include <iostream>
using namespace std;
int main()
{
int n, i;
cout << "Enter a positive number :--> ";
cin >> n;
cout << "Factors of " << n << " are :--> ";
i = 1;
while(i <= n)
{
if (n % i == 0)
{
cout << i << " ";
}
i++;
}
}
// CPP Program to Print Factors of a Number using While Loop #include <iostream> using namespace std; int main() { int n, i; cout << "Enter a positive number :--> "; cin >> n; cout << "Factors of " << n << " are :--> "; i = 1; while(i <= n) { if (n % i == 0) { cout << i << " "; } i++; } }
// CPP Program to Print Factors of a Number using While Loop

#include <iostream>

using namespace std;

int main()
{
    int n, i;

    cout << "Enter a positive number :--> ";
    cin >> n;

    cout << "Factors of " << n << " are :--> ";

    i = 1;
    while(i <= n)
    {
     	if (n % i == 0)
	 	{
            cout << i << " ";
        }
		i++;
	}

}

Output:

Enter a positive number :--> 80
Factors of 80 are :--> 1 2 4 5 8 10 16 20 40 80