Write one c program Write a program that will take an intege

**********Write one c++ program************

Write a program that will take an integer greater than or equal to 2 and finds all of its prime factors.

(The prime factors of a positive integer are the prime numbers that divide that integer exactly )

Sample input:

12

Sample output:

2 2 3

Solution

Solution.cpp

// Program to print all prime factors
# include <iostream>
# include <cmath>
using namespace std;
// A function to print all prime factors of a given number n
void primeFactorsofNumber(int num)
{
    // Print the number of 2s that divide n
    while (num%2 == 0)
    {
       cout<<\"2 \";
        num = num/2;
    }

    // n must be odd at this point. So we can skip one element (Note i = i +2)
    for (int i = 3; i <= sqrt(num); i = i+2)
    {
        // While i divides n, print i and divide n
        while (num%i == 0)
        {
         cout<< i<<endl;
            num = num/i;
        }
    }

    // This condition is to handle the case whien n is a prime number
    // greater than 2
    if (num > 2)
      cout<<num<<\" \";
}

/* Driver program to test above function */
int main()
{
    int number;
    cout<<\"enter the number greater than or equal to 2   :\";
    cin>>number;
    if(number<2)
    exit(0);
    primeFactorsofNumber(number);
    return 0;
}

output

enter the number greater than or equal to 2 :12

2 2 3

**********Write one c++ program************ Write a program that will take an integer greater than or equal to 2 and finds all of its prime factors. (The prime
**********Write one c++ program************ Write a program that will take an integer greater than or equal to 2 and finds all of its prime factors. (The prime

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site