Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Perfect Number Program in C

Last Updated on August 18, 2023 by Mayank Dham

In the realm of mathematics, a perfect number is a positive integer that precisely equals the sum of its positive divisors, excluding the number itself.

For instance, the positive integer 6 is evenly divided by the numbers 1, 2, and 3. Although we are aware that the number is also divisible on its own, we will nonetheless add it to the list of divisors. These divisors (1 + 2 + 3 = 6) when added together result in 6, which is the same as the number we have been considering. As a result, 6 is a perfect number.

What is the Perfect Number?

The sum of a perfect number’s appropriate positive divisors, except the number itself, is a positive integer. Here’s an illustration,
Input: n = 6
Proper divisors of 6 are 1, 2, 3
Sum of its proper divisors = 1 + 2 + 3 = 6
Hence 6 is a perfect number.

Algorithm to Find Perfect Number

  • Request an integer from the user.
  • To hold the total of the correct positive divisors, initialize another variable with 0.
  • From 1 through n/2, go over each integer and see if it is a divisor. Keep track of the total of all divisors. The number will be a perfect number if the total equals n; else, it will not be a perfect number.

Program to Find Perfect Number in C

There are three ways to find the perfect number in C:

  • Using for Loop
  • Using while Loop
  • Using recursion in C

Method 1: Using for Loop

In this method, we will write a c program that accepts an integer input from the user and checks whether the given number is a perfect number or not.

#include <stdio.h>
int main ()
{
    int num = 28, sum = 0;
    // iteratively check for all numbers in range [1, 27]
    for(int i = 1; i < num; i++){
        // check if i is a divisor, if yes then add to sum
        if(num % i == 0)
            sum = sum + i;
    }    
    if(sum == num)
        printf("%d is a perfect number",num);
    else
        printf("%d is not a perfect number",num);
}

Explanation: In the given example, Every value of i is used to run the loop. If the computed remainder equals 0, the total is equal to the sum plus j. A condition outside of the loop determines if the total is equal to the number, and if it is, the print statement is executed.

Method 2: Using a while loop

In this method, we will use a while loop to check whether the given number is a perfect number or not.

#include <stdio.h>
int main ()
{
    int num = 28, sum = 0, i = 1;    
    // iteratively check for all numbers if they are divisors
    while(i < num)  
    {
        // check if i is a divisor, if yes then add to sum
        if(num % i == 0)  
            sum = sum + i;  
        i++;  
    }    
    if(sum == num)
        printf("%d is a perfect number",num);
    else
        printf("%d is not a perfect number",num);
}

Explanation: In the given example, Every value of i is iterated via the while loop. A total is determined. A condition outside of the loop determines if the total is equal to the number, and if it is, the print statement is executed.

Method 3: Using recursion

In this method, we will use a recursive method to check whether the given number is a perfect number or not.

#include <stdio.h>
static int sum = 0;
int getSumDivisors(long num, int i)
{
    // since, all factors can be found will num/2
    // we will only check in this range
    if(i <= num/2)
    {
        if(num % i ==0)
            sum = sum + i;
        i++;
        // recursively call isPerfect
        getSumDivisors(num, i);
    }
    //returns the sum
    // when i becomes > num/2
    return sum;
}
int main ()
{
    int num = 28;    
    if(getSumDivisors(num, 1) == num)
        printf("%d is a perfect number",num);
    else
        printf("%d is not a perfect number",num);    
    return 0;
}

Explanation: In the given example, the function, PerfectNum does the calculation and returns the sum. Out of the loop, there is a condition that checks whether or not the sum is equal to the number, and accordingly, the print statement is executed.

Conclusion
In summary, the implementation of a perfect number program in C offers a practical illustration of number properties and algorithmic thinking. This program empowers users to input an integer and efficiently ascertain whether the entered number is a perfect number or not. By applying basic mathematical concepts and programming logic, the program contributes to a deeper understanding of number theory and coding practices in C.

Frequently Asked Questions (FAQs) related to the perfect number program in C:

1. What is a perfect number, and why is it important to identify them?
A perfect number is a positive integer that equals the sum of its proper divisors. Identifying perfect numbers is important for studying number theory and can have applications in areas like cryptography and optimization algorithms.

2. How does the C program determine if a given number is perfect?
The program calculates the sum of the proper divisors of the input number and checks if it equals the original number. If the sum matches, the number is considered perfect.

3. What are proper divisors of a number?
Proper divisors of a number are the positive divisors excluding the number itself. For example, the proper divisors of 28 are 1, 2, 4, 7, and 14.

4. Are there any limitations to this program?
The program might have limitations in handling very large numbers due to memory and performance constraints. Additionally, it only identifies perfect numbers and doesn’t classify them further (e.g., abundant or deficient numbers).

5. Can this program be extended to find multiple perfect numbers?
Yes, the program’s logic can be modified to find and display a list of perfect numbers within a given range by checking each number in the range.

6. What’s the time complexity of the perfect number program?
The time complexity depends on the factorization of the number and can vary. In general, the program’s time complexity can be considered moderate.

Other C Programs

C Program for Binary Search
C Program to Add Two Numbers
C Program to Calculate Percentage of 5 Subjects
C Program to Convert Binary Number to Decimal Number
C Program to Convert Celsius to Fahrenheit
C Program to Convert Infix to Postfix
C Program to Find Area of Circle
C Program to Find Roots of Quadratic Equation
C program to Reverse a Linked List
C program to reverse a number
Ascending Order Program in C
Menu Driven Program For All Operations On Doubly Linked List in C
C Program for Armstrong Number
C Program For Merge Sort For Linked Lists
C program for performing Bubble sort on Linked List
Hello World Program in C
Leap Year Program in C
Odd Even Program in C
Selection Sort Program in C

Leave a Reply

Your email address will not be published. Required fields are marked *