**Ex:**write a C program to check given number is prime or not using function. How to write a C program to check given number is prime or not using function. C program to check given number is prime or not using function.

**Input from user:**

Enter the number: 5

**Expected output:**

Given number is prime number.

** **** Step by Step logic of the given program:**

1. First declare function give it meaningful name prime().

2. Inside the main() accept input (number) from user declare varible say no.

3. Using

__if__statement call prime function- if(prime(no)). after that inside the if statement print given number is prime otherwise print number is not prime. (If returned value is

**1**(true) then it will print number is prime otherwise it will execute else block.

4. After that inside the prime() we will use following prime numbers logic-

for(i=1;i<=no;i++)

{

if(no%i==0)

{

count++;

}

}

Here,

**count**will be 2 only if number is prime.

5. In the last check

**count**is 2 or not using- if(count==2)

If it's true

__return 1__otherwise

__return 0__;

** C program to check given number is prime or not using function:**

#include<stdio.h>

int prime(int);

int main()

{

int no;

printf("Enter the number\n");

scanf("%d",&no);

if(prime(no))

{

printf("Given number is prime number");

}

else

{

printf("Given number is not prime number");

}

}

int prime(int no)

{

int count=0,i;

for(i=1;i<=no;i++)

{

if(no%i==0)

{

count++;

}

}

if(count==2)

{

return 1;

}

else

{

return 0;

}

}

Above program shows the following

**output:**

**You May Like:**

## No comments:

## Post a Comment

If you have any doubts, please discuss here...