C Program to Check Whether a Number is Prime or Not

C Program to Check Whether a Number is Prime or Not

C program to check whether a number is prime or not; Through this tutorial, we will learn how to check whether a number is prime or not using for loop, while loop, and function.

A number that’s only divisible by 1 and itself is named a Prime Number. For Example, 3, 5, 7, 11 are Prime Numbers.

Algorithm to Check a Number is Prime or Not

Use the following algorithm to write a program to check a number is prime or not; as follows:

Step 1: Start
Step 2: Read number n
Step 3: Set f=0
Step 4: For i=2 to n-1
Step 5: If n mod 1=0 then
Step 6: Set f=1 and break
Step 7: Loop
Step 8: If f=0 then
         print 'The given number is prime'
        else
         print 'The given number is not prime'
Step 9: Stop

Programs to Check Whether a Number is Prime or Not in C

  • C Program to Check Prime Number Using For Loop
  • C Program to Check Prime Number Using While Loop
  • C Program to Check Prime Number Using Function

C Program to Check Prime Number Using For Loop

#include <stdio.h>
 
int main()
{
  int i, Number, count = 0; 
   
  printf("\n Please Enter any number to Check for Prime :- ");
  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;
}

The output of the above c program; as follows:

Please Enter any number to Check for Prime :- 10
10 is Not a Prime Number

C Program to Check Prime Number Using While Loop

#include <stdio.h>
 
int main()
{
  int i = 2, Number, count = 0; 
   
  printf("\n Please Enter any number to Check for Prime :- ");
  scanf("%d", &Number);
 
  while(i <= Number/2)
   {
     if(Number%i == 0)
     {
        count++;
	break;
      }
      i++;	
   }
   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;
}

The output of the above c program; as follows:

Please Enter any number to Check for Prime :- 11
11 is a Prime Number

C Program to Check Prime Number Using Function

#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 :- ");
  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;
}

The output of the above c program; as follows:

Please Enter any number to Check for Prime :- 31
31 is a Prime Number

Recommended C Programs

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *