C++ Program to Find Prime Factors of a Number

In this post, we will learn how to find the prime factors of a number using C++ Programming language.

Suppose the user enters a number 18, then it’s prime factors will be 2 and 3. We will find the prime factors using the following methods:

  1. Using While Loop
  2. Using for Loop

So, without further ado, let’s begin this tutorial.

C++ Program to Find Prime Factors of a Number

// C++ Program to Find Prime Factors of a Number
#include <iostream>
using namespace std;

int main(){
    int num, i = 1, j, count;
    
    // Asking for input
    cout << "Enter any number to find prime factors: ";
    cin >> num;
    
    while (i <= num){
        count = 0;
        if (num % i == 0){
            j = 1;
            while (j <= i){
                if (i % j == 0){
                    count++;
                }
                j++;
            }
            if (count == 2){
                cout << i << " is a prime factor of " << num << endl;
            }
        }
        i++;
    }
    return 0;
}

Output

Enter any number to find prime factors: 15
3 is a prime factor of 15
5 is a prime factor of 15

How Does This Program Work ?

    int num, i = 1, j, count;

In this program, we have declared four int data type variables named num, i, j and count.

    // Asking for input
    cout << "Enter any number to find prime factors: ";
    cin >> num;

Then, the user is asked to enter any number to find its prime factors.

    while (i <= num){
        count = 0;
        if (num % i == 0){
            j = 1;
            while (j <= i){
                if (i % j == 0){
                    count++;
                }
                j++;
            }

We have used a while loop to find the factors of the entered number.

            if (count == 2){
                cout << i << " is a prime factor of " << num << endl;
            }

We display the prime factors of the entered number with the help of the cout statement.

C++ Program to Find Prime Factors of a Number Using For Loop

// C++ Program to Find Prime Factors of a Number
#include <iostream>
using namespace std;

int main(){
    int num, i, j, count;
    
    // Asking for input
    cout << "Enter any number: ";
    cin >> num;
    
    for (i = 1; i <= num; i++){
        count = 0;
        if (num % i == 0){
            for (j = 1; j <= i; j++){
                if (i % j == 0){
                    count++;
                }
            }
        }
            if (count == 2){
                cout << i << " is a prime factor of " << num << endl;
            }
        }
    return 0;
}

Output

Enter any number: 42
2 is a prime factor of 42
3 is a prime factor of 42
7 is a prime factor of 42

Conclusion

I hope after going through this post, you understand how to find the prime factors of a number using C++ Programming language.

If you have any doubts regarding the program, feel free to contact us in the comment section. We will be delighted to assist you.

Also Read:

2 thoughts on “C++ Program to Find Prime Factors of a Number”

  1. Gurpreet Singh

    #include
    #include
    void main()
    {
    int num,n,a=2;
    cout<>num;
    while( a<=num)
    {
    if(num%a==0)
    {
    num=num/a;
    cout<<"\n "<<a;
    }
    else
    {
    a=a+1;
    }
    }

    }

    I think this works as well

  2. Gurpreet Singh

    #include
    #include
    void main()
    {
    int num,n,a=2;
    cout<>num;
    while( a<=num)
    {
    if(num%a==0)
    {
    num=num/a;
    cout<<"\n "<<a;
    }
    else
    {
    a=a+1;
    }
    }

    }

Leave a Comment

Your email address will not be published.