Perfect number
Perfect number is sum of the Divisors equal to the given number.
Example-1:
Input : n = 6 Output : Perfect number Explain : Divisors of 6 is (1,2,3) and sum all the values 1+2+3=6.
Example-2:
Input : n = 8 Output : Not a Perfect number Explain : Divisors of 8 is (1,2,4) and sum all the values 1+2+4=7.
Solution
public class Main { public static void main(String [] args) { int n = 6,sum = 0; for(int i=1;i<n;i++) { if(n%i==0) { sum = sum + i; } } if(sum==n) { System.out.print("Perfect number"); } else { System.out.print("Not a Perfect number"); } } }
n = 6 k = 0 for i in range(1,n): if(n % i == 0): k += i if(n==k): print("Perfect number") else: print("Not a Perfect number")
Output
Perfect number