ABUNDANT Number or Not in C Program
ADVERTISEMENTS
Abundant number or not in c program.
In this article, you will learn to check whether an input number is an Abundant number.
Example
INPUT A NUMBER: 12
12 IS AN ABUNDANT NUMBER
ABUNDANCE = 4
What is an Abundant Numbers?
- Input Number is
12
- Properly Divisors of 12 are
1, 2, 3, 4, 6
- The sum of properly divisors
1 + 2 + 3 + 4 + 6
=16
- Now compare the sum of proper divisors & input number
- Here is the sum of proper divisors
16
>12
- It means
12
is an abundant number
There are 4 ways to solve this problem:
- Using While loop
- Using Do-while loop
- Using For loop
- Using Function
You should know about the following topics in C programming to understand this program:
- If-else statement
- While loop in C
- Do-while loop in C
- For loop in C
- C Functions
1 - ABUNDANT Number or Not in C using While loop
// ABUNDANT Number or Not in C using While loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int number, sum = 0;
// Step-1 Take an input from the User
printf("INPUT A NUMBER: ");
scanf("%d", &number);
// Step-2 Calculate the sum of divisors of the input number
int i = 1;
while (i < number)
{
if (number % i == 0)
sum += i;
i++;
}
// Step-3 Compare the input number with the sum of its divisors
if (sum > number)
{
printf("\n%d IS AN ABUNDANT NUMBER", number);
printf("\nABUNDANCE = %d\n", sum - number);
}
else
printf("\n%d IS NOT AN ABUNDANT NUMBER!\n", number);
return 0;
}
Output
INPUT A NUMBER: 12
12 IS AN ABUNDANT NUMBER
ABUNDANCE = 4
2 - ABUNDANT Number or Not in C using Do-while loop
// ABUNDANT Number or Not in C using Do-while loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int number, sum = 0;
// Step-1 Take an input from the User
printf("INPUT A NUMBER: ");
scanf("%d", &number);
// Step-2 Calculate the sum of divisors of the input number
int i = 1;
do
{
if (number % i == 0)
sum += i;
i++;
} while (i < number);
// Step-3 Compare the input number with the sum of its divisors
if (sum > number)
{
printf("\n%d IS AN ABUNDANT NUMBER", number);
printf("\nABUNDANCE = %d\n", sum - number);
}
else
printf("\n%d IS NOT AN ABUNDANT NUMBER!\n", number);
return 0;
}
3 - ABUNDANT Number or Not in C using For loop
// ABUNDANT Number or Not in C using For loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int number, sum = 0;
// Step-1 Take an input from the User
printf("INPUT A NUMBER: ");
scanf("%d", &number);
// Step-2 Calculate the sum of divisors of the input number
for (int i = 1; i < number; i++)
{
if (number % i == 0)
sum += i;
}
// Step-3 Compare the input number with the sum of its divisors
if (sum > number)
{
printf("\n%d IS AN ABUNDANT NUMBER", number);
printf("\nABUNDANCE = %d\n", sum - number);
}
else
printf("\n%d IS NOT AN ABUNDANT NUMBER!\n", number);
return 0;
}
Output
INPUT A NUMBER: 16
16 IS NOT AN ABUNDANT NUMBER!
4 - ABUNDANT Number or Not in C using Function
// ABUNDANT Number or Not in C using Function
#include <stdio.h>
void is_abundant_number(int number)
{
int sum = 0, i = 1;
// Calculate the sum of divisors of the input number
while (i < number)
{
if (number % i == 0)
sum += i;
i++;
}
// Compare the input number with the sum of its divisors
if (sum > number)
{
printf("\n%d IS AN ABUNDANT NUMBER", number);
printf("\nABUNDANCE = %d\n", sum - number);
}
else
printf("\n%d IS NOT AN ABUNDANT NUMBER!\n", number);
}
// It's the main function of the program
int main()
{
int number;
// Step-1 Take an input from the User
printf("INPUT A NUMBER: ");
scanf("%d", &number);
// Step-2 Call the is_abundant_number(); function check input number is ABUNDANT or Not
is_abundant_number(number);
return 0;
}
Output
INPUT A NUMBER: 25
25 IS NOT AN ABUNDANT NUMBER!