A perfect number is one where the sum of factors of a number (excluding the number itself) is equal to the number itself. EXAMPLE- We take 6. Factors of 6 are: 1,2,3 6 = 1 + 2 + 3 6 is a perfect number. Now we take 28 .Factors of 28 are: 1,2,4,7,14 28 = 1 + 2 + 4 + 7 + 14 28 is a perfect number. Perfect number in java language can be made by first applying a for loop starting from 1 to 1 less than that number and then with the help of if statement, checking if the number is divisible by that particular number which is currently stored in loop control variable. Then after the loop terminates we will check whether the sum of factors of the number is equal to the number itself or not. If the statement returns true then appropriate statement prints. Here is the code for perfect number in two different formats firstly with the help of methods and secondly a very simple program using main method.
Basic java programs with source code, java programs for beginners, tutorials, exam tips, tips and tricks