In the previous article, we have discussed Java Program to Check Peterson Number

In this article we are going to understand what Perfect number is and how we can check whether a number is Perfect number or not in Java with examples.

## Program to Check Perfect Number

**Perfect numbers** are numbers that are equal to the sum of it s proper divisors i.e. sum of all divisors except the number itself.

Example : 6: 1+2+3= 6 Perfect number 10: 1+2+5=8 Not a Perfect number 12: 1+2+3+4+6=16 Not a Perfect number

In the above examples the numbers 10 and 12 are not Perfect numbers as their divisors don’t add up to the numbers. Hence 6 is the only Perfect number here.

Let’s see different ways to check Perfect number.

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

**Approach :**

- Enter/Declare a number and store it.
- We calculate all the divisors of the number and add them.
- If the resultant sum is the same as the entered number, then the number is said to be a Perfect number.

### Method-1: Java Program to Check Perfect Number By Using Static Value

import java.util.Scanner; public class PerfectNumber { public static void main(String args[]) { //A number declared; int num = 6; int sum = 0, iter; //Loop to find all the divisors of the number and add them for(iter = 1; iter<num; iter++) { if(num%iter==0) sum+=iter; } if(sum==num) { System.out.println(num+" is a Perfect Number"); } else { System.out.println(num+" is Not a Perfect Number"); } } }

Output: 6 is a Perfect Number

### Method-2: Java Program to Check Perfect Number By User Input Value

import java.util.Scanner; public class PerfectNumber { public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); int sum = 0, iter; //Loop to find all the divisors of the number and add them for(iter = 1; iter<num; iter++) { if(num%iter==0) sum+=iter; } if(sum==num) { System.out.println(num+" is a Perfect Number"); } else { System.out.println(num+" is Not a Perfect Number"); } } }

Output: Case-1 Enter a number : 6 6 is a Perfect Number Case-2 Enter a number : 14 14 is Not a Perfect Number

### By C Programming Language:

#include<stdio.h> int main() { // variables declared int i, num; printf("Enter a number: "); // taking user input scanf("%d", &num); //checking number is perfect square or not for(i = 0; i <= num; i++) { if(num == i*i) { printf("%d is a perfect square", num); return 0; } } printf("%d is not a perfect square", num); }

Output: Case-1 Enter a number: 85 85 is not a perfect square Case-2 Enter a number: 64 64 is a perfect square

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

**Related Java Programs:**