In this tutorial , We will write perfect number program in java using for loop , while loop and recursion . Firstly we will learn what is a perfect number in java and how to find perfect number with examples.
What is a Perfect Number ?
In number theory, a perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself . The sum of divisors of a number, excluding the number itself, is called its aliquot sum, so a perfect number is one that is equal to its aliquot sum. Equivalently, a perfect number is a number that is half the sum of all of its positive divisors including itself; in symbols, σ1(n) = 2n where σ1 is the sum-of-divisors function. For Example , 28 is perfect number as 1 + 2 + 4 + 7 + 14 + 28 = 56 = 2 × 28. Let’s discuss perfect number example here
Example 1: Check 6 is a perfect number or not.
Positive factors are : 1,2,3 and 6
Here, the sum of all the factors of 6 excluding the number itself is equal to (1+2+3=6)6. Or Here, the sum of all the factors including the number itself is equal to 12. So 6 is perfect number here .
Example 2 : is 28 a perfect number ?
Positive factors of 28 are : 1,2,4,7,14 and 28
Here, the sum of all the factors of 28 excluding the number itself is equal to (1+2+4+7+14=28)28 . So 28 is perfect number here .
Example 3 : is 496 a perfect number ?
Positive factors of 496 are : 1, 2, 4, 8, 16, 31, 62, 124, 248 and 496
Here Again , the sum of all the factors of 496 excluding the number itself is equal to (1+2+4+8+16+31+62+124+248=496)496 . So 496 is a perfect number .
Perfect Number Program In Java
We have discussed different perfect number example in this article till , Now we will write perfect number program in java using for loop , while loop and recursion .
Perfect Number program Using for loop
In this java program we check a given number is perfect or not
Steps to find perfect Number
- Take num as input from user to check perfect or not
- initialize sum variable with zero.
- Now find factor of num with for loop and add to sum all factors .We will iterate loop till equal n\2 here .
- if sum is equal to given num then it is perfect
Let’s see java program implementation for this approach
import java.util.Scanner;
public class PerfectNumber1 {
public static void main(String[] args) {
int num, sum = 0;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer:");
num = scanner.nextInt();
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sum = sum + i;
}
}
if (sum == num) {
System.out.println(num + " is a perfect number");
} else {
System.out.println(num + " is not a perfect number");
}
}
}
Output :
Enter an integer:50
50 is not a perfect number
and
Enter an integer:28
28 is a perfect number
Find Perfect Number In java Using while loop
Now we will write java program to check given number is perfect number or not using while loop . We will iterate while loop till equal num\2 and calculate sum of all factors and store in sum variable . Then check sum is equal to number or not as below .
import java.util.Scanner;
public class PerfectNumber2 {
public static void main(String[] args) {
int num, sum = 0;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer:");
num = scanner.nextInt();
int i =1 ;
while (i <= num /2) {
if (num % i == 0) {
sum = sum + i;
}
i++;
}
if (sum == num) {
System.out.println(num + " is a perfect number");
} else {
System.out.println(num + " is not a perfect number");
}
}
}
Output :
Enter an integer:28
28 is a perfect number
and
Enter an integer:90
90 is not a perfect number
Find Perfect Number In java Using Recursion
We will write a method which is return sum of all factors of a given number in this program . Then to find perfect number we will check sum of all factor is equal to number .
import java.util.Scanner;
public class PerfectNumber3 {
static int sum=0;
public static int getSumOfFactorsOfNum(int num, int i)
{
//executes until the condition becomes false
if(i <= num/2)
{
if(num % i ==0)
{
//calculates the sum of factors
sum=sum + i;
}
//after each iteration, increments the value of variable i by 1
i++;
//recursively called function
getSumOfFactorsOfNum(num, i);
}
//returns the sum of factors of number
return sum;
}
public static void main(String[] args) {
int num;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer:");
num = scanner.nextInt();
int sum = getSumOfFactorsOfNum(num, 1) ;
if (sum == num) {
System.out.println(num + " is a perfect number");
} else {
System.out.println(num + " is not a perfect number");
}
}
}
Output :
Enter an integer:496
496 is a perfect number
And
Enter an integer:8128
8128 is a perfect number
In this tutorial we have learn perfect number program in java using for loop , while loop and recursion . We have also learn about what is a perfect number , how to find perfect number and different perfect number example in java .
Reference :