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

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

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

#include <iostream>

using namespace std;

void primefactors(int num)
{
    int Count;

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

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 ≫