Answer to Question #225177 in C++ for Ulando

Question #225177

Write a program that read an integer and display all its smallest factors. example if the input integer 120,then the output should be as follows: 2,2,2,3,5.


1
Expert's answer
2021-08-11T03:20:22-0400
#include <bits/stdc++.h>
using namespace std;
 

void primeFactors(int n)
{

    while (n % 2 == 0)
    {
        cout << 2 << " ";
        n = n/2;
    }
 

    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
 
        while (n % i == 0)
        {
            cout << i << " ";
            n = n/i;
        }
    }
 
  
    if (n > 2)
        cout << n << " ";
}
 

int main()
{
    int n;
    cin >> n;
    primeFactors(n);
    return 0;
}

Input 1:
120
Output 1:
2 2 2 3 5

Input 2:
315
Output 2:
3 3 5 7

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS