Program to find a prime number after a given number: I used the theory that prime numbers are divisible by only 2 numbers 1 and itself.
#include <stdio.h>
#include <conio.h>
int main(int argc, char const *argv[])
{
int num,a=0,i,j;
printf("Enter a number: ");
scanf("%d",&num);
for(i=num+1;i>0;i++){
for(j=1;j<=i;j++){
if(i%j==0){
a=a+1;
}
}
if(a==2){
printf("%d",i);
break;
}
}
return 0;
}
This code is working only for a single increment like if user gives the input as 4 it is returning the output as 5. But it wont return output if input is given as 7 (it should return 11)
Then I modified the code as below :
#include <stdio.h>
#include <conio.h>
void main(int argc, char const *argv[])
{
int num,a=0,i,j;
printf("Enter a number: ");
scanf("%d",&num);
for(i=num+1;i>0;i++){
a=0;
for(j=1;j<=i;j++){
if(i%j==0){
a=a+1;
}
}
if(a==2){
printf("%d",i);
break;
}
}
}
This one runs well. I am confused why the program gives the output if i declared the variable after the for loop but fails to give it if declared before?