-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprime_factor.c
54 lines (47 loc) · 847 Bytes
/
prime_factor.c
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
46
47
48
49
50
51
52
53
54
/*
* Prime Factorization- Have the user enter a number and find all
* prime factors (if there are any) and display them.
*/
#include <stdio.h>
#include <math.h>
#define bool int
#define true 1
#define false 0
bool isPrime(int);
int main(void)
{
int i, n;
printf("Enter the value of n : ");
scanf("%d", &n);
printf("Prime factors of %d are ", n);
for (i = 2; i <= n; i++)
{
if(n % i == 0)
{
if(isPrime(i))
{
printf("%d ", i);
}
}
}
printf("\n");
}
bool isPrime(int n)
{
if (n == 2)
{
return true;
}
if (n < 2 || n % 2 == 0)
{
return false;
}
for (int i = 2; i <= sqrt(n) ; i++)
{
if(n % i == 0)
{
return false;
}
}
return true;
}