Java - Perfect Number or not
Problem Statement
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
- 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.
num | i<=num/2 | num%i==0 | sum=sum+i; | sum | i++ OR i=i+1 | i | Comment |
---|---|---|---|---|---|---|---|
6 | ----- | ----- | ----- | 0 | ----- | 1 | Initial Values |
6 | 1<=6/2 ↓ 1<=3 ↓ true | 6%1==0 ↓ 0==0 ↓ true | sum=0+1; | 1 | i=1+1 | 2 | Iteration 1 |
6 | 2<=6/2 ↓ 2<=3 ↓ true | 6%2==0 ↓ 0==0 ↓ true | sum=1+2; | 3 | i=2+1 | 3 | Iteration 2 |
6 | 3<=6/2 ↓ 3<=3 ↓ true | 6%3==0 ↓ 0==0 ↓ true | sum=3+3; | 6 | i=3+1 | 4 | Iteration 3 |
6 | 4<=6/2 ↓ 4<=3 ↓ false | ----- | ----- | ----- | ----- | ----- | For loop stops working as condition is evaluated to false |
- 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.