Prime numbers are positive integers greater than 1 that have exactly two factors: 1 and the number itself. This program generates and displays all prime numbers from 1 to N.
- A prime number is not divisible by any number other than 1 and itself.
- The program checks each number in the given range and prints it if it is prime.
Illustration
For N = 50, the program checks every number from 1 to 50 and prints the prime numbers:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

Approach
- Iterate through all numbers from 1 to N.
- For each number, check whether it is prime.
- In isPrime(), return false for numbers less than 2.
- Check divisibility from 2 to √n.
- If any value divides n completely, the number is not prime.
- Otherwise, print the number.
Example: Program to Print Prime Numbers From 1 to N
#include <stdbool.h>
#include <stdio.h>
#include <math.h>
// This function is to check
// if a given number is prime
bool isPrime(int n)
{
// 0 and 1 are not prime numbers
if (n == 1 || n == 0)
return false;
// Check for divisibility from 2 to sqrt(n)
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0)
return false;
}
return true;
}
// Driver code
int main()
{
int N = 50;
// Check every number from 1 to N
for (int i = 1; i <= N; i++) {
if (isPrime(i)) {
printf("%d ", i);
}
}
return 0;
}
Output
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Explanation: The program checks every number from 1 to N using isPrime(). For each number, divisibility is tested up to its square root; if no divisor is found, the number is printed as prime.