Advertisement
Mr_kindle

perfectnumbers.cpp

Nov 30th, 2022
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | Source Code | 0 0
  1. /*
  2.     Name: perfectnumbers.c
  3.     Copyright:
  4.     Author: Mr.Kindle
  5.     Date: 30-11-22 20:39
  6.     Description: This code print all perfect numbers within a given range;
  7. */
  8.  
  9.  
  10. #include<iostream>
  11. using namespace std;
  12.  
  13. int sumOfFactor(int);
  14. int main()
  15. {
  16.     int i,n,j;
  17.     cout << "Enter maximum range: ";
  18.     cin >> n;
  19.     for(i=1;i<=n;i++)
  20.         {
  21.             j = sumOfFactor(i);
  22.            if(i==j)
  23.             printf("%d ",i);
  24.            
  25.            
  26.         }
  27. return 0;
  28. }//main
  29.  
  30. int sumOfFactor(int num)
  31.     {
  32.         int i,sum = 0;
  33.         for(i=1;i<=num/2;i++)
  34.             {
  35.                 if(num%i == 0)
  36.                     sum = sum + i;//adding all factors
  37.             }
  38. //        printf("%d ",sum);
  39.         return sum;
  40.     }//end of sumoffactor
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement