How to Check Perfect Number in Java

Posted By: Java Examples - 5:43 PM


& Comment

A perfect number is a positive integer where the sum of all its positive divisors, except itself, is equal to the number itself. 
For example 6 is a perfect number as 1,2 and 3 are its divisors and the sum of divisors=1+2+3=6. Here we have created a program that will take the number from the user and reports whether it is perfect or not.
Java version of perfect is given bellow :
package number;

import java.util.Scanner;

 * @author ACHCHUTHAN
public class perfect {

    private int number;

    public perfect() {
        Scanner input = new Scanner(;
        System.out.println("Enter positive integer");
        number = input.nextInt();

    public perfect(int number) {
        this.number = number;

    public boolean isPerfect() {
        int pefect = 0;
        for (int i = 1; i < number; i++) {
            if (number % i == 0) {
                pefect += i;
        if (number == pefect) {
            return true;
        } else {
            return false;

    public void print() {
        if (isPerfect()) {
            System.out.println("Number is Perfect");
        } else {
            System.out.println("Number is not Perfect");

    public static void main(String args[]) {
        perfect pn = new perfect();
        perfect pn1 = new perfect(6);

Download of this program :

Enter positive integer
Number is not Perfect
Number is Perfect
BUILD SUCCESSFUL (total time: 6 seconds)

About Java Examples

I’m passionate about Web Development and Programming and I go to extreme efforts to meet my passion. I’m a believer of learning the fundamentals first. I try to understand everything little bit more than the average.

Copyright © 2016 Java Examples ACHCHUTHAN.ORG. Designed by Templateism .