Friday, April 13, 2012

c program for prime number between 1 to n


I have assumed N to be hundred. You can use scanf to input N and display prime number between 1 to n.


Prime Numbers are numbers which are only divisible by two numbers. eg 2,3,5,7,11 etc.


c program for prime number


int main()
{
int i,j,n=0;
for(i=2;i<=100;i++)
{
for(j=1;j<=i;j++)
{
if(i%j==0)
{
n++;
}
}
if(n==2)
printf("%d\n",i);
n=0;
}
getch();
}

4 comments:

  1. very nice,
    easy to understand

    ReplyDelete
  2. i like the idea, but there is a bug. You have to set n = 0 in an else branch as well because otherwise it doesn't reset for each i.

    ReplyDelete
  3. @above
    n=0 is not part of if block since there are no {} and therefore we don't need else block

    ReplyDelete