Perfect Number Program in Java Using While Loop Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report The number which is equal to the sum of its divisors is called a perfect number. Read the entered long number, assigned to the long variable n. While loop iterates until the condition (i<=n/2) is false. If the remainder of n/i=0 then add i value to the sum and increase the i value. After all the iterations compare the number with the sum, if both are equal then prints the number as a perfect number. Example: n = 6, i = 1 condition at while is 1 <= 3 is true, here 6%i = 0 is true so sum = 1, i = 2, 2 < 3 is true, 6%2 = 0 is true so sum = 1+2=3, i=3, 3<=3 is true, 6%3=0 is true so sum=3+3=6, for i=4 the while loop terminates and compares the sum value with n, both are equal, so it prints 6 is a perfect number. Implementation Java import java.util.Scanner; class Perfect { public static void main(String arg[]) { long n,sum=0; Scanner sc=new Scanner(System.in); System.out.println("Enter a number"); n=sc.nextLong(); int i=1; while(i<=n/2) { if(n%i==0) { sum+=i; } i++; } if(sum==n) { System.out.println(n+" is a perfect number"); } else System.out.println(n+" is not a perfect number"); } } Output: The time complexity is O(n), where n is the input number. The auxiliary space is O(1) Example : Java import java.io.*; public class PerfectNumber { public static void main(String[] args) { int number = 1; while (number <= 10000) { int sum = 0; // Find all the factors of the current number and add them up for (int i = 1; i < number; i++) { if (number % i == 0) { sum += i; } } // If the sum of the factors equals the number, it's a perfect number if (sum == number) { System.out.println(number + " is a perfect number"); } number++; } } } output : 6 is a perfect number 28 is a perfect number 496 is a perfect number 8128 is a perfect number Comment More infoAdvertise with us Next Article Perfect Number Program in Java Using While Loop S subramanyasmgm Follow Improve Article Tags : Java Programs Java Practice Tags : Java Similar Reads Java Program to Find Sum of Natural Numbers Using While Loop While loop arises into play where beforehand there is no conclusive evidence that how many times a loop is to be executed. This is the primary reason as there is no strict tight bound over how many numbers the sum is to be evaluated. Situations in which the output can be displayed using test conditi 3 min read Java Program to Compute the Sum of Numbers in a List Using While-Loop The task is to compute the sum of numbers in a list using while loop. The List interface provides a way to store the ordered collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion 2 min read Java Program to Reverse a Number and find the Sum of its Digits Using do-while Loop Problem Statement: The number is supposed to be entered by the user be it any random number lying within the primitive data-type holding the number. First, the number needs to be reversed. Secondary the sum of the number is to be calculated with the constraint to use a do-while loop. do-while loop: 6 min read How to Write Robust Programs with the Help of Loops in Java? Here we will discuss how we can write effective codes with the help of loops. It is a general perception that the approach using loops is treated as naive approach to solve a problem statement. But still, there is a huge scope of improvisation here. So basically, a loop statement allows us to execut 4 min read Java Program to Guess a Random Number in a Range Write a program that generates a random number and asks the user to guess what the number is. If the userâs guess is higher than the random number, the program should display Too high, try again. If the userâs guess is lower than the random number, the program should display Too low, try again. The 2 min read Basic Calculator Program Using Java Create a simple calculator which can perform basic arithmetic operations like addition, subtraction, multiplication, or division depending upon the user input. Example: Enter the numbers:Â 2Â 2 Enter the operator (+,-,*,/) + The final result: 2.0 + 2.0 = 4.0ApproachTake two numbers using the Scanner 2 min read Java Exercises - Basic to Advanced Java Practice Programs with Solutions Looking for Java exercises to test your Java skills, then explore our topic-wise Java practice exercises? Here you will get 25 plus practice problems that help to upscale your Java skills. As we know Java is one of the most popular languages because of its robust and secure nature. But, programmers 7 min read Java Program to Check Array Bounds while Inputting Elements into the Array Concept: Arrays are static data structures and do not grow automatically with an increasing number of elements. With arrays, it is important to specify the array size at the time of the declaration. In Java, when we try to access an array index beyond the range of the array it throws an ArrayIndexOu 4 min read Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its 8 min read How to Take Input From User Separated By Space in Java ? There are 2 methods to take input from the user which are separated by space which are as follows: Using BufferedReader Class and then splitting and parsing each valueUsing nextInt( ) method of Scanner class Let us discuss both the methods one by one in order to get a better understanding by impleme 3 min read Like