-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathClosest Prime.c
46 lines (43 loc) · 864 Bytes
/
Closest Prime.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
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,i,j=1,s=0,x,y,q=0,w=0;
printf("Enter The Number...:");
scanf("%d",&a);
for(i=1;i<=a;i++)
{
if(a%i==0)
s++;
}
if(s==2)
printf("Closest prime to %d is %d",a,a);
else
{
while(w<2&&q<2)
{
w=0;
q=0;
x=a+j;
y=a-j;
j++;
for(i=1;i<=x;i++)
{
if(x%i==0)
q++;
}
for(i=1;i<=y;i++)
{
if(y%i==0)
w++;
}
}
if(w==2&&q==2)
printf("%d and %d is closest prime to %d",y,x,a);
else if(q==2)
printf("%d is the closest prime to %d",x,a);
else if(w==2)
printf("%d is the closest prime to %d",y,a);
}
return 0;
}