C Program to Check Whether a Number is Prime or Not Using User Defined Function

#include<stdio.h>
#include<conio.h>


#include

/* Function prototype to check prime number */
int check_prime(int n);

/* Main function */
int main()
{
	 int number, i, flag=0;

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

	 // Function call and taking decision
	 if(check_prime(number)==1)
	 {
	  	printf("%d is PRIME.", number);
	 }
	 else
	 {
	  	printf("%d is NOT PRIME.", number);
	 }

	 return(0);
}


int check_prime(int n)
{
    int i, flag=0;
    for(i=2;i<=n/2; i++)
	 {
		  if(n%i==0)
		  {
			   return 0;
		  }
	 }
	 return 1;
}

Output

Run 1:
-----------
Enter integer number: 13 ↲
13 is PRIME.

Run 2:
-----------
Enter integer number: 16 ↲
16 is NOT PRIME.

Run 3:
-----------
Enter integer number: 1 ↲
1 is NOT PRIME.

Run 4:
-----------
Enter integer number: 2 ↲
2 is PRIME.

Leave a Reply

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

https://atozonlinesolutions.com/wp-content/uploads/2017/03/logo_white.png
https://atozonlinesolutions.com/wp-content/uploads/2017/03/logo_white.png
About
Products
Subscribe

If you wish to receive our latest news in your email box, just subscribe to our newsletter. We won’t spam you, we promise!

    Applauz

    As the pioneer of the lean startup movement, APPLAUZ has dedicated it’s time to sharing effective business strategies that help new businesses and enterpreneurs put their money to work in the right way.

    Subscribe

    If you wish to receive our latest news in your email box, just subscribe to our newsletter. We won’t spam you, we promise!

      Applauz

      As the pioneer of the lean startup movement, APPLAUZ has dedicated it’s time to sharing effective business strategies that help new businesses and enterpreneurs put their money to work in the right way.

      Copyright by BOLD THEMES. All rights reserved.