PhysicsTeacher.in

High School Physics

Perfect Number Java Program – check if a Number is a Perfect Number

This post presents a Java Program to check if a given Number is a Perfect Number.

A perfect number is a positive integer that is equal to the sum of its proper positive divisors excluding the number itself.

Perfect Number Java Program – code

import java.util.Scanner;
public class Perfect
{
public static void main(String[] args)
{


int n, sum = 0;
Scanner s = new Scanner(System.in);
System.out.print(“Enter any integer you want to check:”);
n = s.nextInt();


for(int i = 1; i < n; i++)
{
if(n % i == 0)
{
sum = sum + i;
}
}
if(sum == n)
{
System.out.println(“Given number is Perfect”);
}
else
{
System.out.println(“Given number is not Perfect”);
}
}


int divisor(int x)
{
return x;
}
}

See also  Java program with String and StringBuffer
Scroll to top
error: physicsTeacher.in