Java Basic
Java Interview Programs
- Java Interview Programs
- Java Abundant Number
- Java Amicable Number
- Java Armstrong Number
- Java Average of N Numbers
- Java Automorphic Number
- Java Biggest of three numbers
- Java Binary to Decimal
- Java Common Divisors
- Java Composite Number
- Java Condense a Number
- Java Cube Number
- Java Decimal to Binary
- Java Decimal to Octal
- Java Disarium Number
- Java Even Number
- Java Evil Number
- Java Factorial of a Number
- Java Fibonacci Series
- Java GCD
- Java Happy Number
- Java Harshad Number
- Java LCM
- Java Leap Year
- Java Magic Number
- Java Matrix Addition
- Java Matrix Division
- Java Matrix Multiplication
- Java Matrix Subtraction
- JS Matrix Transpose
- Java Maximum Value of an Array
- Java Minimum Value of an Array
- Java Multiplication Table
- Java Natural Number
- Java Number Combination
- Java Odd Number
- Java Palindrome Number
- Java Pascalβs Triangle
- Java Perfect Number
- Java Perfect Square
- Java Power of 2
- Java Power of 3
- Java Pronic Number
- Java Prime Factor
- Java Prime Number
- Java Smith Number
- Java Strong Number
- Java Sum of Array
- Java Sum of Digits
- Java Swap Two Numbers
- Java Triangular Number
Java Program to find Composite Number
Photo Credit to CodeToFun
π Introduction
In the realm of programming, identifying and working with different types of numbers is a fundamental skill.
A composite number is one that has more than two positive divisors, excluding 1 and itself. In this tutorial, we'll explore a java program that efficiently determines whether a given number is composite or not.
π Example
Let's dive into the java code that checks whether a number is composite or not.
import java.util.Scanner;
public class CompositeNumberChecker {
// Function to check if a number is composite
static boolean isComposite(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return true; // Found a divisor other than 1 and itself
}
}
return false; // No divisors other than 1 and itself
}
// Driver program
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Default input value is 12
int number = 12;
// Call the function to check if the number is composite
boolean result = isComposite(number);
// Display the result
if (result) {
System.out.println(number + " is a composite number.");
} else {
System.out.println(number + " is not a composite number.");
}
scanner.close();
}
}
π» Testing the Program
To test the program with different numbers, modify the value of the number variable in the main method.
12 is a composite number.
Compile and run the program to see whether the input number is a composite number.
π§ How the Program Works
- The program defines a class CompositeNumberChecker containing a static method isComposite that takes a number as input and returns true if the number is composite and false otherwise.
- Inside the method, it iterates through numbers from 2 to the square root of the input number.
- For each iteration, it checks if the current number is a divisor of the input number.
- If it finds a divisor, the number is composite, and the method returns true.
- The main method tests the program with a default input value of 12 and displays whether it is a composite number or not.
π Between the Given Range
Let's take a look at the Java code that checks for composite numbers in the specified range.
public class CompositeNumberChecker {
// Function to check if a number is composite
static boolean isComposite(int num) {
if (num <= 1) {
return false; // 1 is neither prime nor composite
}
for (int i = 2; i < num; i++) {
if (num % i == 0) {
return true; // Found a divisor other than 1 and itself
}
}
return false; // No divisors other than 1 and itself
}
// Driver program
public static void main(String[] args) {
// Check for composite numbers in the range from 1 to 10
System.out.println("Composite numbers in the range 1 to 10:");
for (int i = 1; i <= 10; i++) {
if (isComposite(i)) {
System.out.print(i + " ");
}
}
}
}
π» Testing the Program
Composite numbers in the range 1 to 10 are: 4 6 8 9 10
Run the program to check for composite numbers in the specified range.
π§ How the Program Works
- The program defines a class CompositeNumberChecker containing a static method isComposite that checks if a given number is composite.
- The main method iterates through the numbers in the range from 1 to 10 and prints the composite numbers.
π§ Understanding the Concept of Composite Numbers
Composite numbers are integers greater than 1 that have divisors other than 1 and themselves.
For example, the number 12 is composite because it can be formed by multiplying 2 and 6 or 3 and 4.
π’ Optimizing the Program
While the provided program is effective, consider exploring and implementing optimizations for larger numbers, such as checking divisors up to the square root of the number for improved efficiency.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (Java Program to find Composite Number), please comment here. I will help you immediately.