The Program in C that prints all prime numbers from 1 to 300 is given below:
#include <stdio.h>
int main()
{
int i, j, isPrime;
printf("Prime numbers between 1 and 300 are: \n");
for(i=2; i<=300; i++)
{
isPrime = 1;
for(j=2; j<=i/2; j++)
{
if(i%j == 0)
{
isPrime = 0;
break;
}
}
if(isPrime == 1)
printf("%d ",i);
}
return 0;
}
Output:
Prime numbers between 1 and 300 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293
Pro-Tips💡
This program uses a nested for loop to check if each number between 1 and 300 is prime.
The outer loop iterates through all the numbers between 1 and 300, and the inner loop checks if the number is prime by dividing it by all numbers between 2 and half of the number.
If no divisors are found, the number is prime and is printed to the screen.
Note that this program is using the basic method to check the primality, there are more optimized algorithms to find prime numbers.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.