Perfect Number In Java Program
Perfect Number In Java Program
- What is Perfect Number?
- Find Single Perfect Number In Java Program
- Find Perfect numbers from 1 to 100 and 1 to n
What is Perfect Number?
Perfect number, a positive integer that is equal to the sum of its proper divisors. The smallest perfect number is 6, which is the sum of 1, 2, and 3. Other perfect numbers are 28, 496, and 8,128.
Perfect numbers are positive integers such that
where is the restricted divisor function (i.e., the sum of proper divisors of ), or equivalently
where is the divisor function (i.e., the sum of divisors of including itself). For example, the first few perfect numbers are 6, 28, 496, 8128, ...
6 = 1+2+3,
28 = 1+2+4+7+14,
496 = 1+2+4+8+16+31+62+124+248,
etc.
Find Single Perfect Number In Java Program
package hpkingdom.com; public class PerfectNumber { public static void main(String[] args) { int num = 28; int sum = 0; for (int i=2; i<num; i++) { if (num%i==0) { sum =sum+i; } } if (sum==num) { System.out.println(num+" : is a Perfect Number"); } else { System.out.println(num+" : is a Not Perfect Number"); } } }
Output:
28 : is a Perfect NumberFind Perfect numbers from 1 to 100 and 1 to n
package hpkingdom.com; public class PerfectNumber { public static void main(String[] args) { for (int num=1; num<29; num++){ int sum = 0; for (int i=1; i<num; i++) { if (num%i==0) { sum =sum+i; } } if (sum==num) { System.out.println(num+" : is a Perfect Number"); } else { System.out.println(num+" : is a Not Perfect Number"); } } } }Output: 1 : is a Not Perfect Number 2 : is a Not Perfect Number 3 : is a Not Perfect Number 4 : is a Not Perfect Number 5 : is a Not Perfect Number 6 : is a Perfect Number 7 : is a Not Perfect Number 8 : is a Not Perfect Number 9 : is a Not Perfect Number 10 : is a Not Perfect Number 11 : is a Not Perfect Number 12 : is a Not Perfect Number 13 : is a Not Perfect Number 14 : is a Not Perfect Number 15 : is a Not Perfect Number 16 : is a Not Perfect Number 17 : is a Not Perfect Number 18 : is a Not Perfect Number 19 : is a Not Perfect Number 20 : is a Not Perfect Number 21 : is a Not Perfect Number 22 : is a Not Perfect Number 23 : is a Not Perfect Number 24 : is a Not Perfect Number 25 : is a Not Perfect Number 26 : is a Not Perfect Number 27 : is a Not Perfect Number 28 : is a Perfect Number