In Java, to find all Perfect numbers in a given range we can use loop concepts such as while loop or for loop.
Perfect numbers are those numbers whose sum of factors excluding the number itself is equal to original number. For example, 6 is a Perfect number.
In this article, we will learn through examples, detailed logic and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Loop Concepts
What is Perfect Number?
A perfect number is defined as a positive integer that is equal to the sum of its proper divisors, excluding itself. For example, the number 6 is a perfect number because its proper divisors (1, 2, and 3) sum to 6 (1 + 2 + 3 = 6).
In the case of the number 2, its only proper divisor is 1, and 1 does not equal 2. Therefore, 2 is not a perfect number. The smallest perfect number is actually 6.
Problem Statement
We have to write a java program to print all Perfect numbers in a given range. Our program should take two integer numbers as a range, and should print all the Perfect numbers in that range. This action will be done in our program. Let’s see an example,
Example:
For input number 1 to 7, Output should be 6
Explanation:
Input Integer numbers as a range: 1 and 6
Factor of 1 : 1
Factor of 2 : 1 2, excluding the original number 2, factor will be 1
Factor of 3 : 1,3, excluding the original number 3, factor will be 1
Factor of 4 : 1, 2, 4, excluding the original number 4, factors will be
1, 2 and their sum is 3
Factor of 5 : 1,5 excluding the original number 5, factor will be 1
Factor of 6 : 1 2 and 3, Sum of factors = 1+2+3 =6.
Factor of 7 : 1,7, excluding the original number 7, factor will be 1
Hence, For a given range of numbers from 1 to 7, number 6 only the perfect number.
Logic to print all Perfect numbers in a given range
There are multiple logics to print all perfect numbers in a given range in Java programming language.
Logic 1: Using while loop
- Take firstNum and lastNum as a range from the user.
- It uses a while loop to iterate from firstNum and ending at lastNum.
- For each number firstNum in the range, it initializes sum to 0.
- It also initializes a variable fact (factor) to 1.
- Another while loop iterates from 1 to half of firstNum.
- Using an if statement, if firstNum is divisible by fact (firstNum%fact==0), fact is added to sum.
- After the inner loop, it checks if sum equals firstNum using an if statement.
- If true, it prints firstNum indicating it is a perfect number.
- The program increments firstNum and continues until it reaches lastNum.
- The scanner is closed after the loop completes.
Logic 2: Using for loop
- Take firstNum and lastNum as a range from the user.
- It uses a for loop with variable i starting from firstNum and ending at lastNum.
- Initialize a variable sum to 0.
- For each number i in the range, an inner for loop with variable fact checks divisibility from 1 to i-1.
- Inside for loop using an if statement if i is divisible by fact (i%fact==0), fact is added to sum.
- After completing inner loop, using an if statement checks if sum is equals to i. If true, it prints i, indicating i is a perfect number.
- After completing outer loop, close the scanner class.
Java program to print all Perfect numbers in a given range
Program 1: Using while loop
In the below programming we are using while loop to print all Perfect numbers in a given range in Java.
import java.util.Scanner;
public class PerfectNumRange {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter First Number");
int firstNum = s.nextInt();
System.out.println("Enter Second Number");
int lastNum = s.nextInt();
System.out.println("Perfect Numbers are: ");
while (firstNum <= lastNum) {
int fact = 1;
int sum = 0;
while (fact <=firstNum/2) {
if (firstNum % fact == 0) {
sum = sum + fact;
}
fact++;
}
if (sum == firstNum)
System.out.println(firstNum);
firstNum++;
}
s.close();
}
}
Output:
Enter First Number
1
Enter Second Number
50
Perfect Numbers are:
6
28
Java program explanation to print all Perfect numbers in a given range
- The program utilizes
Scanner
to capture user input for two integers,firstNum
andlastNum
, setting the range. - Executes a
while
loop to iterate through each number fromfirstNum
tolastNum
. - Initializes
fact
to 1 andsum
to 0 within the outer loop. - Uses an inner
while
loop to check divisibility offirstNum
byfact
and accumulate divisors insum
. - Checks if
sum
equalsfirstNum
to determine if it’s a perfect number, printing it if true. - Increments
firstNum
after each iteration and closes theScanner
to release resources when done.
Program 2: Using for loop
In the below programming we are using for loop to print all Perfect numbers in a given range.
import java.util.Scanner;
public class PerfectNumRange {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter First Number");
int firstNum= s.nextInt();
System.out.println("Enter Second Number");
int lastNum = s.nextInt();
System.out.println("Perfect Numbers are: ");
for(int i=firstNum;i<=lastNum;i++) {
int sum = 0;
for(int fact= 1;fact<i;fact++) {
if(i%fact==0) {
sum= sum+fact;
}
}
if(sum==i)
System.out.println(i);
}
s.close();
}
}
Output:
Enter First Number
256
Enter Second Number
2500
Perfect Numbers are:
496
Java program explanation to print all Perfect numbers in a given range
- The program uses a
Scanner
to take user input for two integers,firstNum
andlastNum
, defining a range. - It iterates through each number from
firstNum
tolastNum
. - For each number
i
, it calculates the sum of its proper divisors using a nestedfor
loop. - If the sum equals
i
, it printsi
as a perfect number. - After processing all numbers in the range, the
Scanner
is closed to free up resources.
Conclusion
In this article we have learnt how to print all perfect numbers in a given range in Java. We have seen the logic and implemented the program using if-else statements and a ternary operator.
For more information on related topics, you might find these articles helpful:
- Understanding the concept of Conditional statements in Java
- Understanding the concept of Operators
- Top 10 Programming Tips in Java
We hope you find this tutorial helpful. If you have any questions and need any clarifications, feel free to comment below. Don’t forget to subscribe our newsletter to get more updates on Java Programming tips and tutorials.
Happy Programming!!