Perfect number program in Java

In this tutorial you will learn how to write a program in Java to check a given number is perfect or not.


Read This: What is Perfect Number? Write a program in C for Perfect Number.

Java program for perfect number is written below. This program will take a positive number as an input and after performing some operation it will print given number is perfect or not.

Suppose you want to check a number like is perfect or not then you have to give that number as an input to this below program at time of execution.

After the calculation it will give you appropriate output.

Below is a program to check a given number is perfect or not in Java

import java.util.*;  
class Main{
    public static void main(String ...args){
        int remainder, sum=0, i, originalNum;
        Scanner sc= new Scanner(;
        System.out.print("please enter a number: ");  
        originalNum = sc.nextInt();  
        for (i = 1; i <= originalNum/2; i++)
            remainder = originalNum % i;
	        if (remainder == 0)
                sum = sum + i;
        if (sum == originalNum)
            System.out.print("given no. is perfect number");
            System.out.print("given no. is not a perfect number");


java program to check given number is perfect or not

Leave a Comment