C Program to Find Abundant Number
A C program to find an abundant number sums all proper divisors of a number and checks if the sum is greater than the number using a loop and
if condition, for example: if (sum > num) printf("Abundant number\n");.Examples
Input12
Output12 is an abundant number
Input15
Output15 is not an abundant number
Input18
Output18 is an abundant number
How to Think About It
To find if a number is abundant, first find all its proper divisors (numbers less than it that divide it evenly). Add these divisors together. If the total sum is more than the original number, it is abundant; otherwise, it is not.
Algorithm
1
Get input number from the user2
Initialize sum to zero3
Loop from 1 to number-1 and check if the current number divides the input number evenly4
If yes, add it to sum5
After the loop, compare sum with the input number6
If sum is greater, print that the number is abundant; else print it is notCode
c
#include <stdio.h> int main() { int num, sum = 0; printf("Enter a number: "); scanf("%d", &num); for (int i = 1; i < num; i++) { if (num % i == 0) { sum += i; } } if (sum > num) { printf("%d is an abundant number\n", num); } else { printf("%d is not an abundant number\n", num); } return 0; }
Output
Enter a number: 12
12 is an abundant number
Dry Run
Let's trace the input 12 through the code to see how it finds if 12 is abundant.
1
Input number
num = 12, sum = 0
2
Loop through 1 to 11
Check divisors of 12: 1, 2, 3, 4, 6 divide evenly
3
Add divisors
sum = 1 + 2 + 3 + 4 + 6 = 16
4
Compare sum and number
sum (16) > num (12), so 12 is abundant
| i | num % i | sum |
|---|---|---|
| 1 | 0 | 1 |
| 2 | 0 | 3 |
| 3 | 0 | 6 |
| 4 | 0 | 10 |
| 5 | 2 | 10 |
| 6 | 0 | 16 |
| 7 | 5 | 16 |
| 8 | 4 | 16 |
| 9 | 3 | 16 |
| 10 | 2 | 16 |
| 11 | 1 | 16 |
Why This Works
Step 1: Find proper divisors
The program finds all numbers less than the input that divide it evenly using num % i == 0.
Step 2: Sum divisors
It adds these divisors to a running total stored in sum.
Step 3: Compare sum with number
If the sum of divisors is greater than the original number, the number is abundant.
Alternative Approaches
Using a function to check abundance
c
#include <stdio.h> int isAbundant(int n) { int sum = 0; for (int i = 1; i < n; i++) { if (n % i == 0) sum += i; } return sum > n; } int main() { int num; printf("Enter a number: "); scanf("%d", &num); if (isAbundant(num)) printf("%d is an abundant number\n", num); else printf("%d is not an abundant number\n", num); return 0; }
This approach improves readability by separating logic into a function.
Optimized divisor search up to num/2
c
#include <stdio.h> int main() { int num, sum = 0; printf("Enter a number: "); scanf("%d", &num); for (int i = 1; i <= num / 2; i++) { if (num % i == 0) sum += i; } if (sum > num) printf("%d is an abundant number\n", num); else printf("%d is not an abundant number\n", num); return 0; }
Checking divisors only up to half the number reduces unnecessary checks, improving efficiency.
Complexity: O(n) time, O(1) space
Time Complexity
The program loops from 1 to n-1 (or n/2 in optimized), so time grows linearly with input size.
Space Complexity
Uses only a few variables, so space is constant regardless of input.
Which Approach is Fastest?
Checking divisors up to n/2 is faster than up to n-1, but both are simple and efficient for small inputs.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Basic loop to n-1 | O(n) | O(1) | Simple understanding and small inputs |
| Optimized loop to n/2 | O(n/2) ~ O(n) | O(1) | Better efficiency for larger inputs |
| Function-based check | O(n) | O(1) | Code clarity and reuse |
Check divisors only up to half the number to save time.
Including the number itself as a divisor, which should be excluded.