Q1.Write a code to check whether the given number is prime number or not

The article will help you write a algorithm and program to Check Whether a Number is Prime or not.

Prime Number:- A number that is divisible by only and only 1 and itself is known as a Prime Number. For example: – 11 is only divisible by 1, so 11 is prime, while 10 is divisible by 1, 2, and 5 so 10 is not a prime number.

Here you will find the algorithm and program to check whether a number is prime or not with explanation.

Prime Number Checking Algorithm

Here’s a simple algorithm to check whether a given positive integer is prime or not:

 

1.Start with the number n to be checked.

2.If n is less than 2, it is not prime. Return false.

3.If n is 2 or 3, it is prime. Return true.

4.If n is even (i.e., divisible by 2), it is not prime (except for 2 itself). Return false.

5.For all odd integers i from 3 to the square root of n (inclusive), do the following:

6.If n is divisible by i, it is not prime. Return false.

7.If none of the above conditions hold, n is prime. Return true.

Program to check whether the number is Prime or not

Output

Enter a positive integer : 11
it is a prime number.
Enter a positive integer : 24
it is not a prime number.

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number