Program to check Perfect Number in Java | AskTheCode

Java program to check Perfect Number using isPerfectNumber method | Java Programming Solution


An integer is said to be Perfect Number if the sum of its factors including 1 (but not the number itself ) is equal to the number itself.

For example, 6 is a perfect number because 6 = 1 + 2 + 3.

Write a method isPerfectNumber using the following signature that determines if parameter number is perfect number. Use this method in a main method that determine and prints all the perfect numbers between 1 and 1000.

public boolean isPerfectNumber (int number)






import java.lang.*;

class PerfectNumber{
	public static void main (String[] args){
		PerfectNumber pn = new PerfectNumber();
		for (int i = 1; i <= 1000; i++) {
			boolean result = pn.isPerfectNumber(i);
			if (result == true)

	public boolean isPerfectNumber(int number){
		int sum = 0;
		for (int i = 1; i < number; i++) {
			if (number % i == 0)
				sum = sum + i;

		if (sum == number)
        	return true;
    	return false;
22 views0 comments

Recent Posts

See All

Golf CodeChef Solution in Java - AskTheCode

CodeChef May Long Challenge Solution | Golf (LKDNGOLF) solution | AskTheCode Golf (LKDNGOLF) solution Problem: It's a lockdown. You’re bored in your house and are playing golf in the hallway. The hall

Solubility - CodeChef Solution in Java and C++| AskTheCode

CodeChef May Long Challenge Solution | Solubility (SOLBLTY) solution | AskTheCode Solubility (SOLBLTY) solution Problem: Suppose for a unit rise in temperature, the solubility of sugar in water increa

Valid Paths CodeChef Solution in C++ | AskTheCode

CodeChef May Long Challenge Solution | Valid Paths (VPATH) solution | AskTheCode Valid Paths (VPATH) CodeChef solution Problem: You are given a tree with N nodes numbered from 1 to N. A set S of nodes