C Program to Find Prime Number

Any natural number not divisible by other numbers except one and itself is called Prime Number in C. Let us see how to write a C Program to Find Prime Number using For Loop, While Loop, and Functions.

Prime Numbers: 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, etc. 2 is the only even prime number in C

C Program to Find Prime Number Using For Loop

This program allows the user to enter any integer value. Using this value, this C program will check whether the given number is a Prime number or Not using For Loop.

#include <stdio.h>
 
int main()
{
  int i, Number, count = 0; 
   
  printf("\n Please Enter any number to Check for Prime \n");
  scanf("%d", &Number);
 
  for (i = 2; i <= Number/2; i++)
   {
     if(Number%i == 0)
     {
        count++;
	break;
     }	
   }
   if(count == 0 && Number != 1 )
   {
   	printf("\n %d is a Prime Number", Number);
   }
   else
   {
   	printf("\n %d is Not a Prime Number", Number);
   }
  return 0;
}
C Program to Find Prime Number 1

Let me enter another Value for this prime number program in c

 Please Enter any number to Check for Prime 
199

 199 is a Prime Number

This time we will use different one

 Please Enter any number to Check for Prime 
365

 365 is Not a Prime Number

In this c prime number program, We initialized the integer i value to 1, and also (i <= Number) condition will help the For Loop to terminate when the condition fails.

Within the C Programming for loop, there is an If statement to check whether divisible by i is exactly equal to 0 or not. If the condition is True, then Count will increment, and then Break Statement will execute.

Next, we used another If statement to check, whether Count is Zero and it is Not equal to 1.

 Please Enter any Value
11

 11 is a Prime

User entered integer in the above program to find prime number example is 11

First Iteration: for(i = 2; i <= 11/2; i++)
It means condition inside the For loop (2 <= 5.5) is True
Now, Check the if condition – if (11%2 == 0). As you know, the condition is False
i++ means i will become 3

Second Iteration: for(i = 3; 3 <= 11/2; i++)
It means condition inside the For loop (3 <= 5.5) is True
if (11 % 3 == 0) – condition is False
i++ means i will become 4

Third Iteration: for(i = 4; 4 <= 11/2; i++)
It means condition inside the For loop of C prime number program (4 <= 5.5) is True
if (11 % 4 == 0) – condition is False
i++ means i will become 5

Fourth Iteration: for(i = 5; 5 <= 11/2; i++)
It means condition inside the For loop (5 <= 5.5) is True
if (11 % 5 == 0) – condition is False
i++ means i will become 6

Fifth Iteration: for(i = 6; 6 <= 11/2; i++)
It means condition inside the For loop (5 <= 5.5) of C program to find prime number is False. So, compiler will come out of the For Loop.

Next, the compiler will enter in to If statement. if(count == 0 && Number!= 1 ). In all Five iterations, If statement failed, so Count Value has not incremented from initialized o. And the Number that we inserted is 11. So, the condition is True, which means Prime. So, 11 is a prime.

C Program to Find Prime Number Using While Loop

This c program to check prime number allows the user to enter any integer value. Next, this program will check a number is a Prime or not using While Loop.

#include <stdio.h>
 
int main()
{
  int i = 2, Num, count = 0; 
   
  printf("\n Please Enter any Value \n");
  scanf("%d", &Num);
 
  while(i <= Num/2)
   {
     if(Num%i == 0)
     {
        count++;
	break;
      }
      i++;	
   }
   if(count == 0 && Num != 1 )
   {
   	printf("\n %d is a Prime", Num);
   }
   else
   {
 	printf("\n %d is Not", Num);
   }
  return 0;
}

We just replaced the For in the above program example with the While loop. If you don’t understand the While Loop, please refer to the article here: WHILE LOOP.

c program to check prime number using a While Loop output

 Please Enter any Value
30

 30 is Not

C Program to Find Prime Number Using Functions

This c program to check prime number allows the user to enter any integer value. User entered value will be passed to the Function that we created. Within this User defined function, this C program find Factors of a using For Loop.

#include <stdio.h>

int Find_Factors(int Number)
{ 
  int i, Count = 0; 
  
  for (i = 2; i <= Number/2; i++)
   {
    if(Number%i == 0)
     {
       Count++;
     } 
   }
   return Count;
}
 
int main()
{
  int Number, count = 0; 
   
  printf("\n Please Enter any number to Check for Prime \n");
  scanf("%d", &Number);
 
  count = Find_Factors(Number);
   if(count == 0 && Number != 1 )
   {
   	printf("\n %d is a Prime Number", Number);
   }
   else
   {
   	printf("\n %d is Not a Prime Number", Number);
   }
  return 0;
}
 Please Enter any number to Check for Prime 
61

 61 is a Prime Number

In this program, When the compiler reaches to Find_Factors line in the main() program, the compiler will immediately jump to below function:

int Find_Factors(int Number)

We already explained the LOGIC in the above example. If you did not understand the functions, Please refer Functions in C article for a better understanding

Comments are closed.