C program to print all Perfect numbers from 1 to n

Category: C Program

Learn how to write a C program to print all perfect numbers from 1 to n. This guide includes an explanation of perfect numbers, a step-by-step algorithm, and complete code with examples.

A perfect number is a positive integer that is equal to the sum of its proper divisors, excluding itself. Proper divisors of a number are the positive divisors other than the number itself. For example, 28 is a perfect number because its proper divisors are 1, 2, 4, 7, and 14, and their sum is 1 + 2 + 4 + 7 + 14 = 28.

In this article, we will write a C program to find and print all perfect numbers in the range from 1 to a user-specified number n. We'll start by understanding the concept of perfect numbers in more detail, then outline the algorithm, and finally provide the complete C program with an explanation.

Understanding Perfect Numbers

A perfect number is a number that satisfies the following condition:

Sum of proper divisors=Number

For example:

  • 6 is a perfect number because its proper divisors are 1, 2, and 3, and their sum is 1 + 2 + 3 = 6.
  • 28 is a perfect number because its proper divisors are 1, 2, 4, 7, and 14, and their sum is 1 + 2 + 4 + 7 + 14 = 28.

Algorithm to Find Perfect Numbers from 1 to n

  1. Input the number n: Get the upper limit from the user.
  2. Iterate through each number from 1 to n.
  3. Find proper divisors: Iterate through all numbers from 1 to the current number - 1 and find the divisors.
  4. Sum the divisors: Calculate the sum of all proper divisors.
  5. Check if the sum is equal to the original number.
  6. Print the number if it is a perfect number.

Write a C program to print all Perfect numbers from 1 to n

Here's the complete C program to print all perfect numbers from 1 to n:

#include <stdio.h>

int main() {
    int n, num, sum;

    // Input the upper limit from the user
    printf("Enter an upper limit: ");
    scanf("%d", &n);

    printf("Perfect numbers between 1 and %d are:\n", n);

    // Iterate through each number from 1 to n
    for (num = 1; num <= n; num++) {
        sum = 0;

        // Find and sum all proper divisors of the number
        for (int i = 1; i <= num / 2; i++) {
            if (num % i == 0) {
                sum += i;
            }
        }

        // Check if the sum of divisors is equal to the original number
        if (sum == num && num != 0) {
            printf("%d\n", num);
        }
    }

    return 0;
}

Output

Enter an upper limit: 100
Perfect numbers between 1 and 100 are:
6
28

Explanation of the Code

  1. Input the Upper Limit:
    • scanf("%d", &n); reads the upper limit ( n ) from the user.
  2. Iterate through Each Number:
    • The for loop iterates through each number from 1 to ( n ).
  3. Find and Sum Proper Divisors:
    • The nested for loop iterates through all numbers from 1 to num / 2 to find the divisors.
    • if (num % i == 0) checks if i is a divisor of num.
    • sum += i; adds the divisor to the sum.
  4. Check for the Perfect Number:
    • The condition if (sum == num && num != 0) checks if the sum of the proper divisors equals the original number. If true, it prints that the number is perfect.

Example Runs

Let's look at some example runs to see how the program works:

Example 1

Enter an upper limit: 1000
Perfect numbers between 1 and 1000 are:
6
28
496
  • Calculation for 6: Proper divisors are 1, 2, 3. Sum = 1 + 2 + 3 = 6.
  • Calculation for 28: Proper divisors are 1, 2, 4, 7, 14. Sum = 1 + 2 + 4 + 7 + 14 = 28.
  • Calculation for 496: Proper divisors are 1, 2, 4, 8, 16, 31, 62, 124, 248. Sum = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248 = 496.

Example 2

Enter an upper limit: 500
Perfect numbers between 1 and 500 are:
6
28

Recommended Posts