Prime Number Program in c: To Check whether a Given Number is Prime or Not

  • by

Prime number program in c: This is an example to check whether a number n (entered by the user) is prime or not using –

1. For Loop
2. If…else statement
3. While Loop
4. User-defined Function

In the last examples you will print Prime Numbers in c From 1 to N and also Between a Given Range (from 1 to 100)

What is a Prime Number?

Prime number is a natural number greater than 1, which is divisible by only two numbers ,i.e., by 1 and by the number itself.
For Example  2, 3, 5, 7, 11, 13… and so on are prime numbers

Why 2, 3, 5, 7, 11, 17, 19, etc. are prime numbers?

2 = 2 * 1
3 = 3 * 1
7 = 7 * 1
11 = 11 * 1
19 = 19 * 1

As they all have only 2 factors, 1 and the number itself. So, these are prime numbers. The smallest even prime number is 2 only.

Why 4, 6, 8, 9, 10, 18, 20, etc. are not prime numbers?

4 = (2 * 2) and (4 * 1)  are factors of 4
6 = (2 * 3) and (6 * 1) are factors of 6
9 = (3 * 3) and (9 * 1)
10 = (2 * 5) and (10 * 1)
20 = (2 * 5 * 2), (20 * 1) and (10 * 2)

All the numbers are having more than two factors, other than 1 and the number itself.
In the above example, 4 has 3 factors (1, 2, 4) and 20 has 5 factors (1, 2, 5, 10, 20). And according to prime number rule, a prime number should have only 2 factors. So, 4, 6, 8, 9, etc. are not prime numbers.
Non-prime numbers are called composite numbers.

Note: 1 has only one factor ,i.e., the number 1 itself. So, 1 is neither a prime nor a composite number.
1 = (1 * 1)
prime number program in c
Visit the page Prime Number, to learn more about it.

You should have the knowledge of following C Programming topics to understand the Prime Number program in c better –

  • C operator
  • C for loop
  • C while and do…while loop
  • C break and continue statements
  • C Functions

Prime Number Program in C Algorithm

An algorithm is the logic of the program. If we understand the logic of the program then it becomes easy to write program in c. Here, is the algorithm for Prime Numbers in C Programming.
  1. First, we will take two variables: n and flag=0. Variable flag indicates the success or unsuccess of the program. flag=0 indicates sucess and flag=1 indicates unsuccess.
  2. Input a number from the user and store it in variable n
  3. If the number entered by the user is 1 ,i.e., n=1 then print “1 is not a prime number”
  4. Else run for loop to check whether the number n is prime number or not in c programming
  5. If n%i=0 ,i.e., the number is divisible by any other number (except 1 and itself) then the value of flag is 1 ,i.e., flag=1. In this case, break the for loop and print “n is not a prime number”.
  6. Else print “the number n is a prime number”

Note: % is Modulus which returns remainder of a division. In this prime  number c program we have used  % to check whether the number is prime or not.

START
   Step 1 → Take integer variable N, I, FLAG=0
   Step 2 → Read N
   Step 3 → IF (N=1) DISPLAY 1 is neither a prime nor a composite number 
   Step 4 → ELSE SET I=2
   Step 5 → REPEAT STEPS 5 TO 9 WHILE I<=N/2: 
   Step 6 → IF (N%I=0) SET FLAG=1 and break the loop
   Step 7 → I = I + 1
   Step 8 → IF (FLAG=1) DISPLAY number is not prime
   Step 9 → ELSE DISPLAY number is prime
STOP

Prime Number C Program using For Loop


/* Prime Number c program using For Loop */

#include  //printf() & scanf() Header File  
#include  //clrscr() & getch() Header File    

int main()
{
    int n, i, flag = 0;

    clrscr(); // to clear output screen 
    
    /* to take number from the user */    
    printf("Enter a number: "); 
    scanf("%d", &n); 

    if (n==1)
    {
         printf("1 is not a prime number");
    }

    else
    {
	for(i=2; i<=n/2; i++)
    	{
            /* condition for non-prime number */
	    if (n%i==0)
	    {
                flag = 1;
	        break;
	    }
        }
        if (flag==1)
        {
             printf("%d is not a prime number", n);
        }
        else
        {
             printf("%d is a prime number", n);
        }
    }
 
    getch(); //to hold output screen 
    return(0);  
}
Output 1 –
Enter a number: 1 
1 is not a prime number
Output 2 –
Enter a number: 4 
4 is not a prime number
Output 3 –
Enter a number: 11 
11 is a prime number

Prime Number C Program Explanation

Let the number entered by the user is 13 ,i.e., n=13
for(i=2; i<=n/2; i++)    /* for loop is executed till i is equal to n/2 */
for(i=2; i<=13/2; i++)      /* for loop is executed till i is equal to 6 */

1st Iteration: for(i=2; i<=6; i++)
(13 % 2) is not equal to 0
value of flag=0
for loop will continue
i is incremented to 3 ,i.e., i=3

2nd Iteration: for(i=3; i<=6; i++)
(13 % 3) is not equal to 0
value of flag=0
for loop will continue
i is incremented to 4 ,i.e., i=4

