As per the question, the task is to find the nearest prime number by adding the prime number starting from 2 if the number N is not Prime.
Input: N=6 Output: 11
Explanation
Since 6 is not prime add first prime to 6 i.e. 2 which will result to 8 now 8 is also not prime now add next prime after 2 which is 3 which will give 8+3 = 11. Hence 11 is a prime number output will be 11.
Algorithm
START Step 1- > declare num=15, i = num/2 Step 2 -> Loop For k=2 and k<=i and k++ Set I=k/2 Loop For j=2 and j<=l and j++ Set flag=0; If k%j=0 Set flag=1 Break End End IF flag=0 Set num=num+k; End IF Set a=num/2 Loop For m=2 and m<=a and m++ Set flag1=0; IF num%m=0 Set flag1=1 break End END If flag1=0 Print num End END STOP
Example
#include<stdio.h> int main(){ int num =15 ; int i,k,j,sum=0,flag=0,l,flag1=0,a,m; i=num/2; for(k=2;k<=i;k++) { l=k/2; for(j=2;j<=l;j++) { flag=0; if(k%j==0) { flag=1; break; } } if(flag==0) { num=num+k; } a=num/2; for(m=2;m<=a;m++) { flag1=0; if(num%m==0) { flag1=1; break; } } if(flag1==0){ printf("%d",num); return 0; } } }
Output
if we run the above program then it will generate the following output
17