C Program to Check Whether a Given Number is Perfect Number

A perfect number is a number that is equal to the sum of its proper divisors. For example, the divisors of 6 are 1, 2 and 3. The sum of the proper divisors of 6 is 1 + 2 + 3 = 6, which is a perfect number. The sum of the proper divisors of 28 is 1 + 2 + 4 + 7 + 14 = 28, which is also a perfect number.

Problem Description

Write a C Program that will ask the user for a number and then check whether the number is a perfect number or not.

Problem Solution

1. Take the number as input and store the number in a variable.
2. Loop through all the numbers from 1 to the number and check whether the number is a divisor of the number.
3. If the number is a divisor of the number, add it to the sum.
4. If the sum is equal to the number, print “The number is a perfect number”.
5. If the sum is not equal to the number, print “The number is not a perfect number”.

There are several ways to write a perfect number program in C language. Let’s look at the different techniques for writing a perfect number program.

Method 1: (Using For Loop)

In this approach we’ll not make use of any functions. We’ll just loop through all the numbers from 1 to the number usin for loop and check whether the number is a divisor of the number.

Example:

advertisement
advertisement

Step 1: i = 1, rem = num % i => 6 % 1 = 0. Because rem == 0, this condition is satisfied.

As a result, sum = sum + I => sum = 0 + 1 => 1

Step 2: i = 2, rem = num % i => 6 % 2 = 0. Because rem == 0, this condition is satisfied.

As a result, sum = sum + I => sum = 1 + 2 => 3

Note: Join free Sanfoundry classes at Telegram or Youtube

Step 3: i = 3, rem = num % i, => 6 % 3 = 0. Because rem == 0, this condition is satisfied.

As a result, sum = sum + I => sum = 3 + 3 => 6

Step 4: i = 4, rem = num % i, => 6 % 4 = 2. Because rem != 0, this condition is failed.

Step 5: i = 5, rem = num % i, => 6 % 5 = 1. Because rem != 0, this condition is failed.

Step 6: i = 6, Here, Sum == num. So print 6 is a perfect number.

advertisement
Program/Source Code

Here is source code of the C Program to check whether a given number is perfect number or not using for loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to Check whether a given Number is Perfect Number using for loop
 */
 
#include <stdio.h>
 
int main()
{
    int number, rem, sum = 0, i;
 
    printf("Enter a Number: ");
    scanf("%d", &number);
    for (i = 1; i <= (number - 1); i++)
    {
        rem = number % i;
	if (rem == 0)
        {
            sum = sum + i;
        }
    }
    if (sum == number)
        printf("%d is perfect number", number);
    else
        printf("%d is not a perfect number", number);
    return 0;
}
Program Explanation

1. In this C program, we are reading the integer value using ‘number’ variable.
2. For loop statement is used to assign the modulus of the value of ‘number’ variable by the value of ‘i’ variable.
3. If condition statement is used to check the value of ‘rem’ variable is equal to 0, if the condition is true to execute if condition statement and compute the summation the value of ‘sum’ variable with the value of ‘i’ variable.
4. Then it will loop through all the numbers from 1 to the number and check whether the number is a divisor of the number.
5. If the number is a divisor of the number, it will add it to the sum.
6. Another If-else condition statement is used to check that both the value of ‘sum’ variable and the value of ‘number’ variable are equal, if the condition is true print the statement as perfect number.
7. Otherwise, execute else condition statement and print the statement as not a perfect number.

Time Complexity: O(n)
The above program for checking whether a number is perfect or not has a time complexity of O(n), where n is the number given as input.

Space Complexity: O(1)
In this program we are not initializing any array or other data types that takes lot of storage. We are just initializing the variable. Therefore, our space complexity is constant, i.e. O(1).

advertisement
Runtime Test Cases

Testcase 1: In this case, we enter the number “6” as input to determine whether it is a perfect number or not.

Enter the number: 6
6 is a perfect number

Testcase 2: In this case, we enter the number “34” as input to determine whether it is a perfect number or not.

Enter the number: 34
34 is not a perfect number

Method 2: (Using While Loop)

In this approach, we’ll just use a while loop to iterate through all the integers from 1 to the number and see if the number is a divisor of the number.

Examples:

Input: 28
Divisors of 28 are 1, 2, 4, 7 and 14.

Sum = 1 + 2 + 4 + 7 + 14 = 28

Output: 28 is a perfect number

Input: 34
Divisors of 34 are 1, 2 and 17.

Sum = 1 + 2 + 17 = 20. 34 != 20

Output: 34 is not a perfect number

Program/Source Code

Here is source code of the C Program to check whether a given number is perfect number or not using while loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C program to check whether a number is a perfect number or not using while loop
 */
 
#include <stdio.h>
 
int main(void)
{
    int number, i, sum = 0;
 
    printf("Enter the number: ");
    scanf("%d", &number);
 
    i = 1;
    while (i < number)
    {
        if (number % i == 0)
            sum += i;
        i++;
    }
 
    if (sum == number)
        printf("%d is a perfect number", number);
    else
        printf("%d is not a perfect number", number);
}
Program Explanation

1. Take the number as input and store the number in a variable number.
2. Loop through all the numbers from 1 to the number and check whether the number is a divisor of the number.
3. Inside while loop, take the number and divide it by i and store the result in a variable sum.
4. If the sum is equal to the number, print “The number is a perfect number“.
5. Otherwise, print “The number is not a perfect number”.

Time Complexity: O(n)
The above program for checking whether a number is perfect or not has a time complexity of O(n), where n is the number given as input.

Space Complexity: O(1)
In this program we are not initializing any array or other data types that takes lot of storage. We are just initializing the variable. Therefore, our space complexity is constant, i.e. O(1).

