w3resource

C Exercises: Check whether a given number is Deficient or not


4. Deficient Number Check Variants

Write a program in C to check whether a given number is Deficient or not.

Test Data
Input an integer number: 15

Sample Solution:

C Code:

#include <stdio.h>
#include <stdbool.h>
#include <math.h>

// Function to calculate the sum of divisors of a number
int getSum(int n)
{
    int sum = 0;
    for (int i = 1; i <= sqrt(n); i++) // Loop through numbers from 1 to the square root of 'n'
    {
        if (n % i == 0) // Check if 'i' is a divisor of 'n'
        {
            if (n / i == i)
                sum = sum + i; // If 'i' is a divisor and is equal to the square root of 'n', add it to 'sum'
            else
            {
                sum = sum + i; // Add 'i' to 'sum'
                sum = sum + (n / i); // Add 'n / i' to 'sum'
            }
        }
    }
    sum = sum - n; // Subtract the number 'n' from the sum of its divisors
    return sum; // Return the sum of divisors
}

// Function to check if a number is a deficient number
bool checkDeficient(int n)
{
    return (getSum(n) < n); // Return true if the sum of divisors is less than 'n', otherwise return false
}

// Main function
int main()
{
    int n;
    printf("\n\n Check whether a given number is Deficient or not:\n");
    printf(" -----------------------------------------------------\n");
    printf(" Input an integer number: ");
    scanf("%d", &n); // Read an integer from the user and store it in variable 'n'

    // Check if the number is deficient and print the result
    checkDeficient(n) ? printf(" The number is Deficient.\n") : printf(" The number is not Deficient.\n");

    return 0;
}

Sample Output:

 Input an integer number: 15                                                                                  
 The number is Deficient.  

Visual Presentation:

C programming: Check whether a given number is Deficient or not.

Flowchart:

Flowchart: Check whether a given number is Deficient or not

For more Practice: Solve these Related Problems:

  • Write a C program to check if a number is deficient and output its sum of proper divisors.
  • Write a C program to compare a number's divisor sum to itself and classify it as deficient, perfect, or abundant.
  • Write a C program to check deficiency using an optimized loop for divisor calculation.
  • Write a C program to test the deficiency of numbers and output detailed divisor information.

Go to:


PREV : Abundant Numbers Between 1 and 1000 Variants.
NEXT : Deficient Numbers Between 1 and 100 Variants.

C Programming Code Editor:



Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.