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
#include <stdio.h>
int main() {
int n, i, flag = 0;
printf(“Enter a positive integer: “);
scanf(“%d”, &n);
for (i = 2; i <= n / 2; ++i) {
// condition for non-prime
if (n % i == 0) {
flag = 1;
break;
}
}
if (n == 1) {
printf(“1 is neither prime nor composite.”);
}
else {
if (flag == 0)
printf(“This is a prime number.”);
else
printf(“This is not a prime number.”);
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int n, i;
bool isPrime = true;
cout << “Enter a positive integer: “;
cin >> n;
for(i = 2; i <= n / 2; ++i)
{
if(n % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
cout << “This is a prime number”;
else
cout << “This is not a prime number”;
return 0;
}
import java.util.Scanner;
class PrimeCheck
{
public static void main(String args[])
{
int temp;
boolean isPrime=true;
Scanner scan= new Scanner(System.in);
System.out.println(“Enter a positive number:”);
//capture the input in an integer
int num=scan.nextInt();
scan.close();
for(int i=2;i<=num/2;i++)
{
temp=num%i;
if(temp==0)
{
isPrime=false;
break;
}
}
//If isPrime is true then the number is prime else not
if(isPrime)
System.out.println(num + ” is a Prime Number”);
else
System.out.println(num + ” is not a Prime Number”);
}
}
# To take input from the user
num = int(input(“Enter a positive integer: “))
# prime numbers are greater than 1
if num > 1:
# check for factors
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num,”is a prime number”)
# if input number is less than
# or equal to 1, it is not prime
else:
print(num,”is not a prime number”)
function primeCheck($number){
if ($number == 1)
return 0;
for ($i = 2; $i <= $number/2; $i++){
if ($number % $i == 0)
return 0;
}
return 1;
}
// Driver Code
$number = 13;
$flag = primeCheck($number);
if ($flag == 1)
echo “13 is a prime number.”;
else
echo “13 is Not a prime number.”
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