Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Name: perfectnumbers.c
- Copyright:
- Author: Mr.Kindle
- Date: 30-11-22 20:39
- Description: This code print all perfect numbers within a given range;
- youtube: https://youtu.be/oGXFCCsYKwQ
- */
- #include<stdio.h>
- int sumOfFactor(int);
- int main()
- {
- int i,n,j;
- printf("Enter maximum range: ");
- scanf("%d",&n);
- for(i=1;i<=n;i++)
- {
- j = sumOfFactor(i);
- if(i==j)
- printf("%d ",i);
- }
- return 0;
- }//main
- int sumOfFactor(int num)
- {
- int i,sum = 0;
- for(i=1;i<=num/2;i++)
- {
- if(num%i == 0)
- sum = sum + i;//adding all factors
- }
- // printf("%d ",sum);
- return sum;
- }//end of sumoffactor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement