Task:Write a Java Program To Check a Number is Prime or Composite
The Source code for Java Program to input a number and check whether it is prime number or composite
/* Write a Java program to input a number and check it whether it is prime or composite number. */ package primenocheck; /** * @author EasyCodeBook.com */ import java.util.Scanner; public class PrimeNoCheck { public static void main(String[] args) { int number ; Scanner input = new Scanner(System.in); System.out.print("Enter a number:"); number = input.nextInt(); boolean isPrime = true; for(int i = 2; i <= number/2; i++) { // condition for nonprime number if(number % i == 0) { isPrime = false; break; } } if (isPrime==true) System.out.println(number + " is a Prime number."); else System.out.println(number + " is not a Prime number\n(it is a Composite number.)"); } }
Sample Run and Output of Java Program Check for Prime Number
Enter a number:12
12 is not a Prime number
(it is a Composite number.)