C Basic
C Interview Programs
- C Interview Programs
- C Abundant Number
- C Amicable Number
- C Armstrong Number
- C Average of N Numbers
- C Automorphic Number
- C Biggest of three numbers
- C Binary to Decimal
- C Common Divisors
- C Composite Number
- C Condense a Number
- C Cube Number
- C Decimal to Binary
- C Decimal to Octal
- C Disarium Number
- C Even Number
- C Evil Number
- C Factorial of a Number
- C Fibonacci Series
- C GCD
- C Happy Number
- C Harshad Number
- C LCM
- C Leap Year
- C Magic Number
- C Matrix Addition
- C Matrix Division
- C Matrix Multiplication
- C Matrix Subtraction
- C Matrix Transpose
- C Maximum Value of an Array
- C Minimum Value of an Array
- C Multiplication Table
- C Natural Number
- C Number Combination
- C Odd Number
- C Palindrome Number
- C Pascalβs Triangle
- C Power of 2
- C Power of 3
- C Pronic Number
- C Perfect Number
- C Perfect Square
- C Prime Factor
- C Prime Number
- C Smith Number
- C Strong Number
- C Sum of Array
- C Sum of Digits
- C Swap Two Numbers
- C Triangular Number
C Program to Check Abundant Number
Photo Credit to CodeToFun
π Introduction
In the realm of programming, understanding and identifying special types of numbers is a fascinating exploration. One such category is abundant numbers.
An abundant number, also known as an excessive number, is a positive integer that is smaller than the sum of its proper divisors (excluding itself).
In this tutorial, we will delve into a C program designed to check whether a given number is an abundant number or not.
The program will employ logic to calculate the sum of the proper divisors of a number and determine if it exceeds the number itself.
π Example
Let's dive into the C code to achieve this functionality.
#include <stdio.h>
// Function to check if a number is abundant
int isAbundant(int num) {
int sum = 1; // Start with 1 as every number is divisible by 1
// Iterate from 2 to the square root of the number
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0) {
sum += i;
if (i != num / i) {
sum += num / i;
}
}
}
// If the sum of divisors exceeds the number, it is abundant
return sum > num;
}
// Driver program
int main() {
// Replace this value with your desired number
int number = 12;
// Check if the number is abundant
if (isAbundant(number)) {
printf("%d is an abundant number.\n", number);
} else {
printf("%d is not an abundant number.\n", number);
}
return 0;
}
π» Testing the Program
To test the program with different numbers, simply replace the value of number in the main function.
12 is an abundant number.
Compile and run the program to check if the number is an abundant number.
π§ How the Program Works
- The program defines a function isAbundant that takes a number as input and returns 1 if the number is abundant and 0 otherwise.
- Inside the function, it iterates through numbers from 2 to the square root of the given number to find its divisors.
- For each divisor found, it adds it to the sum, including the corresponding divisor on the other side of the square root.
- Finally, it compares the sum of divisors with the original number to determine if it is abundant.
π Between the Given Range
Let's explore the C code that checks for abundant numbers within the specified range.
#include <stdio.h>
// Function to check if a number is abundant
int isAbundant(int num) {
int sum = 0;
// Find the sum of proper divisors
for (int i = 1; i <= num / 2; ++i) {
if (num % i == 0) {
sum += i;
}
}
return sum > num;
}
// Driver program
int main() {
printf("Abundant numbers between 1 and 50 are: ");
// Check for abundant numbers in the range 1 to 50
for (int i = 1; i <= 50; ++i) {
if (isAbundant(i)) {
printf("%d ", i);
}
}
printf("\n");
return 0;
}
π» Testing the Program
Abundant numbers between 1 and 50 are: 12 18 20 24 30 36 40 42 48
Run the program to see the abundant numbers within the specified range.
π§ How the Program Works
- The program defines a function isAbundant that checks whether a given number is abundant or not.
- Inside the function, it calculates the sum of proper divisors for the given number.
- The main function iterates through the numbers in the range of 1 to 50 and prints the abundant numbers.
π§ Understanding the Concept of Abundant Number
Before diving into the code, let's take a moment to understand abundant numbers. An abundant number is a positive integer for which the sum of its proper divisors (excluding itself) is greater than the number itself.
For example, the number 12 is abundant because its divisors (excluding 12) are 1, 2, 3, 4, 6, and the sum of these divisors is 16, which is greater than 12.
π’ Optimizing the Program
While the provided program is effective, there are opportunities for optimization, such as caching previously calculated divisor sums to avoid redundant computations.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (C Program to Check Abundant Number), please comment here. I will help you immediately.