C Program to Find Prime Number

Any natural number not divisible by other numbers except one and itself is called Prime Number. 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.

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 or find 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  = ");
  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", Number);
   }
  return 0;
}
C Program to Find Prime Number using for loop

Let me enter another Value for this example

 Please Enter any number to Check = 199

 199 is a Prime Number

This time we will use a different one

 Please Enter any number to Check = 365

 365 is Not

In this C Program to Find Prime Number, 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, the Count will increment, and Break Statement will execute.

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

 Please Enter any Value
11

 11 is a Prime

The user entered an integer in the above C program to Find Prime Number is 11.

First Iteration: for(i = 2; i <= 11/2; i++)
It means the 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 the 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 the condition inside the For loop of the C program to find the prime number (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 the 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 the condition inside the For loop (5 <= 5.5) is False. So, the compiler will come out of the For Loop.

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

C Program to Find Prime Number Using While Loop

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

#include <stdio.h>

int main(void)
{
    int i = 2, Num, count = 0;
    
    printf("Please Enter any Value = ");
    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 Find Prime Number using While Loop

C Program to Find Prime Number Using Functions

This c program allows the user to enter any integer value. The user-entered value will be passed to the Function that we created. Within this User defined function, this program finds Factors of using For Loop and the if statement.

#include <stdio.h>

int Find_Fact(int nm)
{
    int i, Count = 0;
    
    for (i = 2; i <= nm/2; i++)
    {
        if(nm%i == 0)
        {
            Count++;
        }
    }
    return Count;
}

int main(void)
{
    int nm, count = 0;
    
    printf("\n Please Enter any to Check  \n");
    scanf("%d", &nm);
    
    count = Find_Fact(nm);
    if(count == 0 && nm != 1 )
    {
        printf("\n %d is a Prime", nm);
    }
    else
    {
        printf("\n %d is Not", nm);
    }
    return 0;
}
C Program to check Prime Number using functions

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

int Find_Fac(int nm)

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

Comments are closed.