Write a java program to check whether a number is perfect or not. Given an input number, we have to write a java code which check whether an input number is perfect number or not.

Before solving this problem, let's first understand what is a perfect number.

A perfect number is a

For example -

Similarly,

Java Program to Convert Binary to Decimal Number

Java program to find second smallest number in an array

Java Programming Questions with Answers

We have discussed what is a perfect number? Let's write a java code which takes an input number and check whether an input number is a perfect number or not.

28 is a perfect number

Sorting algorithms and their time complexity

Before solving this problem, let's first understand what is a perfect number.

### What is Perfect Number?

A perfect number is a

**positive integer**that is equal to the sum of it's positive divisors excluding the number itself.For example -

**6 is a perfect number**. It's positive divisor is 1, 2 and 3 and sum of it's positive divisor is 6 (1 + 2 + 3).Similarly,

**28 is a perfect number**. Sum of the positive divisor is 28 (1 + 2 + 4 + 7 + 14).**10 is not a perfect number**. Sum of the positive divisor of 10 is 8 ( 1 + 2 + 5).Java Program to Convert Binary to Decimal Number

Java program to find second smallest number in an array

Java Programming Questions with Answers

## Java Program to Check Perfect Number or Not

We have discussed what is a perfect number? Let's write a java code which takes an input number and check whether an input number is a perfect number or not.

package perfectnumber; import java.util.*; public class PerfectNumber { public static void main(String[] args) { int num, sum = 0; System.out.println("Enter a number"); Scanner in = new Scanner(System.in); num = in.nextInt(); /* If number is greater than zero */ if (num > 0) { for (int i = 1; i < num; i++) { /* sum of it's factors */ 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 "); } } else { System.out.println (" Please enter positive number "); } } }

**Output :****Enter a number : 28**

28 is a perfect number

Sorting algorithms and their time complexity

## No comments:

## Post a Comment