- Write a function that tests if an integer is ABUNDANT (returns 1 if yes and 0 otherwise). The integer n is abundant when the sum of its divisors (including n itself) is greater than its double (2n). Example: 12 is abundant (1 + 2 + 3 + 4 + 6 + 12> 24)
- Test the function using a simple instruction
- Write a main function that takes 2 integers a and b and then displays the abundant numbers in the interval [a, b].
Difficulty level
Video recording
This exercise is mostly suitable for students
#include <stdio.h>
int ABUNDANT (int n)
{
int i, sum=0;
for(i=1; i<=n;i++)
if(n%i==0)
sum+=i;
if(sum>2*n)
return 1;
return 0;
}
int main()
{
int a,b, i;
printf("ABUNDANT(12)=%d\n",ABUNDANT(12));
do{
printf("Enter a: ");
scanf("%d",&a);
}while(a<=0);
do{
printf("Enter b (>%d): ", a);
scanf("%d",&b);
}while(b<=a);
for(i=a; i<=b;i++)
if(!ABUNDANT(i))
printf("%d is an abundant number\n",i);
return 0;
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Hashing using quadratic probing