# C Program To Check Prime Number Using Function

In this post, we will learn how to check the Prime Number using function in C Programming language.

Any whole number which is greater than 1 and has only two factors 1 and itself is known as the Prime Number. For example: 2, 3, 5, 11, 47, . . . so on.

We will check whether the entered number has two or more factors using functions. If the entered number has more than two factors, then it’s not a Prime Number.

So, without further ado, let’s begin the tutorial.

## C Program To Check Prime Number Using Function

```// C Program To Check Prime Number Using Function
#include <stdio.h>
int checkPrime(int num){
int count = 0;

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

int main(){
int num;

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

if (checkPrime(num) == 0){
printf("%d is a Prime Number.", num);
}
else{
printf("%d is not a Prime Number.", num);
}
return 0;
}
```

Output 1

``````Enter a number: 17
17 is a Prime Number.``````

Output 2

``````Enter a number: 91
91 is not a Prime Number.``````

## How Does This Program Work ?

``    int num;``

In this program, we have declared an int data type variable named num.

``````    // Asking for Input
printf("Enter a number: ");
scanf("%d", &num);``````

Then, the user is asked to enter a number. The value of this number will get stored in the num named variable.

``````    if (checkPrime(num) == 0){
printf("%d is a Prime Number.", num);
}
else{
printf("%d is not a Prime Number.", num);
}``````

Then, we call a custom function named checkPrime which we have declared above. This function will return 0 if the entered number is Prime and 1 if the number is not prime.

If the value of checkPrime(int num) = 0, then the number is a Prime number and we’ll print it.

Otherwise the number is not a Prime Number, and we will print The entered number is not a Prime Number.

``````int checkPrime(int num){
int count = 0;

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

We have declared a custom function named checkPrime which checks whether the entered number is a Prime Number or Not.

If this function returns 0 as output, then the number is a Prime Number, otherwise the entered number is not a Prime Number.

## Conclusion

I hope after going through this post, you understand how to check the Prime number using function in C Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.