## Friday, July 28, 2006

Write a program to print all prime numbers from 1 to 300.
(Hint: Use nested loops, break and continue statements.)

Improvements:
The work done by the computer is considerably used..if we chuck out all the even numbers..cuz even numbers cannot be prime numbers (except the number 2)

Also, 1 is not a prime number. Instead..it is a Unique Number.

#include<stdio.h>

main()
{

int number, div, ifprime;

for (number=2;number<=300;number++)

{

for (div=2; div<number; div++)

{
if (number%div==0)
{

ifprime=0;
break;
}

ifprime=1;

}

if (ifprime)
{
printf("\n%d", number);

}
}
}

The File can be found at:

Anonymous said...

very cofusing code

Anonymous said...

what is ifprime?

in if(ifprime), what is ifprime value?

what is ifprime value in if(ifprime)?

Surbhi Bakshi said...

That's just a variable!
Used to give 0 or 1 value based on whether that no is prime or not!
If it is 1, it is a prime!

Tanya said...

its very confusing..... pls elaborate wid a simple method....

Abdul wali said...
This comment has been removed by the author.
Abdul wali said...

Take a simple mehthod
#include
void main()
{
int a,b;count=2;
for(a=3;a<=50;a++)
{
for(b=2;b<=a-1;b++)
if(a%b==0)
break;
else if(b==a-1)
{
count=count+1;
printf("%d is a prime number\n",a);
}
}
getche();
}

@abdul

why did ya introduce count in here ????
whats the use ??

Anonymous said...

#include
int main()
{
int num,div;

for(num=2;num<=300;num++)
{
for(div=2;div<num;div++)
{
if((num%div==0))
{
break;
}
else if(div==num-1)
{
printf ("\n%d",num);
}
}
}

return(0);
}

Anonymous said...

tuhadi ma da phuda panchodo ya bnaya h q

even more simple is here...
#include
void main()
{
int i,num;

for(num=2;num<=1000 ;num++)
{
for(i=2;i<=num;i++)
{ if(num%i==0)
break;
}
if(i==num)
printf("\n%d is Prime",num);
}
}

This comment has been removed by the author.
Anonymous said...

realy good solution...!!!

Anonymous said...

realy good solution

Anonymous said...

Fucking solution.

Anonymous said...

int i,j;

for (i = 3; i <= 300; i++)
{
for (j = 2; j < i; j++)
{
if(i % j == 0)
//printf("%d\t",i);
break;
}
if (i == j)
printf("%d\t", i);
}

Anonymous said...

Sorry.. Here is the final one. There was a mistake in my earlier one :)

int i,j;

printf("2\t");

for (i = 3; i <= 300; i++)
{
for (j = 2; j < i; j++)
{
if(i % j == 0)
break;
}
if (i == j)
printf("%d\t", i);
}

Anantha Boudmanabhan said...

This may help!

#include
#include
main()
{
clrscr();
int i,n;

printf("The prime numbers b/w 1 and 300 are\n");

for(n=1;n<=300;n++)
{
i=2;
for(i=2;i<=n-1;i++)
{
if(n%i==0)
{break;
}
}
if(i==n)
printf("%d\n",n);
}
getch();
}

Anantha Boudmanabhan said...

This may help!

#include
#include
main()
{
clrscr();
int i,n;

printf("The prime numbers b/w 1 and 300 are\n");

for(n=1;n<=300;n++)
{
i=2;
for(i=2;i<=n-1;i++)
{
if(n%i==0)
break;
}
if(i==n)
printf("%d\n",n);
}
getch();
}