3rd Iteration: for(i=4; i<=6; i++)
(13 % 4) is not equal to 0
value of flag=0
for loop will continue
i is incremented to 5 ,i.e., i=5

3rd Iteration: for(i=5; i<=6; i++)
(13 % 5) is not equal to 0
value of flag=0
for loop will continue
i is incremented to 6 ,i.e., i=6

4th Iteration: for(i=6; i<=6; i++)
(13 % 6) is not equal to 0
value of flag=0
for loop will continue
i is incremented to 7 ,i.e., i=7

5th Iteration: for(i=7; i<=6; i++)
for loop will terminate as i>6
As the value of flag=0 so 13 is a prime number

Prime Number C Program using While Loop


/* Prime Number c program using While Loop */

#include <stdio.h> //printf() & scanf() Header File    
#include <conio.h> //printf() & scanf() Header File       

int main()
{
    int n, i, flag = 0;

    clrscr(); // to clear output screen  
    
    /* to take number from the user */    
    printf("Enter a number: "); 
    scanf("%d", &n); 

    if (n==1)
    {
         printf("1 is not a prime number");
    }

    else 
    {
        i = 2;   
	while(i<=n/2)
    	{
            /* condition for non-prime number */
	    if (n%i==0)
	    {
                flag = 1;
	        break;
	    }
            i++;
        }
        if (flag==1)
        {
             printf("%d is not a prime number", n);
        }
        else
        {
             printf("%d is a prime number", n);
        }
    }
 
    getch(); // to clear output screen    
    return(0);  

}
Output 1 –
Enter a number: 1
1 is not a prime number
Output 2 –
Enter a number: 9 
9 is not a prime number
Output 3 –
Enter a number: 19 
19 is a prime number

C Program to Print Prime Numbers Between 1 to n


/* c program to find prime numbers between 1 to 100 (1 to n) */

#include <stdio.h> //printf() & scanf() Header File     
#include <conio.h> //clrscr() & getch() Header File     

int main()
{
    int n, end, i, flag;

    clrscr(); // to clear output screen 
    
    /* to take number from the user */    
    printf("Find all prime numbers between 1 to: "); 
    scanf("%d", &end); 

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

    for(n=2; n<=end; n++)
    {
	flag = 0;

        for(i=2; i<=n/2; i++)
    	{
            /* condition for non-prime number */
	    if (n%i==0)
	    {
                flag = 1;
	        break;
	    }
        }
        if (flag==0)
        {
             printf("%d\n, ", n);
        }
    }    
 
    getch(); // to clear output screen  
    return(0);  
}
Output –
Find all prime numbers between 1 to: 20
Prime numbers between 1 to 20 are:
2
3
5
7
11
13
17
19

C Program to Check Prime Number in a Given Range


/* c program to find prime numbers between 1 to 100 (a given range) */

#include <stdio.h> //printf() & scanf() Header File    
#include <conio.h> //clrscr() & getch() Header File      

int main()
{
    int n, begin, end, i, flag;

    clrscr(); // to clear output screen
    
    /* to take lower and upper limit from user to print prime numbers */    
    printf("Enter lower limit: "); 
    scanf("%d", &begin);
    printf("Enter upper limit: "); 
    scanf("%d", &end);

    if(begin2)
	begin = 2;

    printf("Prime numbers between %d to %d are:\n", begin, end);

    for(n=begin; n<=end; n++)
    {
	flag = 0;

        for(i=2; i<=n/2; i++)
    	{
            /* condition for non-prime number */
	    if (n%i==0)
	    {
                flag = 1;
	        break;
	    }
        }
        if (flag==0)
        {
             printf("%d\n, ", n);
        }
    }    
 
    getch(); //to hold output screen  
    return(0);  
}
Output –
Enter lower limit: 3
Enter lower limit: 20
Prime numbers between 3 to 20 are:
3
5
7
11
13
17
19

Check Prime Number in c using Function


/* Prime Number c program using User-Defined Function */

#include <stdio.h> //printf() & scanf() Header File  
#include <conio.h> //clrscr() & getch() Header File    

/* function declaration */
void prime_number(int);

int main()
{
    int n;

    clrscr(); // to clear output screen 
    
    /* to take number from the user */    
    printf("Enter a number: "); 
    scanf("%d", &n);

    /* calling function */
    prime_number(n); 

    getch(); //to hold output screen 
    return(0);  
}

void prime_number(int n);
{
    int i, flag = 0; 

    if (n==1)
    {
         printf("1 is not a prime number");
    }

    else
    {
	for(i=2; i<=n/2; i++)
    	{
            /* condition for non-prime number */
	    if (n%i==0)
	    {
                flag = 1;
	        break;
	    }
        }
        if (flag==1)
        {
             printf("%d is not a prime number", n);
        }
        else
        {
             printf("%d is a prime number", n);
        }
    }
 
}
Output 1 –
Enter a number: 1
1 is not a prime number
Output 2 –
Enter a number: 9 
9 is not a prime number
Output 3 –
Enter a number: 19 
19 is a prime number