Table of Contents
This program receives an input number from user and check if the number is prime number or not.The number is divided by 2, 3 and 5 to see if the remainder of the division is 0. If the remainder is zero, then the number is not prime otherwise, it is prime number.
Problem Definition
Prime numbers are very important topic in mathematics. It is usually easy to find the small prime numbers, but very difficult to identify a prime number, when the numbers grow larger. We want to write a program to test whether a given number is a prime number or not.
What is a Prime number?
Any positive number greater than 1 which is divisible only by itself and number one is called a Prime number, all other numbers are Composite numbers.
For example
\Large 63 = 3 \times 21
The number \Large 63 has two factors \Large 3 and \Large 21, therefore it is a composite number.
\Large 73 = 73 \times 1
The factors of number \Large 73 are \Large 73 itself and \Large 1, therefore, \Large 73 is a prime number.
How do we process the given input number?
Here are the steps to process the input number in the program.
Step 1 – Get the number, \Large N.
Step 2 – Check if the number, \Large N is divisible by \Large 2, 3, or \Large 5.
Step 3 – If the number, \Large N is divisible, then the \Large remainder = = 0.
Step 4 – If number is divisible, then \Large check = 0, else \Large check = 1.
Step 5 – If \Large check = = 1, number is a prime number
Step 6 – Print the output
Step 7 – End the program
Flowchart – Program to check a Prime number

Program Codes – Prime Number Check
/* Program to Check whether a Number is Prime or Not */
#include <stdio.h>
#include <conio.h>
int main()
{
int num, i, check = 1;
/* Read the number */
printf("Enter a number: ");
scanf("%d", &num);
/* Numbers less than or equal to 1 are not prime */
if (num <= 1)
check = 0;
/* Check divisibility */
for (i = 2; i <= num / 2; i++)
{
if (num % i == 0)
{
check = 0;
break;
}
}
/* Print the result */
for (i = 0; i < 35; i++)
printf("_");
printf("\n\n");
if (check == 1)
printf("The Number %d is a Prime Number\n\n", num);
else
printf("The Number %d is not a Prime Number\n\n", num);
for (i = 0; i < 35; i++)
printf("_");
printf("\n\n");
getch();
return 0;
}#include <iostream>
using namespace std;
int main()
{
int num, i;
bool isPrime = true;
cout << "Enter a number: ";
cin >> num;
if (num <= 1)
isPrime = false;
for (i = 2; i <= num / 2; i++)
{
if (num % i == 0)
{
isPrime = false;
break;
}
}
cout << "\n-----------------------------------\n";
if (isPrime)
cout << "The Number " << num << " is a Prime Number\n";
else
cout << "The Number " << num << " is not a Prime Number\n";
cout << "-----------------------------------\n";
return 0;
}import java.util.Scanner;
class PrimeNumber
{
public static void main(String[] args)
{
int num, i;
boolean isPrime = true;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
num = sc.nextInt();
if (num <= 1)
isPrime = false;
for (i = 2; i <= num / 2; i++)
{
if (num % i == 0)
{
isPrime = false;
break;
}
}
System.out.println("\n-----------------------------------");
if (isPrime)
System.out.println("The Number " + num + " is a Prime Number");
else
System.out.println("The Number " + num + " is not a Prime Number");
System.out.println("-----------------------------------");
sc.close();
}
}num = int(input("Enter a number: "))
is_prime = True
if num <= 1:
is_prime = False
for i in range(2, num // 2 + 1):
if num % i == 0:
is_prime = False
break
print("\n-----------------------------------")
if is_prime:
print(f"The Number {num} is a Prime Number")
else:
print(f"The Number {num} is not a Prime Number")
print("-----------------------------------")The for loop in the above source code continuously divides the given number to verify if remainder becomes a zero. If not then variable check is 1.
The program performs a second test in which if the value of variable check is \Large 0, it prints “Number is not a prime number” and if the value of variable check is \Large 1, prints “The Number is a prime number”.
Output
Enter a number:71
---------------------------------------
The Number 71 is a Prime Number
---------------------------------------