Friday, October 14, 2011

Write a C program to generate all the prime numbers between 1 and n, where n is a positive integer

#include //This is include information about standard library
main() //User define function
{
int n,a,b,c; //int funcion which takes all integer values
printf("Enter n value :"); //printing statement
scanf("%d",&n); //attaching the user input to the variable
c=0; //taking a variable as zero
for(a=1;n>=a;a++) //for loop for take the range to the user input
{ for(b=1;a>=b;b++) //for loop for printing the all prime numbers up to user input number
{ if(a%b==0) //if a%b is equal to zero
c=c+1; //incrimenting the value
}
if (c==2) //if c is equal to 2
{
printf("%d",a); //printing primenumbers upto given number
printf("\n");
c=0;
}
else //else statement
{
c=0; //stops the program
}
}
}

No comments:

Post a Comment