ComputerShikshak.com

Java - Perfect Number or not

Problem Statement

Write a program in Java to check whether an integer number entered by the user is a perfect number or not.

Brief Description

According to the problem statement we have to write a program in Java that will take an integer value as input from the user through the keyboard and will check whether it is a perfect number or not.

A positive number is perfect if it is equal to the number obtained by adding its proper positive divisors excluding the number itself.

For example, if the user enters 6 as input, the program will generate an output indicating that 6 is a perfect number since the number 6 is divisible by 1, 2 and 3 (excluding 6) and 1+2+3=6. But if the user enters 8 as input, the program will generate an output indicating that 8 is not a perfect number since the number 8 is divisible by 1, 2 and 4 (excluding 8) and 1+2+4=7.

Program

				
					import java.util.*;
class Perfect
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int num,sum=0;
        System.out.print("Enter a number: ");
        num=sc.nextInt();
        for(int i=1;i<=num/2;i++)
        {
            if(num%i==0)
                sum=sum+i;    //OR sum+=i;
        }
        if(num==sum)
            System.out.print(num+" is a perfect number");
        else
            System.out.print(num+" is not a perfect number");
    }
}
				
			

Output 1

Enter a number: 6
6 is a perfect number

Output 2

Enter a number: 8
8 is not a perfect number

Explanation

In this program:
  • 3 integer variable namely num, i and sum are declared.
  • Initially 0 is stored in sum.
  • The integer value entered by the user is stored in num.
The step-by-step working of the for loop (when num contains 6) is shown in the table given below:
numi<=num/2num%i==0sum=sum+i;sumi++
OR
i=i+1
iComment
6---------------0-----1Initial Values
61<=6/2
↓
1<=3
↓
true
6%1==0
↓
0==0
↓
true
sum=0+1;1i=1+12Iteration 1
62<=6/2
↓
2<=3
↓
true
6%2==0
↓
0==0
↓
true
sum=1+2;3i=2+13Iteration 2
63<=6/2
↓
3<=3
↓
true
6%3==0
↓
0==0
↓
true
sum=3+3;6i=3+14Iteration 3
64<=6/2
↓
4<=3
↓
false
-------------------------For loop
stops
working
as
condition
is
evaluated
to
false
While executing the for loop:
  • At first 1 is stored in i.
  • The condition i<=num/2 is evaluated.
  • The condition i<=num/2 will produce either true or false after evaluation.
    • If it is true, the statement written within for loop is executed and the loop continues to iterate till the condition is true.
    • If it is false then the loop stops working and the control goes to the statement written immediately after the loop.
  • At the end of each iteration the value of i is incremented by 1 by executing the statement i++.
  • Within for loop it is checked to see whether the value stored in i is a proper positive divisor of the value stored in num or not.
    • If it is then the value stored in i is added with the value stored in sum and the result is stored in the same variable i.e. sum.
    • If it is not then the statement written within if statement is not executed.

Thus the above process is used to add the proper positive divisors of the value stored in num excluding the number itself.

The final result is stored in sum.

Finally it is checked to see whether the value stored in num is equal to the value stored in sum. If it is then we can conclude that the value stored in num is a perfect number otherwise not and an appropriate message is displayed on the output screen.

Share this page on
Scroll to Top