lucproblem008.c ​
Metadata ​
- Author — Amit Dutta (amitdutta4255@gmail.com)
- Last updated — 12 Dec 2025
- License — MIT
Problem Statement ​
Problem Statement
Write a problem to print all the prime numbers from 1 to 300.
Source Code ​
Printing the code
To print this file, open it on GitHub and click Raw before printing, or use the Download Raw button above and print directly from that page.
c
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#define LIMIT 300
int main()
{
printf("Prime numbers from 1 to 300 : 2"); // as 2 is the only even prime number
for (int i = 3; i <= LIMIT; i += 2) // skipping all other even number
{
int n = (int)sqrt(i);
bool prime = true;
for (int j = 3; j <= n; j += 2)
// an odd number is only devisable by another odd number.
// so, skipping even number.
{
if (i % j == 0)
{
prime = false;
break;
}
}
if (prime)
{
printf(" %d", i);
}
}
return 0;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31