-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path8.c
More file actions
35 lines (29 loc) · 745 Bytes
/
8.c
File metadata and controls
35 lines (29 loc) · 745 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
#include <stdio.h>
void primeFactors(int n) {
// Print the number of 2s that divide n
while (n % 2 == 0) {
printf("%d ", 2);
n = n / 2;
}
// n must be odd at this point, so we can skip even numbers
for (int i = 3; i * i <= n; i = i + 2) {
// While i divides n, print i and divide n
while (n % i == 0) {
printf("%d ", i);
n = n / i;
}
}
// This condition is to handle the case when n is a prime number
// greater than 2
if (n > 2)
printf("%d ", n);
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Prime factorization of %d is: ", n);
primeFactors(n);
printf("\n");
return 0;
}