Composite Check
Check whether a given number is composite.
IntermediateTopic: Module 3: Loop Programs
Java Composite Check Program
This program helps you to learn the fundamental structure and syntax of Java programming.
import java.util.Scanner;
public class Main {
private static boolean isComposite(int n) {
if (n <= 3) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return true;
}
return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
if (isComposite(n)) {
System.out.println(n + " is Composite");
} else {
System.out.println(n + " is not Composite");
}
sc.close();
}
}Output
Enter a number: 12 12 is Composite
Understanding Composite Check
Composite numbers have at least one divisor other than 1 and themselves; we search for such a divisor.
Note: To write and run Java programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Java Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Java programs.