Java program to check if a number is a composite number or not

Java program to check if a number is a composite number or not:

In this post, we will write one Java program that will check if a number is a composite number or not. This program will take one number as input from the user, check if it is a composite number and print one message to the user.

Composite number:

A number is called a composite number if it has a positive divisor other than 1 and the number itself. For example, 9 is a composite number. We can also say that if a number is not a prime number, it will be a composite number.

Method 1: Java program to check if a number is a composite number or not:

Let’s write down the java program to check if a number is a composite number or not:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        int number;
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a number: ");
        number = sc.nextInt();

        boolean isComposite = false;

        for (int i = 2; i < number; i++) {
            if (number % i == 0) {
                isComposite = true;
                break;
            }
        }

        if (isComposite) {
            System.out.println(number + " is a composite number");
        } else {
            System.out.println(number + " is not a composite number");
        }
    }
}

Here,

  • The program asks the user to enter one number, it assigns that number to the number variable.
  • The scanner variable is used to read the user input values.
  • The boolean variable isComposite is used to define if the number is a composite number or not. It is initialized as false.
  • The for loop runs from 2 to number - 1. On each iteration, it checks if the current value of i can divide the number or not. If yes, it assigns true to the isComposite variable and exits from the loop.
  • Based on the value of isComposite, it prints one message.

Java example to check if composite number

Method 2: Java program to check for a composite number by using a separate function:

We can also use a separate function to check if a number is a composite number or not. The main method will call the isComposite method to check if a number is a composite number or not. This method returns one boolean value and based on its value, it prints one message.

import java.util.Scanner;

public class Main {
    private static boolean isComposite(int number) {
        for (int i = 2; i < number; i++) {
            if (number % i == 0) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int number;
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter a number: ");
        number = sc.nextInt();

        if (isComposite(number)) {
            System.out.println(number + " is a composite number");
        } else {
            System.out.println(number + " is not a composite number");
        }
    }
}

It will give a similar result.

Example 3: Find all composite numbers in a range:

Let’s use a loop and find all composite numbers in a given range:

import java.util.Scanner;

public class Main {
    private static boolean isComposite(int number) {
        for (int i = 2; i < number; i++) {
            if (number % i == 0) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int lowerLimit, upperLimit;
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the lower limit: ");
        lowerLimit = sc.nextInt();

        System.out.println("Enter the upper limit: ");
        upperLimit = sc.nextInt();

        System.out.println("Composite numbers between " + lowerLimit + " and " + upperLimit + " are:");
        for (int i = lowerLimit; i <= upperLimit; i++) {
            if (isComposite(i)) {
                System.out.println(i);
            }
        }
    }
}

Instead of the number, this program will read the lower and upper limits to find all composite numbers. The for loop iterates between these limits and uses the isComposite method to find all the composite numbers in that range.

Sample output:

Enter the lower limit: 
10
Enter the upper limit: 
20
Composite numbers between 10 and 20 are:
10
12
14
15
16
18
20

You might also like: