Saturday, January 30, 2010

CHAP 3[E]a Printing prime numbers from 1 to 300

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

void main()
{
int d,f,e;
printf("\Input a number to print all the prime numbers from 1 to that value\n");
scanf("%d",&e);
printf("The prime numbers are 2");

for(d=1;d<=e;d++)
{
for(f=2;f {
if(d%f==0)
break;
if(f==d-1)
printf(" %d",d);
}
}
}

3 comments:

  1. void main()
    {
    int d,f,e;
    printf("\Input a number to print all the prime numbers from 1 to that value\n");
    scanf("%d",&e);
    printf("The prime numbers are 2");

    for(d=3;d<=e;d++)
    {
    for(f=2;f<(d/2);d++)
    {
    if(d%f==0)
    break;
    else
    printf(" %d",d);
    }
    }
    }

    ReplyDelete