C program to print all prime numbers between 1 to n

Write a C program to print all Prime numbers between 1 to n using loop. How to print all prime numbers between given interval using loop in C program. Logic to print prime numbers in a given range in C program.

Example

Input

Input lower limit: 1
Input upper limit: 20

Output

Prime numbers between 1-20: 2, 3, 5, 7, 13, 17, 19

Required knowledge

Basic C programming, If else, For loop, Nested loop

What is Prime number?

Prime number is a positive integer greater than 1 that is only divisible by 1 and itself. For example: 2, 3 , 5, 7, 11 are the first five prime numbers.

Logic to print prime numbers between 1 to n

Step by step descriptive logic to print all prime numbers between 1 to n.

  1. Input upper limit to print prime numbers from user. Store it in some variable say end.
  2. Run a loop from 1 to end, increment 1 in each iteration. The loop structure should be like for(i=2; i<=end; i++).
  3. Inside the loop for each iteration print value of i if it is prime number.

Program to print prime numbers between 1 to n

/**
 * C program to print all prime numbers between 1 to n
 */

#include <stdio.h>

int main()
{
    int i, j, end, isPrime; // isPrime is used as flag variable

    /* Input upper limit to print prime */
    printf("Find prime numbers between 1 to : ");
    scanf("%d", &end);

    printf("All prime numbers between 1 to %d are:\n", end);

    /* Find all Prime numbers between 1 to end */
    for(i=2; i<=end; i++)
    {
        /* Assume that the current number is Prime */
        isPrime = 1; 

        /* Check if the current number i is prime or not */
        for(j=2; j<=i/2; j++)
        {
            /*
             * If i is divisible by any number other than 1 and self
             * then it is not prime number
             */
            if(i%j==0)
            {
                isPrime = 0;
                break;
            }
        }

        /* If the number is prime then print */
        if(isPrime==1)
        {
            printf("%d, ", i);
        }
    }

    return 0;
}

Once you are done with generating prime numbers between 1 to n. You can easily modify the program to work for any range. To print all prime numbers in given range. You need to input upper as well as lower limit from user. Let us modify the above program to work for prime numbers in given range.

Program to print prime numbers in given range

/**
 * C program to print all prime numbers between 1 to n
 */

#include <stdio.h>

int main()
{
    int i, j, start, end;
    int isPrime;

    /* Input upper and lower limit to print prime */
    printf("Enter lower limit: ");
    scanf("%d", &start);
    printf("Enter upper limit: ");
    scanf("%d", &end);

    printf("All prime numbers between %d to %d are:\n", start, end);

    /* Find all Prime numbers between 1 to n */
    for(i=start; i<=end; i++)
    {
        /* Assume that the current number is Prime */
        isPrime = 1; 

        /* Check if the current number i is prime or not */
        for(j=2; j<=i/2; j++)
        {
            /*
             * If i is divisible by any number other than 1 and self
             * then it is not prime number
             */
            if(i%j==0)
            {
                isPrime = 0;
                break;
            }
        }

        /* If the number is prime then print */
        if(isPrime==1)
        {
            printf("%d, ", i);
        }
    }

    return 0;
}

Move a step forward and learn this program using functional approach.

Learn more - Program to print prime numbers in given range using function.

Output

Enter lower limit: 1
Enter upper limit : 100 
All prime numbers between 1 to 100 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,

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>