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. C program to print all prime number within a given range. How to print all prime numbers between given interval using loop in C program.

Example:
Input lower limit: 1
Input upper limit: 10
Output prime numbers between 1-10: 2, 3, 5, 7

You can also check this program using the concept of functions - C program to find all prime numbers between 1 to n using functions.

Required knowledge

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

Prime number

Prime number is a positive integer greater than 1 that is only divisible by 1 and self.

Example: 2, 3 , 5, 7, 11 are the first five prime numbers

Before printing all primes between 1 to n you must first know how to check prime numbers in C.

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, n, isPrime; //isPrime is used as flag variable

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

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

    /* Finds all Prime numbers between 1 to n */
    for(i=2; i<=n; 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 is Prime number\n", i);
        }
    }

    return 0;
} 

Note: For checking whether a number is Prime or not we just need to check that the number should not be divisible by any number between 2 to n-1. Apart from that you can also check between 2 to n/2. Since any number more than (n/2)+1 cannot be exactly divided by n except self n.

Output
Find prime numbers between 1 to : 100

All prime numbers between 1 to 100 are:
2 is Prime number
3 is Prime number
5 is Prime number
7 is Prime number
11 is Prime number
13 is Prime number
17 is Prime number
19 is Prime number
23 is Prime number
29 is Prime number
31 is Prime number
37 is Prime number
41 is Prime number
43 is Prime number
47 is Prime number
53 is Prime number
59 is Prime number
61 is Prime number
67 is Prime number
71 is Prime number
73 is Prime number
79 is Prime number
83 is Prime number
89 is Prime number
97 is Prime number

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

2 comments:

  1. Replies
    1. Thanks @Akshay for observing. There was a small logical bug which has been removed.

      Delete