In: Computer Science
Please write a JAVA program which reads a number n from the user and check whether n is a perfect number or not. For example, when n = 7, the print out should be 7 is not a perfect number. If the input n is 6, then the program prints out 6 = 1 * 2 * 3
import java.util.Scanner; public class PerfectNumber { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a positive integer: "); int n = in.nextInt(); int result = 1; for (int i = 1; i < n; i++) { if (n % i == 0) { result *= i; } } if (result != n) { System.out.println(n + " is not a perfect number"); } else { System.out.print(n); for (int i = 1; i < n; i++) { if (n % i == 0) { if (i == 1) { System.out.print(" = 1"); } else { System.out.print(" * " + i); } } } System.out.println(); } } }