×

Perfect Number in Java

The concept of a perfect number in Java will be defined in this chapter, along with creating Program code that determine whether a specific number is perfect or not. Additionally, a Java application will be written to find every perfect integer inside the specified range. Java coding exams and educational courses usually inquire about the perfect number application.

What is Perfect Number in Java

A perfect number in java is a number in which all the factors of that number are combined or added, then the result would be the number itself. Mathematically, the sum of all the positive factors of a number except the number itself must be equal to the number.

Let’s see how it works with a small example

Consider a small number 6

The factors of 6 include 1,2,3 and the number 6 itself.

Now let’s sum up the factors except the number

1+2+3=6

As we can observe the above example the sum of 1,2,3 would be mathematically be equal to 6 and they are also the factors of 6.

Thus, it can be said that the number 6 is the perfect number.

Similarly, there exit many perfect numbers like 28, 496, 8128 and many others.

Now let us understand the algorithm or a method of finding the perfect numbers in general

How to find the perfect number

Step 1: Begin or read a value (a).

Step 2: Set another variable (m) to store summation.

Step 3: Using the loops find out all the factors of the given value (a).

Step 4: Compute the sum and store the sum value in (m).

Step 5: Now compare both the values (a) and (m).

  1. If the values are equal, then (a) is said to be the perfect number.
  2. Else the value (a) is not regarded as perfect number.

Ways to find a perfect number in Java

The below are the following techniques for finding the perfect number in java. They include

  • By Method
  • By While Loop
  • By Recursion

By Method Technique

The below program depicts how to verify whether a number is perfect number or not by using the methods that perform the specified operation for verification.

File name: PerfNum.java

import java.util.Scanner; // Scanner helps in taking input dynamically 
public class PerfNum  //  name of the class
{  
static long perfect(long n)  // name of the method
{  
long s=0;  // Initially setting the sum as zero
for(int i=1; i <= n/2; i++)  
{  
if(n % i == 0)  
{  
s=s+ i;  // storing the sum of factors 
} 
}  
return s;   //returning the sum
} // End of the method
public static void main(String args[])    
{  
long num, m;  
Scanner sc=new Scanner(System.in);         
System.out.print("Enter the number: ");  
num=sc.nextLong();  // Takes a  number as input from the user
m = perfect(num); // calling the method  
if(m==num)  // comparing the m and num 
System.out.println(num+" is a perfect number");  
else  
System.out.println(num+" is not a perfect number");   
}   
}  

Output

Enter the number: 28
28 is a perfect number
Enter the number: 7
7 is not a perfect number

By While Loop Technique

The below program depicts how to verify whether a number is perfect number or not by using the while loop that helps in performing the specified operation for verification.

File name: PerfNum1.java

import java.util.Scanner;  // Enables in taking the input dynamically
public class PerfNum1  // name of the class
{  
public static void main(String args[])    
{  
long n, s=0;  
Scanner sc=new Scanner(System.in);         
System.out.print("Enter the number: ");  
n=sc.nextLong(); // Taking a number as input from user 
int i=1;  
while(i <= n/2)  // Begin of while loop
{  
if(n % i == 0)  
{  
s= s+ i;  
} 
i++; // Incrementing the value
} // End of While
if(s==n)  //Comparing the n and s
{  
System.out.println(n+" is a perfect number.");  
} 
else  
System.out.println(n+" is not a perfect number.");   
}  
}  

Output

Enter the number: 15
15 is not a perfect number.
Enter the number: 496
496 is a perfect number.

By using Recursion method

The below program depicts how to verify whether a number is perfect number or not by using the recursion technique that helps in performing the specified operation for verification.

File name: PerfNum2.java

import java.util.Scanner;  
public class PerfNum2 
{     
static long s=0;    
long perfect(long n, int i)  
{  
if(i <= n/2)  
{  
if(n% i ==0)  
{  
s=s + i;  
}  
i++;  
perfect(n, i);  
}  
return s;   
}  
public static void main(String args[])    
{  
long num, m;  
int i=1;  
Scanner sc=new Scanner(System.in);         
System.out.print("Enter the number: ");  
num=sc.nextLong();  
PerfNum2 p=new PerfNum2( );  
m = p.perfect(num, i);  
if(m == num)  
System.out.println(num+" is a perfect number");  
else  
System.out.println(num+" is not a perfect number");   
}   
}  

Output

Enter the number: 25
25 is not a perfect number
Enter the number: 6
6 is a perfect number

Related Topics

Radix Sort in Java

Radix Sort in Java Radix sort in Java uses the digits of the numbers given in the sorted array to sort the numbers. The radix sort uses the place value of...

5 minutes read.

How to check version of java in Linux

Java is one of the most famous and thoroughly utilized programming tongues from one side of the world to the other. On the off chance that you are a Java...

2 minutes read.

Generic queue in Java

Before understanding how to implement a generic queue in java, one must know about generics and queue in java. Generics in Java Generics are parameterized types. The goal is to enable type...

6 minutes read.

Java Editors

A straightforward text editor may be used to create Java applications. However, a Java integrated programming environment (IDE) enables the software developer to create programs more quickly. An IDE offers...

4 minutes read.

Java Math atan() Method

The atan() method of Math class computes the trigonometric Arc Tangent (inverse of tangent ) of an angle. The value returned is between -pi/2 to pi/2. Syntax: public static double atan(double a) Parameters: The...

2 minutes read.

Rotate matrix by 90 degrees in Java | Rotate matrix in Java clockwise and anti-clockwise

In this article, you will be acknowledged about what is a matrix along with an example. Most importantly you will be equipped knowledge on how to rotate the matrix by...

4 minutes read.

Java Database Connectivity with Oracle

JDBC: A Programmer can develop a complete application using the Java built-in API’s. So, for storing the data required for solving a real-world problem is stored into a database. To connect...

5 minutes read.

Java Wrapper classes

Java Wrapper classes A wrapper class is a class whose object contains a primitive data type; moreover it provides a way to use primitive data type (int, boolean, etc.) as objects. Wrapper...

2 minutes read.

Java Command Line Argument

Command-line arguments are passed to the main() method when we want to pass information into a program during runtime. It is the information that directly follows the program’s name on the...

1 minute read.

Java Socket Programming

Java Socket Programming Socket programming is used for the communication between the applications, i.e., client and server running on different JRE may be connection-oriented or connectionless. The client program can be designed using the...

8 minutes read.

Java Math toDegrees() Method

The toDegrees() method of Java Math class converts a radian angle to an approximately equivalent angle measured in degrees. Syntax: public static double toDegrees(double angrad) Parameters: The parameter ‘angrad ‘represents an angle measured in...

2 minutes read.

Deadlock in Java

Deadlock is when two or more processes wait for the state to do their tasks, but none of them can do so. It is a very common problem that one...

6 minutes read.

Heap Sort in Java

Heap Sort in JavaHeap sort in Java uses the data structure binary heap, min-heap, or max heap to do the sorting of elements. Since min-heap always gives the minimum element first,...

8 minutes read.

Java 9 Tutorial

The Java 9 is most popular release of java programming to make it platform modular. It is used to improve JDK and java implementation security. It provides JAVA SE and...

3 minutes read.

Insertion Sort in Java

Insertion Sort in Java Insertion sort in Java is a simple sorting algorithm that works in the same way as we hold cards in hand. Insertion sort does the sorting element-by-element,...

3 minutes read.

Java float vs double

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Brilliant Number in Java

It is a number N that is made up of two prime numbers that have the same number of digits and is called a brilliant number. Several/Some of the brilliant Numbers...

3 minutes read.

How to stop execution after a certain time in Java

We will learn how to stop a long-running execution after a set amount of time in this post. We will take a look at a few alternative approaches to this...

6 minutes read.

How to Create Different Packages for Different Classes in Java

Packages in Java In Java, Packages are an assortment of classes, sub-packages, and connection points. i.e. A package addresses a word reference that contains a connected gathering of styles and points...

7 minutes read.

Upcasting in Java

To know what is Upcasting in Java we should first equip ourselves about what is casting or type casting in Java. Casting The process of providing or replacing a reference variable of...

3 minutes read.