Complete Prime Checker Function

By | June 22, 2019

“Complete Prime Checker Function” is a C program using functions in C programming language. Prime checker program will use an isPrime() function to check whether the given number is prime or not. isprime() function will take an integer parameter. It will return 1 or zero. isPrime() function will return 1 if the given number is prime or it will return a zero if the number is composite.

Complete Prime Checker Function

Complete Prime Checker Function

Is 1 Prime Number?

The definition of prime number is ” A prime number can have only two factors / divisors. For example 5 is prime number because it has only two divisors 1 and 5. But 1 has only one divisor so it is not a prime number.

Is 1 Composite Number?

By definition of a composite number we know that a number is composite if it has more than two divisors / factors. But 1 has only one divisor so 1 is not a composite number.

Is 1 Prime or Composite Number?

By definition of prime number and composite number we know that 1 is neither prime nor composite. We explained the definitions in above paragraphs.

Is 2 Prime?

Yes 2 is a prime number because 2 has two divisors that is 1 and 2.

The following code block show the source code of complete prime number checker program in C programming using function.

 

/*
Write a C program to use a function int checkPrime(int n)
that receives one number and returns 1 if number
is prime or zero if number is composite. It will return 2
if number is 1, because 1 is neither prime nor
composite, it will return 3 if 0 or negative number
is entered by user
*/
int checkPrime(int n); /*function prototype or declaration*/
#include<stdio.h>
int main()
{
   int num1,ans;
   /* Enter one number in main() */
   printf("Enter a number for prime checker program=");
   scanf("%d", &num1);
   /* call the function by sending one number */
   ans = checkPrime(num1);
   if ( ans ==1)
     printf("%d is a Prime number",num1);
   else if ( ans==0)
     printf("%d is a Composite number",num1);
    else if(ans==2) 
    {
	 printf("1 is neither prime nor composite \nSince by definition every prime has two divisors");
	 printf("\nbut 1 has only one divisor that is 1\n And one is not composite \n");
	 printf("because every composite has more than two\ndivisors but 1 has only one divisor");
    }
	else 
      printf("Invalid input, enter only positive natural number");
   return 0;  
}
/* wrtite down function definition */

int checkPrime(int n)
{
	int i, flag=1; /*flag=1 means number is prime*/
	
	if(n==1)
	  return 2; /* 1 is neither prime nor composit*/
	else if (n<=0)
      return 3; /*invalid input*/
	  	
	for(i=2;i<=(n/2);i++)
	if(n % i == 0)
	  {
	    flag=0;
	    break;
	  }
	  if(flag==1)
	   return 1; /*flag=1 means number is prime*/
	   else
	   return 0; /*number is composite*/
}

Loading

2 thoughts on “Complete Prime Checker Function

  1. Pingback: C Program to check for Prime or Composite Number - EasyCodeBook.com

Leave a Reply

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