Advertisement
Mr_kindle

perfectnumber.c

Nov 30th, 2022 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.79 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. youtube:  https://youtu.be/oGXFCCsYKwQ
  9. */
  10.  
  11.  
  12. #include<stdio.h>
  13.  
  14. int sumOfFactor(int);
  15. int main()
  16. {
  17.     int i,n,j;
  18.     printf("Enter maximum range: ");
  19.     scanf("%d",&n);
  20.     for(i=1;i<=n;i++)
  21.         {
  22.             j = sumOfFactor(i);
  23.            if(i==j)
  24.             printf("%d ",i);
  25.            
  26.            
  27.         }
  28. return 0;
  29. }//main
  30.  
  31. int sumOfFactor(int num)
  32.     {
  33.         int i,sum = 0;
  34.         for(i=1;i<=num/2;i++)
  35.             {
  36.                 if(num%i == 0)
  37.                     sum = sum + i;//adding all factors
  38.             }
  39. //        printf("%d ",sum);
  40.         return sum;
  41.     }//end of sumoffactor
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement