C Program to Find Perfect Number

In this post, we will learn how to find the perfect number using C Programming language. But before that, let’s first learn about perfect numbers.

A perfect number is a positive number that is equal to the sum of positive divisors excluding the number itself. For example: 6 has divisors 1, 2 and 3, and 1 + 2 + 3 = 6, so 6 is a perfect number.

We will use the following approaches to find the perfect number.

  1. Using For Loop
  2. Using Function

So, without further ado, let’s begin this tutorial.

C Program to Find Perfect Number

// C Program to Find Perfect Number
#include <stdio.h>
int main(){
    int i, num, sum = 0;
    
    // Asking for input
    printf("Enter a number: ");
    scanf("%d", &num);
    
    // Calculating sum of all positive divisors
    for (i = 1; i <= num/2; i++){
        if (num % i == 0){
            sum = sum + i;
        }
    }
    
    // Checking whether sum is equal to original number
    if (num == sum){
        printf("%d is a perfect number.", num);
    }
    else{
        printf("%d is not a perfect number.", num);
    }
    return 0;
}

Output 

Enter a number: 6
6 is a perfect number.

How Does This Program Work ?

    int i, num, sum = 0;

In this program, we have declared three integer data type variables named i, num, and sum.

    // Asking for input
    printf("Enter a number: ");
    scanf("%d", &num);

Then, the program asks the user to enter a number.

    // Calculating sum of all positive divisors
    for (i = 1; i <= num/2; i++){
        if (num % i == 0){
            sum = sum + i;
        }
    }

We use a for loop to calculate the sum of the divisors.

  • 1st For Loop Iteration: (i = 1; i <= 6/2; i++)
  • If (6 % 1 == 0), here the condition is true, so sum = sum + i = 0 + 1 = 1.
  • 2nd For Loop Iteration: (i = 2; i <= 6/2; i++)
  • If (6 % 2 == 0), here the condition is true, so sum = sum + i = 1 + 2 = 3.
  • 3rd For Loop Iteration: (i = 3; i <= 6/2; i++)
  • If (6 % 3 == 0), here the condition is true, so sum = sum + i = 3 + 3 = 6.
    // Checking whether sum is equal to original number
    if (num == sum){
        printf("%d is a perfect number.", num);
    }
    else{
        printf("%d is not a perfect number.", num);
    }

Now, we check whether the sum is equal to the original number or not. If yes, then the entered number is a perfect number otherwise the entered number is not a perfect number.

C Program to Find Perfect Number Using Function

// C Program to Find Perfect Number Using Functions
#include <stdio.h>

int sumDivisor(int);

int main(){
    int num, sum = 0;
    
    // Asking for input
    printf("Enter a number: ");
    scanf("%d", &num);
    
    // Calling out user-defined function
    sum = sumDivisor(num);
    
    // Checking for perfect number
    if (num == sum){
        printf("%d is a perfect number.", num);
    }
    else{
        printf("%d is not a perfect number.", num);
    }
    return 0;
}

// Defining custom function
int sumDivisor(int n){
    int i, sum = 0;
    
    for (i = 1; i <= n/2; i++){
        if (n % i == 0){
            sum = sum + i;
        }
    }
    return sum;
}

Output

Enter a number: 28
28 is a perfect number.

Conclusion

I hope after going through this post, you understand how to find the perfect number using C Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published.