C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Print all prime numbers between 1 and N

Introduction

A Prime number is a number greater than 1 that is divisible only by 1 and itself.
For example:
 Prime numbers: 2, 3, 5, 7, 11, 13
 Not prime: 4, 6, 8, 9, 10

In this program, the user enters a positive integer N, and the program prints all prime numbers from 1 to N using nested for, while and do..while loops.

 

C Program: Print all prime numbers between 1 and N

Method 1: Using for loop

C

#include <stdio.h>

 

int main() {

    int n, i, j, flag;

 

    // Input

    printf("Enter the value of N: ");

    scanf("%d", &n);

 

    // Validate input

    if (n <= 1) {

        printf("There are no prime numbers less than or equal to %d.\n", n);

        return 0;

    }

 

    printf("Prime numbers between 1 and %d are:\n", n);

 

    // Loop through numbers from 2 to n

    for (i = 2; i <= n; i++) {

        flag = 0;

 

        // Check divisibility

        for (j = 2; j <= i / 2; j++) {

            if (i % j == 0) {

                flag = 1;

                break;

            }

        }

 

        // If no divisors found, print the prime number

        if (flag == 0)

            printf("%d ", i);

    }

 

    printf("\n");

    return 0;

}

Output

 
OUTPUT 1 :
Enter the value of N: 20
Prime numbers between 1 and 20 are:
2 3 5 7 11 13 17 19


OUTPUT 2 :
Enter the value of N: 10
Prime numbers between 1 and 10 are:
2 3 5 7


Explanation

  1. The user inputs an upper limit n.
  2. The outer loop runs from 2 to n (each number is checked).
  3. The inner loop checks whether each number i is divisible by any number from 2 to i/2.
  4. If no divisor is found (flag == 0), that number is prime.
  5. The program prints all prime numbers up to n.

 

C Program: Print all prime numbers between 1 and N

Method 2: Using while loop

C

#include <stdio.h>

 

int main() {

    int n, i = 2, j, flag;

 

    // Input

    printf("Enter the value of N: ");

    scanf("%d", &n);

 

    // Validate input

    if (n <= 1) {

        printf("There are no prime numbers less than or equal to %d.\n", n);

        return 0;

    }

 

    printf("Prime numbers between 1 and %d are:\n", n);

 

    // Loop through numbers from 2 to n

    while (i <= n) {

        flag = 0;

        j = 2;

 

        // Check if i is prime

        while (j <= i / 2) {

            if (i % j == 0) {

                flag = 1;

                break;

            }

            j++;

        }

 

        // If no divisors found, print i

        if (flag == 0)

            printf("%d ", i);

 

        i++;

    }

 

    printf("\n");

    return 0;

}

Output

 
OUTPUT 1 :
Enter the value of N: 15
Prime numbers between 1 and 15 are:
2 3 5 7 11 13


OUTPUT 2 :
Enter the value of N: 5
Prime numbers between 1 and 5 are:
2 3 5


Explanation

  1. User inputs an upper limit n.
  2. Variable i starts at 2 (the smallest prime number).
  3. Outer while loop iterates from i = 2 to n.
  4. Inner while loop checks if i is divisible by any number from 2 to i/2.
  5. If no divisors are found (flag == 0), the number is prime and printed.
  6. i increments until the loop reaches n.

 

C Program: Print all prime numbers between 1 and N

Method 3: Using do..while loop

C

#include <stdio.h>

 

int main() {

    int n, i = 2, j, flag;

 

    // Input

    printf("Enter the value of N: ");

    scanf("%d", &n);

 

    // Validate input

    if (n <= 1) {

        printf("There are no prime numbers less than or equal to %d.\n", n);

        return 0;

    }

 

    printf("Prime numbers between 1 and %d are:\n", n);

 

    // Loop through numbers from 2 to n

    do {

        flag = 0;

        j = 2;

 

        // Check if i is prime using inner do...while loop

        if (i > 2) { // for numbers greater than 2

            do {

                if (i % j == 0) {

                    flag = 1;

                    break;

                }

                j++;

            } while (j <= i / 2);

        }

 

        // If no divisors found, print i

        if (flag == 0)

            printf("%d ", i);

 

        i++;

    } while (i <= n);

 

    printf("\n");

    return 0;

}

Output

 
OUTPUT 1 :
Enter the value of N: 20
Prime numbers between 1 and 20 are:
2 3 5 7 11 13 17 19


OUTPUT 2 :
Enter the value of N: 10
Prime numbers between 1 and 10 are:
2 3 5 7


Explanation

  1. The user inputs an upper limit n.
  2. Outer ..while loop iterates i from 2 to n.
  3. For each i, an inner ..while loop checks if i has divisors from 2 to i/2.
  4. If no divisors are found (flag == 0), the number is prime and printed.
  5. This continues until all numbers up to n are checked.