0% found this document useful (0 votes)
3 views

Java2

The document contains a Java program that checks whether a given number is a perfect number. It prompts the user to enter a number, calculates the sum of its divisors, and determines if the sum equals the original number. The program provides output indicating whether the entered number is a perfect number or not.

Uploaded by

121pratham2032
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Java2

The document contains a Java program that checks whether a given number is a perfect number. It prompts the user to enter a number, calculates the sum of its divisors, and determines if the sum equals the original number. The program provides output indicating whether the entered number is a perfect number or not.

Uploaded by

121pratham2032
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

2.

Write a java program to check given number is perfect number or not

import java.u l.Scanner;

public class PerfectNumber {

public sta c void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");

int number = scanner.nextInt();

if (isPerfectNumber(number)) {

System.out.println(number + " is a perfect number.");

} else {

System.out.println(number + " is not a perfect number.");

scanner.close();

public sta c boolean isPerfectNumber(int number) {

if (number <= 1) {

return false;

int sum = 1;

for (int i = 2; i <= Math.sqrt(number); i++) {

if (number % i == 0) {

sum += i;

if (i != number / i) {

sum += number / i;

return sum == number;


}

O/P: Enter a number: 28

28 is a perfect number.

Enter a number: 12

12 is not a perfect number.

You might also like