w3resource

C Exercises: Find the Deficient numbers (integers) between 1 to 100


5. Deficient Numbers Between 1 and 100 Variants

Write a program in C to find the Deficient numbers (integers) between 1 to 100.

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, ctr = 0; // Declare variables 'n' for the number, 'ctr' to count deficient numbers
    printf("\n\n The Deficient numbers between 1 to 100 are: \n");
    printf(" ------------------------------------------------\n");

    for (int j = 1; j <= 100; j++) // Loop through numbers from 1 to 100
    {
        n = j; // Assign the current value of 'j' to 'n'
        if (checkDeficient(n) == true) // Check if 'n' is a deficient number
        {
            printf("%d ", n); // Print the deficient number
            ctr++; // Increment the counter for deficient numbers
        }
    }

    printf("\n The Total number of Deficient numbers are: %d \n", ctr); // Print the total count of deficient numbers

    return 0;
}

Sample Output:

The Deficient numbers between 1 to 100 are:                                                                  
 ------------------------------------------------                                                             
1 2 3 4 5 7 8 9 10 11 13 14 15 16 17 19 21 22 23 25 26 27... 

Visual Presentation:

C programming: Find the Deficient numbers (integers) between 1 to 100.

Flowchart:

Flowchart: Find the Deficient numbers (integers) between 1 to 100

For more Practice: Solve these Related Problems:

  • Write a C program to list all deficient numbers within a user-specified range.
  • Write a C program to count the number of deficient numbers in a given interval.
  • Write a C program to display each deficient number along with its proper divisor sum.
  • Write a C program to find deficient numbers that are also prime numbers.

Go to:


PREV : Deficient Number Check Variants.
NEXT : Kaprekar Number Check 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.