Advertisement
Mr_kindle

amicablenumbers.c

Nov 30th, 2022 (edited)
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.83 KB | Source Code | 0 0
  1. /*
  2.     Name: amicablenumbers.c
  3.     Copyright:
  4.     Author: Mr.Kindle
  5.     Date: 30-11-22 20:39
  6.     Description: This code print all pairs of amicable 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,k;
  18.     printf("Enter maximum range: ");
  19.     scanf("%d",&n);
  20.     for(i=1;i<=n;i++)
  21.         {
  22.             j = sumOfFactor(i);
  23.             k = sumOfFactor(j);
  24.            
  25.             if(i==k && i<j)
  26.                 printf("%d %d\n",i,j);
  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