In this tutorial, we will write a C program to check if a number is abundant number or not. A number is called an abundant number if sum of its proper divisors (excluding itself) is greater than the number itself.
Note: Proper divisors are numbers that divide the given number without leaving a remainder.
For example: Let’s check whether number 12 is abundant or not. Proper divisors of 12
are 1
, 2
, 3
, 4
, and 6
. The sum of these proper divisors is 1 + 2 + 3 + 4 + 6 = 16
. Since the sum of proper divisors (16
) is greater than the number itself (12
). Thus, we can say that 12 is an abundant number.
C Program to check if a number is abundant or not
#include <stdio.h>
int isAbundant(int num) {
int sum = 0;
// Calculate the sum of proper divisors
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sum += i;
}
}
// Check if the sum of proper divisors is greater than the number
return sum > num;
}
int main() {
int num;
// Input number from user
printf("Enter a number: ");
scanf("%d", &num);
// Check if the number is an abundant number
if (isAbundant(num)) {
printf("%d is an abundant number.\n", num);
} else {
printf("%d is not an abundant number.\n", num);
}
return 0;
}
The function isAbundant()
takes integer number as argument and returns 1 if the number is abundant else it returns 0.
In this function, a for loop finds the proper divisors and add those to the variable sum
. Then, it compares this sum with the number itself to determine if it’s abundant or not
Output:
Output 1:
Enter a number: 12
12 is an abundant number.
Output 2:
Enter a number: 7
7 is not an abundant number.
Leave a Reply