C++ Program to Display Factors of a Number.

C++ Program to Display Factors of a Number.

#include <iostream.h>
int main()
{
    int n, i;

    cout << "Enter a positive integer: ";
    cin >> n;

    cout << "Factors of " << n << " are: " << endl;  
    for(i = 1; i <= n; ++i)
    {
        if(n % i == 0)
            cout << i << endl;
    }

    return 0;
}


Output


Enter a positive integer: 60
Factors of 60 are: 1 2 3 4 5 6 12 15 20 30 60

Post a Comment

Previous Post Next Post