# Programming Videos

## Search

### C++, C Program to Check whether a Number is Prime or Not

Write a C, C++ program to check whether a number is prime or not. In this program, we take a number as input from a user and your program prints whether an input number is prime or not.

What is a Prime Number?

A prime number is a number which is greater than 1 , and divisible by 1 and itself

For example -  7 is a prime number it is divisible by 1 and 7 only.

NOTE -  2 is the only even prime number.

There are various methods to check whether a number is prime or not. You can check this tutorial for explanation.

Program to check whether a number is prime or not

Algorithm to  check whether number is prime or not

We run a loop from 2 to num/2 and check whether the number is divisible. If the number is not divisible by any number then it's a prime number.

Program to Print all prime numbers between 1 to 100.

Print even numbers from 1 to 100.

Sorting algorithms and their time complexity.

## C++ Program to Check whether a Number is Prime or Not

```#include<iostream.h>
using namespace std;

int main(){

int num, i, flag=0;

cout << "Enter a number\n";
cin  >> num;

/* Check whether number is divisible
by number other than 1 and itself
*/
for(i = 2; i <= num/2; i++){

if(num % i == 0){

val = 1;
break;

}
}

if(flag == 0)
cout << "An input number is a prime number";
else
cout<< " An input number is not a prime number";

return 0;
}

```

## C Program to Check whether a Number is Prime or Not

```#include <stdio.h>

int main(void) {

int num,i,val=0;

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

/* Check whether number is divisible
by number other than 1 and itself
*/
for(i = 2; i <= num/2; i++){

if(num % i == 0){

val=1;
break;

}
}

if(val==0)
printf (" An input number is a prime number");
else
printf (" An input number is not a prime number");

return 0;
}

```

Output :

Enter a number :  5

An input number is a prime number

Enter a number : 6

An input number is not a prime number