C Program to print all prime numbers from 1 to 300.

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.) 


  1. #include<stdio.h>
  2. main()
  3. {
  4.  
  5. int number, div, ifprime;
  6.  
  7.  
  8.  
  9. for (number=2;number<=300;number++)
  10.  
  11. {
  12.  
  13. for (div=2; div<number; div++)
  14.  
  15. {
  16. if (number%div==0)
  17. {
  18.  
  19. ifprime=0;
  20. break;
  21. }
  22.  
  23. ifprime=1;
  24.  
  25. }
  26.  
  27. if (ifprime)
  28. {
  29. printf("\n%d", number);
  30.  
  31. }
  32. }
  33. }


Try Some simple method to print all prime numbers from 1 to 300:

  1. #include
  2. void main()
  3. {
  4. int a,b;count=2;
  5. for(a=3;a<=50;a++)
  6. {
  7. for(b=2;b<=a-1;b++)
  8. if(a%b==0)
  9. break;
  10. else if(b==a-1)
  11. {
  12. count=count+1;
  13. printf("%d is a prime number\n",a);
  14. }
  15. }
  16. getche();
  17. }

  1. #include
  2. int main()
  3. {
  4. int num,div;
  5.  
  6. for(num=2;num<=300;num++)
  7. {
  8. for(div=2;div<num;div++)
  9. {
  10. if((num%div==0))
  11. {
  12. break;
  13. }
  14. else if(div==num-1)
  15. {
  16. printf ("\n%d",num);
  17. }
  18. }
  19. }
  20.  
  21. return(0);
  22. }


even more simple is here C Program to print all prime numbers from 1 to 300.



  1. #include
  2. void main()
  3. {
  4. int i,num;
  5.  
  6. for(num=2;num<=1000 ;num++)
  7. {
  8. for(i=2;i<=num;i++)
  9. { if(num%i==0)
  10. break;
  11. }
  12. if(i==num)
  13. printf("\n%d is Prime",num);
  14. }
  15. }


c program to print prime numbers in a given range


You can change the <= 300; like i <=1000 to print all prime numbers from 1 to 1000.
  1. int i,j;
  2.  
  3. printf("2\t");
  4.  
  5. for (= 3; i <= 300; i++)
  6. {
  7. for (= 2; j < i; j++)
  8. {
  9. if(% j == 0)
  10. break;
  11. }
  12. if (== j)
  13. printf("%d\t", i);
  14. }


Learn More :