A number is prime if it is divisible by only two numbers 1 and the number itself.
Ex:- 11 is prime ( bcz it is divisible by 1 and 11 only) ✔
12 is not prime ( bcz it is divisible by 1, 2, 3, 4, 6
and 12 ) ✖
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import java.util.Scanner; public class Prime{ public static void main(String []args){ Scanner sc = new Scanner(System.in); int num = sc.nextInt(); System.out.print(checkPrime(num)); } public static boolean checkPrime(int num){ for(int i=2; i<=num/2; i++){ if(num%i==0) return false; } return true; } } |
Python
def checkPrime(num): for i in range(2, num//2+1): if num%i==0: return "Not prime" return "prime" num = int(input("Enter a number:")) print(checkPrime(num))