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

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

// 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

≪ Previous | Next ≫