Q. Write a function(efficient) to find the next prime number after a given number ?

Questions by rameshwar83   answers by rameshwar83

Showing Answers 1 - 12 of 12 Answers

baseersd

  • Jul 24th, 2007
 

#include<stdio.h>
main()
{
int i,j,count,num;

printf("nEnter the number");
scanf("%d",&num);
for(i=num+1;1;i++)
{
 for(j=2,count=0;j<=i;j++)
 {
               if(i%j == 0)
               {
                count++;
               }     
 }
 if(count==1)
 {
  printf("%d",i);
  break;           
 }
}

getch();
}

  Was this answer useful?  Yes

#include<stdio.h>
#include<conio.h>
void main()
{
 int a,i;
clrscr();
printf( " enter the number");
scanf("%d",&a);
for(i=a;i<a+1000;i++)
{
if(i==1||i==2||i==3||i==5||i==7)
{
printf("%d",i);
}
else if(i%2==0||i%3==0||i%5==0||i%7==0)
{
}
else
{
printf("%d",i);
break;
}
}
getch();
}

  Was this answer useful?  Yes

Vishal Bharadwaj

  • Aug 31st, 2018
 

Code
  1. #include <stdio.h>

  2.  

  3. void NextPrime(int n)

  4. {

  5.     int i;

  6.     for(i=2;i<=(n/2);i++)

  7.     {

  8.         if(n%i==0)

  9.         {

  10.            NextPrime(n+1);

  11.            return;

  12.         }

  13.     }

  14.   printf("Immediate next prime is:-   %d",n);

  15. }

  16.  

  17. int main(void)

  18. {

  19.         int n;

  20.         scanf("%d",&n);

  21.         NextPrime(n+1);

  22.         return 0;

  23. }

  24.  

  25.  

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions