OverIQ.com

C Program to find Prime Numbers

Last updated on July 27, 2020


What are Prime numbers? #

Numbers which are either divisible by 1 or itself are known as Prime Numbers. For example, 11 is a prime number because it is only divisible by 1 and 11. However, number 6 is not prime since it is divisible by 2 and 3. The following is a C program to print the prime numbers between 2 and 50.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**************************************************************
 * Program to find the Prime numbers between 2 and 50
***************************************************************/

#include <stdio.h>

int main() 
{

    int is_prime = 1, n;

    for(int n = 2; n < 50; n++)
    {
        is_prime = 1;

        for(int i = 2; i < n; i++)
        {
            if(n % i == 0)
            {
                is_prime = 0;            
                break;
            }
        }

        if(is_prime)
        {
           printf("%d is prime\n", n);
        }   
    }

    return 0;
}

Expected Output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
31 is prime
37 is prime
41 is prime
43 is prime
47 is prime

How it works: #

To check for a prime number we successively divide a number n from 2 to n-1. If n is not divisible by any number between this range then it is a prime number.

Example 1: Is 5 a prime number?

Question Statement Result
Is 5 is divisible by 2 5 % 2 == 0 0 (false)
Is 5 is divisible by 3 5 % 3 == 0 0 (false)
Is 5 is divisible by 4 5 % 4 == 0 0 (false)

Hence, 5 is a prime number.

Example 2: Is 6 a prime number

Question Statement Result
Is 6 is divisible by 2 6 % 2 == 0 1 (true)

Number 6 is divisible by 2. Hence, it is not a prime number.

Check Prime Numbers using a user-defined function #

The following is a C program to check prime numbers using a user-defined function.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**************************************************************
 * Program to check whether the number entered by the user 
 * is prime or not
***************************************************************/

#include <stdio.h>
int check_prime_number(int);

int main() 
{
    int n;

    printf("Enter a number: ");
    scanf("%d", &n);

    if(check_prime_number(n))
    {
        printf("%d is prime", n);
    }
    else
    {
        printf("%d is not prime", n);
    }

    return 0;
}

int check_prime_number(int n)
{
    int is_prime = 1;

    if(n == 1)
    {
        return 0;
    }

    for(int i = 2; i < n; i++)
    {
        if(n % i == 0)
        {
            is_prime = 0;            
            break;
        }
    }

    return is_prime;
}

Expected Output: 1st run:

1
2
Enter a number: 11
11 is prime

2nd run:

1
2
Enter a number: 45
45 is not prime

Recommended Reading: