Make Selenium Easy

Frequently Asked Java Program 28: Java Program to Find if Given Number Is a Perfect Number

Perfect Number:

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

For Example: Take Number 6. Its divisors are 1, 2 , 3 and 6. When we add all divisors except 6 ( as it is number itself) (i.e. 1+2+3= 6), it is equal to actual number. So we can say Number 6 is a perfect number.

The smallest perfect number is 6. Other perfect numbers are 28, 496, and 8,128.

Logic:

  1. When we divide a number say X by other number say Y and reminder is zero, then number Y is a proper divisor. All proper divisor of a number X are between 1 and X/2. ( 1 and X/2 are included) . To find the proper divisor we need to divide the number by 1 to number/2.
  2. We need to keep adding all divisors.

Java Program:

Output:

Please enter the number to check for perfect number : 
6
You entered: 6
6 is a perfect number.
=====================================================
Please enter the number to check for perfect number : 
1
You entered: 1
1 is not a perfect number.
====================================================
Please enter the number to check for perfect number : 
-10
You entered: -10
A perfect number must be positive and greater than zero. So kindly enter number greater than zero.
======================================================
Please enter the number to check for perfect number : 
10
You entered: 10
10 is not a perfect number.

#HappyCoding

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

Frequently Asked Java Program 28: Java Program to Find if Given Number Is a Perfect Number

by Amod Mahajan time to read: 1 min
0
%d bloggers like this: