Defining methods Composite numbers

Write a method that tests the given number is composite or not. The composite number is a positive integer that has at least one divisor other than 1 and itself.

The method should return a value of the boolean type.

Sample Input 1:

1

Sample Output 1:

false

Sample Input 2:

2

Sample Output 2:

false

Sample Input 3:

4

Sample Output 3:

true
import java.util.Scanner;

public class Main {

    public static boolean isComposite(long number) {
        for (int i = 2; i < (int) Math.sqrt(number) + 1; i++) {
            if (number % i == 0) {
                return true;
            }
        }

        return false;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        final long number = scanner.nextLong();
        System.out.println(isComposite(number));
    }
}
posted @ 2020-08-12 08:16  longlong6296  阅读(99)  评论(0)    收藏  举报