In this tutorial, you will learn how to write a C program to print all prime numbers between 1 to n using a while loop. We will see some examples along with programming logic, algorithms, and Programing explanations.
Required Knowledge
- C Programming Basics
- C Programming Operators
- C printf and Scanf
- While loop in C
Problem Statement
We have to write a C program that takes the number n as input and prints all prime numbers between 1 and n using a while loop. For a better understanding, we have an example below that will help to know what we have to achieve.
Example
Input:
Enter the value of n: 20
Output:
2
3
5
7
11
13
17
19
Algorithm
- Start the program.
- Declare the variables
n,num, andisPrime. - Read the value of
nfrom the user. - Initialize
numto 2. - Enter the outer while loop with the condition
num <= n.- Initialize
isPrimeto 1 (true). - Declare the variable
divisorand initialize it to 2. - Enter the inner while loop with the condition
divisor <= sqrt(num).- Check if
numis divisible bydivisorwithout a remainder.- If true, set
isPrimeto 0 (false) and break out of the loop. - If false, continue to the next iteration.
- If true, set
- Increment
divisorby 1.
- Check if
- Exit the inner while loop.
- Check the value of
isPrime.- If
isPrimeis 1, printnumas a prime number. - If
isPrimeis 0, continue to the next iteration.
- If
- Increment
numby 1.
- Initialize
- Exit the outer while loop.
- End the program.
Program to Print all Prime numbers between 1 to n
#include <stdio.h>
#include <math.h>
int main() {
int n, num = 2, isPrime;
printf("Enter the value of n: ");
scanf("%d", &n);
while (num <= n) {
isPrime = 1;
int divisor = 2;
while (divisor <= sqrt(num)) {
if (num % divisor == 0) {
isPrime = 0;
break;
}
divisor++;
}
if (isPrime == 1) {
printf("%d\n", num);
}
num++;
}
return 0;
}
Output
Enter the value of n: 7
2
3
5
7
Program Explanation
- The program takes input for the value of n from the user.
- It initializes a variable
numto 2, which represents the first number to be checked for primality. - The program enters a while loop with the condition
num <= nto iterate through numbers from 2 to n. - Inside the while loop, it checks if
numis a prime number. - It initializes a variable
isPrimeto 1, which represents the initial assumption thatnumis prime. - It initializes a variable
divisorto 2, which will be used to check divisibility. - It enters another while loop with the condition
divisor <= num/2to check ifnumis divisible by any number other than 1 and itself. - Inside the inner while loop, it checks if
numis divisible by the current value ofdivisor. - If
numis divisible, it sets the value ofisPrimeto 0 and breaks out of the inner while loop. - It then increments the value of
divisorby 1 to move on to the next possible divisor. - The inner while loop continues until
divisorexceeds half ofnum. - After the inner while loop, it checks the value of
isPrime. - If
isPrimeis 1, it prints the value ofnum, indicating that it is a prime number. - It then increments the value of
numby 1 to move on to the next number. - The outer while loop continues until
numexceeds the value of n. - Once the while loop is complete, the program ends.
This program explanation provides a step-by-step breakdown of how the program works to print all prime numbers between 1 and n using a while loop.
Conclusion
The C program successfully prints all prime numbers between 1 and n using a while loop. By iterating through the numbers and checking each number for primality using a nested while loop, it identifies and displays the prime numbers on the screen.