Runtime Test Cases

Testcase 1: In this case, we enter the number “11” as input to determine whether it is a perfect number or not.

Enter the number: 11
11 is not a perfect number

Testcase 2: In this case, we enter the number “28” as input to determine whether it is a perfect number or not.

Enter the number: 28
28 is a perfect number

Method 3: (Using Function)

In this approach we will make use of a function. We’ll take the number as input and then call the function.

Methods used:

  • int is_perfect_number(int number) – This function will check whether the number is a perfect number or not.
  • int is_divisor(int number, int divisor) – This function will check whether the number is a divisor of the number.
  • int sum_of_divisors(int number) – This function will return the sum of the divisors of the number.

Examples:

Input: 6
Divisors of 6 are 1, 2 and 3.

Sum = 1 + 2 + 3 = 6

Output: 6 is a perfect number

Program/Source Code

Here is source code of the C Program to check whether a given number is perfect number or not using function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C program to check whether a number is a perfect number or not using function
 */
 
#include <stdio.h>
 
int is_divisor(int number, int divisor)
{
    if (number % divisor == 0)
        return 1;
    else
        return 0;
}
 
int sum_of_divisors(int number)
{
    int i = 1, sum = 0;
    while (i < number)
    {
        if (is_divisor(number, i))
            sum += i;
         i++;
    }
    return sum;
}
 
int is_perfect_number(int number)
{
    if (sum_of_divisors(number) == number)
        return 1;
    else
        return 0;
}
 
int main(void)
{
    int number;
 
    printf("Enter the number: ");
    scanf("%d", &number);
 
    if (is_perfect_number(number))
        printf("The number is a perfect number");
    else
        printf("The number is not a perfect number");
}
Program Explanation

1. Take the number as input and store the number in a variable number.
2. Call the function is_perfect_number and passes the number as input.
3. Inside the function another function sum_of_divisors is called and the number is passed as input.
4. Inside this function, we’ll loop through all the numbers from 1 to the number and check whether the number is a divisor of the number.
5. If the number is a divisor of the number, it will add it to the sum.
6. If the sum is equal to the number, it will print the number is a perfect number.
7. Otherwise, it will print the number is not a perfect number.

Time Complexity: O(n)
The above program for checking whether a number is perfect or not has a time complexity of O(n), where n is the number given as input.

Space Complexity: O(1)
In this program we are not initializing any array or other data types that takes lot of storage. We are just initializing the variable. Therefore, our space complexity is constant, i.e. O(1).

Runtime Test Cases

Testcase 1: In this case, we enter the number “99” as input to determine whether it is a perfect number or not.

Enter the number: 99
99 is not a perfect number

Testcase 2: In this case, we enter the number “33550336” as input to determine whether it is a perfect number or not.

Enter the number: 33550336
33550336 is a perfect number

Method 4: Find Perfect Numbers in a Given Range

In this approach, we will ask the user for two limits and then print all the perfect numbers between the limits.

Algorithm:
1. Accept the two limits as input.
2. Loop through all the numbers from the first limit to the second limit and check whether the number is a divisor of the number.
3. If the number is a divisor of the number, add it to the sum.
4. If the sum is equal to the number, print the number.

Example:
Input: lower limit: 1 and upper limit: 10
Output: 6

Program/Source Code

Here is source code of the C Program to find the perfect numbers between two limits.. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C program to find the perfect numbers between two limits.
 */
 
#include<stdio.h>
#include<stdlib.h>
 
int isPerfect(int n)
{
    int i, sum = 0;
    for (i = 1; i < n; i++)
    {
        if (n % i == 0)
        {
            sum += i;
        }
    }
    if (sum == n)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
 
void printPerfect(int lower, int upper)
{
    int i;
    for (i = lower; i <= upper; i++)
    {
        if (isPerfect(i))
        {
            printf("%d ", i);
        }
    }
}
 
int main(void)
{
    int lower, upper;
    printf("Enter the lower limit: ");
    scanf("%d", &lower);
    printf("Enter the upper limit: ");
    scanf("%d", &upper);
    printf("The perfect numbers between %d and %d are: \n", lower, upper);
    printPerfect(lower, upper);
    return 0;
}
Program Explanation

1. In this program, we begin with asking the user for the upper and lower limits and store the numbers in lowerLimit and upperLimit variables.
2. Call a loop through all the numbers from the lower limit to the upper limit and check whether the number is a perfect number. If the number is a perfect number, print the number.
3. In the printPerfectNumbers function, we loop through all the numbers from the lower limit to the upper limit and check whether the number is a perfect number. If the number is a perfect number, print the number.
4. In the isPerfect function, we loop through all the numbers from 1 to the number and check whether the number is a divisor of the number.
5. If the number is a divisor of the number, add it to the sum. If the sum is equal to the number, return 1. Otherwise, return 0.

Time Complexity: O(n2)
The above program for checking whether a number is perfect or not has a time complexity of O(2), where n is the number given as input.

Space Complexity: O(1)
In this program we are not initializing any array or other data types that takes lot of storage. We are just initializing the variable. Therefore, our space complexity is constant, i.e. O(1).

Runtime Test Cases

Testcase 1: In this case, we enter the lower limit as “3” and the upper limit as “100” as input to find all perfect numbers in the range.

Enter the lower limit: 3
Enter the upper limit: 100
Perfect numbers between 3 and 100 are:
6 28

Testcase 2: In this case, we enter the lower limit as “2” and the upper limit as “100000” as input to find all perfect numbers in the range.

Enter the lower limit: 2
Enter the upper limit: 100000
Perfect numbers between 2 and 100000 are:
6 28 496 8128

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.