hpilo

Chapter6_Func_Ex3

Dec 15th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. /*
  2. =====================================================
  3. chapter 6: Functions
  4.  
  5. Ex3:
  6. =====================================================
  7. */
  8. public class MyProgram {
  9.  
  10. public static boolean isPerfect(int num){
  11. int sum=0;
  12. for(int i=1;i<num;i++){
  13. if(num%i==0)
  14. sum+=i;
  15. if(sum>num)
  16. return false;
  17. }
  18. if (sum==num)
  19. return true;
  20. return false;
  21. }
  22.  
  23. public static void main(String[] args) {
  24.  
  25. final int SIZE=10000;
  26. boolean res;
  27.  
  28. for(int i=1;i<SIZE;i++){
  29. res=isPerfect(i);
  30. if (res)
  31. System.out.println(i+" is perfect!");
  32. }
  33.  
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment