-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPair of Prime Numbers
39 lines (36 loc) · 1.04 KB
/
Pair of Prime Numbers
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
For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2.
Write a C program that finds the number of all pairs of prime numbers satisfying the condition n = p1 + p2 for a given even number(n).
Even number which is greater than or equal to 4 and less than 215 is given as input and number of pairs of prime number is the output.
[Note: Do not consider (p1, p2) and (p2, p1) separately as two different pairs.]
#include<stdio.h>
#include<math.h>
int isprime(int n){
int i;
if(n==1 ){
return 0;
}
if(n==2) return 1;
if(n%2==0) return 0;
for(i=2;i<=n/2;i++){
if(n%i==0) return 0;
}
return 1;
}
int main()
{
int i,n,cnt=0;
printf("Enter the value\n");
scanf("%d",&n);
if(n%2!=0 || n<4 || n > pow(2,15)) printf("Invalid Input");
else{
for(i=n-1;i>=n/2;i--){
if(isprime(i)){
if(isprime(n-i)){
cnt++;
}
}
}
printf("Number of pairs for %d is %d",n,cnt);
}
return 0;
}