Verification of a given number if prime or not

Program Description

Accepts a value from user and checks whether the given number is a prime number or not using function.

Program
/*******************************************
*  code.cheraus.com
*  
*  Verification of a given number if prime or not
*	 
*
*******************************************/
#include<stdio.h>

//function prototype
int prime(int no);

int main()
{
   int num,ans;
   
   //taking user input
   printf("\nEnter the number : ");
   scanf("%d",&num);
   ans = prime(num);
   
   if(ans==1)
    printf("\nPrime Number\n");
   else
    printf("\nNot a Prime Number");
   return 0; 
}

/*

Function definition for prime function

Function: prime() .The function takes a single integer number and
          cheack whether the number is prime or not
Input  : Takes a single integer as argument,which is to be verified.

Return : The function returns 
          1-> if the given number is prime
          0-> if the given number is not prime		  

*/
int prime(int no)
{
   int i;
   for(i=2;i<no;i++)
   {
     if(no%i==0)
      return 0;  //not a prime number
   }
   
   //prime number
     return 1;
}

Output
Test Case 1:

Enter the number : 24
Not a Prime Number
Test Case 2:

Enter the number : 47
Prime Number
Test Case 3:

Enter the number : 49
Not a Prime Number

Comments
comments powered by Disqus