forked from VivekDubey9/Competitive-Programming-Algos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrime_factors.cpp
More file actions
46 lines (39 loc) · 782 Bytes
/
Prime_factors.cpp
File metadata and controls
46 lines (39 loc) · 782 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// C++ program to print all prime factors
#include <bits/stdc++.h>
using namespace std;
# include<iostream>
// A function to print all prime
// factors of a given number n
void primeFactors(int n)
{
// Print the number of 2s that divide n
while (n % 2 == 0)
{
cout << 2 << " ";
n = n/2;
}
// n must be odd at this point. So we can skip
// one element (Note i = i +2)
for (int i = 3; i <= sqrt(n); i = i + 2)
{
// While i divides n, print i and divide n
while (n % i == 0)
{
cout << i << " ";
n = n/i;
}
}
// This condition is to handle the case when n
// is a prime number greater than 2
if (n > 2)
cout << n << " ";
}
/* Driver code */
int main()
{
int n;
cout<<"Enter the value of number:\n";
cin>>n;
primeFactors(n);
return 0;
}