C Program To Find Prime Numbers From 2 To N, using While Loop

Lets write a C program to find and print / display all the prime numbers from 2 to N. Here N is the user entered number / limit.

Prime Number: Any natural number which is greater than 1 and has only two factors i.e., 1 and the number itself is called a prime number.

Related Read:
Decision Control Instruction In C: IF
Nested While Loop: C Program
C Program To Find Prime Number or Not using While Loop

Video Tutorial: C Program To Find Prime Numbers From 2 To N, using While Loop



YouTube Link: https://www.youtube.com/watch?v=iYHo-9rClAs [Watch the Video In Full Screen.]

While loop Logic

All the numbers are perfectly divisible by number 1, so we initialize the variables start and count to 2, instead of 1. So our c program starts checking for divisibility from number 2.

Source Code: C Program To Find Prime Numbers From 2 To N, using Nested While Loop

#include < stdio.h >
#include < math.h >

int main()
{
    int start = 2, num, count, flag, inum;

    printf("Enter the limit\n");
    scanf("%d", &num);

    printf("\n\nPrime numbers from 2 to %d are:\n", num);

    while(start <= num)
    {
        inum = sqrt(start);
        count= 2;
        flag = 1;

        while(count <= inum)
        {
            if(start%count == 0)
            {
                flag = 0;
                break;
            }
            count++;
        }

        if(flag) printf("%d, ", start);

        start++;
    }

    printf("\n\n");

    return 0;
}

Output 1:
Enter the limit
50

Prime numbers from 2 to 50 are:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,

Output 2:
Enter the limit
75

Prime numbers from 2 to 75 are:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73,

Output 3:
Enter the limit
100

Prime numbers from 2 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,

Output 4:
Enter the limit
500

Prime numbers from 2 to 500 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, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499,

Output 5:
Enter the limit
1000

Prime numbers from 2 to 1000 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, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997,

Logic To Find Prime Numbers From 2 To N, using While Loop

In this method, we apply square root to the user entered number and store it inside variable inum. This reduces the number of iterations of inner while loop.

For example,
If num = 100;
inum = sqrt(num);
inum = sqrt(100);
inum = 10;

User entered number 100 is perfectly divisible by 5 and 10, so number 100 is not a prime number.

So its enough if we iterate through the while loop sqrt(num) times to check if the user entered number is divisible by any number other than 1 and itself.

We start checking for prime number from number 2 till the user entered number. Variable start is assigned an initial value of 2. For each iteration of outer while loop value of start increments by 1. Inside the inner while loop we check if the value present in variable start is prime number or not. If its prime number then we print that number on to the console window.

Outer while loop selects the number and stores it in variable start on each iteration. Inner while loop checks if the selected number(present in variable start) is prime number or not. You can know the complete logic to check whether a selected number is prime or not: C Program To Find Prime Number or Not using While Loop

Table of all prime numbers up to 1,000:

Note: We are not using curly braces around if statement because we only have 1 line of code after if – so curly braces are optional. If we have multiple lines of code, then we must use curly braces to wrap around the block of code.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert