How to write a C Program to print all prime numbers from 1 to 300. Using nested loops, break and continue statements in C Programming Language ?
This C Program to print all prime numbers from 1 to 300. (Hint: Use nested loops, break and continue statements.)- #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);
- }
- }
- }
Try Some simple method to print all prime numbers from 1 to 300:
- #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();
- }
- #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);
- }
even more simple is here C Program to print all prime numbers from 1 to 300.
- #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);
- }
- }
c program to print prime numbers in a given range
You can change the i <= 300; like i <=1000 to print all prime numbers from 1 to 1000.
- 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);
- }