Friday, June 13, 2008

Find all prime numbers between 1-300

/*Write a program to print all prime numbers from 1-300 - Hint use break,continue & nested loops*/

Solution. Code :-
========

#include  <stdio.h>
int main(void)
{
int num,div ;
for (num = 2 ; num <= 300 ; num++ )
{
for ( div = 2 ; div <= num ;div++ )
{
if ( num % div == 0 )
break;
}
if ( div == num )
printf("\n%d",div);
}
}

1 comment:

Free soul said...

hi.this was really helping.
i know 2 is the only even prime number. but according to progaram in the first step when num=2 & div=2 then num%div shall be zero.....then how come in the output 2 is being shown as a prime number. please help.