# Program to Check Whether a Number is Prime or Not in C

## Prime Number :

Prime Number is defined as the number which is divisible by itself and by one only. It should not be divisible by any other number.

## Working of Program :

In the program, we will take a value from the user and use loop and conditional statement to check whether a number is prime or not.

Run
```#include<stdio.h>
int main() {
int val;
printf("Enter the number to be checked :");
scanf("%d", &val);

for(int i=2; i < val; i++){

if(val%i ==0){
printf("Given Number %d is not prime number", val);
return 0;
}
}
printf("Given Number %d is a Prime Number", val);
return 0;
}

```

### Output :

```Enter the number to be checked : 17
17
Given Number 17 is a Prime Number
```

## Example :

Let’s solve the same problem by same algorithm but this time, we optimize the code little bit by running the for loop to square root of n times.

Run
```#include<stdio.h>
int main() {
int val;
printf("Enter the number to be checked :");
scanf("%d", &val);

for(int i=2; i*i < val; i++){

if(val%i ==0){
printf("Given Number %d is not prime number", val);
return 0;
}
}
printf("Given Number %d is a Prime Number", val);
return 0;
}

```

### Output :

```Enter the number to be checked : 17
17
Given Number 17 is a Prime Number
```

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

## Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others