C program to find perfect numbers between 1 to n

Write a C program to print all Perfect numbers between 1 to n. C program to find all perfect numbers between given range. How to generate all perfect numbers between given interval using loop in C programming. Logic to find all perfect numbers in a given range in C program.

Example

Input

Input upper limit: 100

Output

Perfect numbers between 1 to 100: 6, 28

Required knowledge

Basic C programming, For loop, Nested loop, If else

Must know –

What is Perfect number?

Perfect number is a positive integer which is equal to the sum of its proper positive divisors.

For example: 6 is the first perfect number
Proper divisors of 6 are 1, 2, 3
Sum of its proper divisors = 1 + 2 + 3 = 6.
Hence 6 is a perfect number.

Logic to find all Perfect number between 1 to n

Below is the step by step descriptive logic to find Perfect numbers from 1 to n.

  1. Input upper limit from user to print all Perfect numbers. Store it in some variable say end.
  2. Run a loop from 1 to end, incrementing 1 in each iteration. The loop structure should look like for(i=1; i<=end; i++). Inside this loop, you need to check each element for Perfect number.
  3. Inside the loop print the current number if it is a Perfect number.

    Read more - Program to check Perfect number.

Program to find all perfect numbers between 1 to n

/**
 * C program to print all Perfect numbers between 1 to n 
 */

#include <stdio.h>

int main()
{
    int i, j, end, sum;

    /* Input upper limit to print perfect number */
    printf("Enter upper limit: ");
    scanf("%d", &end);

    printf("All Perfect numbers between 1 to %d:\n", end);
    
    /*
     * Iterate from 1 to end
     */
    for(i=1; i<=end; i++)
    {
        sum = 0;

        /*
         * Check whether the current number i is Perfect number or not
         */
        for(j=1; j<i; j++)
        {
            if(i % j == 0)
            {
                sum += j;
            }
        }
 
        /* If the current number i is Perfect number */
        if(sum == i)
        {
            printf("%d, ", i);
        }
    }

    return 0;
}

Once, you got the logic to print perfect numbers from 1 to n. You can easily modify the logic to print perfect numbers in given range. I am writing the below program with little modification to print perfect numbers in given range.

Program to generate perfect numbers in given range

/**
 * C program to print all Perfect numbers between 1 to n 
 */

#include <stdio.h>

int main()
{
    int i, j, start, end, sum;

    /* Input lower and upper limit from user */
    printf("Enter lower limit: ");
    scanf("%d", &start);
    printf("Enter upper limit: ");
    scanf("%d", &end);

    printf("All Perfect numbers between %d to %d:\n", start, end);
    
    /*
     * Iterate from start to end
     */
    for(i=start; i<=end; i++)
    {
        sum = 0;

        /*
         * Check whether the current number i is Perfect number or not
         */
        for(j=1; j<i; j++)
        {
            if(i % j == 0)
            {
                sum += j;
            }
        }
 
        /* If the current number i is Perfect number */
        if(sum == i)
        {
            printf("%d, ", i);
        }
    }

    return 0;
}

Output

Enter lower limit: 1
Enter upper limit: 1000
All Perfect numbers between 1 to 1000:
6, 28, 496,

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>