Question
Checking Whether the number Given is a prime number or not.
Prime number is a number that is greater than 1 and divided by 1 or itself only. In other words, prime numbers can't be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17.... are the prime numbers.
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
Java
Python
Java
import java.util.Scanner;
public class PrimeExample
{
public static void main(String args[])
{
int i,flag=0,num=0;
System.out.println("Enter an Integer number:");
/*The input provided by user is stored in num*/
Scanner input = new Scanner(System.in);
num = input.nextInt();
/*negative number and 1 are not prime number*/
if(num< 2)
{
System.out.println(num+" is not prime number");
}
else
{
for(i=2;i< num;i++)
{
/*Checking if number is divisible by i.*/
if(num%i==0)
{
System.out.println(num+" is not prime number");
flag=1;
break;
}
}
if(flag==0)
{
System.out.println(num+" is prime number");
}
}
}
}
Python
number=int(input("Enter a number:"))
isPrimeNumber=True
# Checking if the given number is less than 2
# if it is then it is not a prime number
if(number<2):
print(number,"is not a prime number")
else:
for i in range(2,number):
# Checking if the given number is divisible
# by any other number expect 1 and itself
# If it is then it's not a prime number
if(number%i==0):
isPrimeNumber=False
break
if(isPrimeNumber==True):
print(number,"is a prime number")
else:
print(number,"is not a prime number")