bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / letusc / lucproblem008.c

lucproblem008.c (862B)


      1 /* Write a problem to print all the prime numbers from 1 to 300. */
      2 /* Let Us C, Chap - 6, Page - 101, Problem 6.1 */
      3 
      4 // Method: Trial Division (Optimized to check up to sqrt(N))
      5 
      6 #include <stdio.h>
      7 #include <math.h>
      8 #include <stdbool.h>
      9 
     10 #define LIMIT 300
     11 
     12 int main()
     13 {
     14     printf("Prime numbers from 1 to 300 :  2"); // as 2 is the only even prime number
     15     for (int i = 3; i <= LIMIT; i += 2)  // skipping all other even number
     16     {
     17         int n = (int)sqrt(i);
     18         bool prime = true;
     19 
     20         for (int j = 3; j <= n; j += 2)
     21         // an odd number is only devisable by another odd number.
     22         // so, skipping even number.
     23         {
     24             if (i % j == 0)
     25             {
     26                 prime = false;
     27                 break;
     28             }
     29         }
     30         if (prime)
     31         {
     32             printf("  %d", i);
     33         }
     34     }
     35     return 0;
     36 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror