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.
What is Perfect Number?
A perfect number is a positive integer that is equal to the sum of its proper divisors, excluding itself. Proper divisors are the positive divisors of a number other than the number itself. In other words, a perfect number is a number whose divisors (excluding itself) add up to the number itself.
Here’s an example to illustrate a perfect number:
Example:
28 is Perfect number
- The divisors of 28 are 1, 2, 4, 7, 14.
- Excluding the number itself (28), the sum of its divisors is 1 + 2 + 4 + 7 + 14 = 28.
- Therefore, 28 is a perfect number.
How this Java program will behave?
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.
Program 1: Perfect number program in Java
import java.util.*;
class Main{
public static void main(String ...args){
int remainder, sum=0, i, originalNum;
Scanner sc= new Scanner(System.in);
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");
else
System.out.print("Given no. is not a perfect number");
}
}
Output:
Please enter a number: 28
Given no. is perfect number
Program 2: Perfect Number Program Using Java 8
import java.util.*;
import java.util.stream.IntStream;
class Main{
public static void main(String ...args){
int number = 28;
if (number <= 1) {
System.out.print("Given no. is perfect number");
}
int sum = IntStream.range(1, number)
.filter(i -> number % i == 0)
.sum();
if(sum == number)
System.out.print("Given no. is perfect number");
else
System.out.print("Given no. is not a perfect number");
}
}
Output
Given no. is perfect number
Conclusion
In this tutorial we have learnt the writing java Program to check the perfect numbers. We have seen both traditional java program and using java 8 stream api to check given number is perfect or not. A perfect number is defined as a positive integer whose proper divisors (excluding itself) sum up to the number itself.
The first program utilized traditional looping to calculate the sum of divisors for a user-input number. After finding the sum, compare with original number. If both are equal then printed “Given number is perfect number”, otherwise no.
In the second program we have using Java 8 approach. With the help of Stream API, we have generated a range of divisors and calculating their sum in a more functional style.
Both programs demonstrated successful execution with the example number 28 as 28 is perfect number. The use of traditional loops and the more modern Stream API showcased different programming styles, providing flexibility in choosing the approach that aligns with the developer’s preferences or project requirements.
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…