Home » C program to find prime number using a function

C program to find prime number using a function

Learn about C program to find prime number using a function in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Contents

C program to find prime number using a function

Program:

#include<stdio.h>
int checkPrime(int number)
{
  int count = 0;

  for(int i=2; i<=number/2; i++)
  {
     if(number%i == 0)
     {
       count=1;
       break;
     }
  }

  if(number == 1) count = 1;

  return count;
}

int main()
{
  int number ;

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

  if(checkPrime(number) == 0)
  printf("%d is a prime number.", number);
  else
  printf("%d is not a prime number.", number);

  return 0;
}

Output:

Enter number: 5
5 is a prime number.

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
Find nth term of Fibonacci series in C using function
Display fibonacci series upto Nth term