C++ Program to Find Prime Factors of Number using For Loop

Write C++ Program to Find Prime Factors of Number using For Loop

// CPP Program to Find Prime Factors of Number using For Loop

#include <iostream>

using namespace std;

void primefactors(int num)
{
    int Count;

    cout << "\nPrime Factors of " << num << " are :--> ";
    for(Count = 2; num > 1; Count++)
    {
        for(; num % Count == 0; )
        {
            cout << Count << " ";
            num = num / Count;
        }
    }
    printf("\n");
}

int main()
{
    int num;

    cout << "Enter a positive integer :--> ";
    cin >> num;

    primefactors(num);

    return 0;
}

Output:

Enter a positive integer :--> 98

Prime Factors of 98 are :--> 2 7 7

≪ Previous | Next ≫