You are currently viewing C Program To Print All Prime Numbers Between 1 to n Using While Loop.

C Program To Print All Prime Numbers Between 1 to n Using While Loop.

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

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

  1. Start the program.
  2. Declare the variables n, num, and isPrime.
  3. Read the value of n from the user.
  4. Initialize num to 2.
  5. Enter the outer while loop with the condition num <= n.
    • Initialize isPrime to 1 (true).
    • Declare the variable divisor and initialize it to 2.
    • Enter the inner while loop with the condition divisor <= sqrt(num).
      • Check if num is divisible by divisor without a remainder.
        • If true, set isPrime to 0 (false) and break out of the loop.
        • If false, continue to the next iteration.
      • Increment divisor by 1.
    • Exit the inner while loop.
    • Check the value of isPrime.
      • If isPrime is 1, print num as a prime number.
      • If isPrime is 0, continue to the next iteration.
    • Increment num by 1.
  6. Exit the outer while loop.
  7. 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

  1. The program takes input for the value of n from the user.
  2. It initializes a variable num to 2, which represents the first number to be checked for primality.
  3. The program enters a while loop with the condition num <= n to iterate through numbers from 2 to n.
  4. Inside the while loop, it checks if num is a prime number.
  5. It initializes a variable isPrime to 1, which represents the initial assumption that num is prime.
  6. It initializes a variable divisor to 2, which will be used to check divisibility.
  7. It enters another while loop with the condition divisor <= num/2 to check if num is divisible by any number other than 1 and itself.
  8. Inside the inner while loop, it checks if num is divisible by the current value of divisor.
  9. If num is divisible, it sets the value of isPrime to 0 and breaks out of the inner while loop.
  10. It then increments the value of divisor by 1 to move on to the next possible divisor.
  11. The inner while loop continues until divisor exceeds half of num.
  12. After the inner while loop, it checks the value of isPrime.
  13. If isPrime is 1, it prints the value of num, indicating that it is a prime number.
  14. It then increments the value of num by 1 to move on to the next number.
  15. The outer while loop continues until num exceeds the value of n.
  16. 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.