C Program to find Perfect Number between 1 to n

In this blog post, we learn how to write a C Program to find Perfect Number between 1 to n?. We will write the C Program to find Perfect Number between 1 to n using the Arithmetic Operators. 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 programming.Write a function to check if a given number is perfect or not.

What is the perfect number?

A perfect number is a positive integer which is equal to the sum of its proper positive divisors excluding the number itself. Let see an example,

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 check Perfect number between 1 to n:

  1. Ask the user to enter the value of n.
  2. Go through every number from 1 to n and check it is a perfect number or not.
  3. If the number is a perfect number then display it and repeat steps 2 and 3 till the n.
#include <stdio.h>


//return 1 is perfect number either return 0
int isPerfect(int num)
{
    int i,sum = 0;

    // Calculate sum of all proper divisors
    for(i = 1; i <= num / 2; i++)
    {
        //if i is a proper divisor of num
        if(num%i == 0)
        {
            sum += i;
        }
    }
    return (sum == num);
}

int main()
{
    int num, i;

    printf("Enter number = ");
    scanf("%d",&num);

    printf("List of Perfect Numbers from %d to %d are \n",1,num);
    for(i = 1; i <= num; i++)
    {
        if(isPerfect(i))
        {
            printf("%ld ",i);
        }
    }

    return 0;
}

Output:

Enter number = 1000
List of Perfect Numbers from 1 to 1000 are
6 28 496

 

Once, you got the understand the algorithm to print perfect numbers from 1 to n. You can easily print perfect numbers in a given range. Let see the algorithm to print the perfect number in given range.

Algorithm to check Perfect number in a given range:

  1. Ask the user to enter the range for the perfect number.
  2. Go through every number from StartRange to EndRange and check it is a perfect number or not.
  3. If the number is a perfect number then display it and repeat steps 2 and 3 till the end of the range.
#include <stdio.h>


//return 1 is perfect number either return 0
int isPerfect(int num)
{
    int i,sum = 0;

    // Calculate sum of all proper divisors
    for(i = 1; i <= num / 2; i++)
    {
        //if i is a proper divisor of num
        if(num%i == 0)
        {
            sum += i;
        }
    }
    return (sum == num);
}

int main()
{
    int rangeMinValue,rangeMaxValue, i;
    unsigned long int sum = 0;

    printf("Please Enter the rangeMinValue for perfect number = ");
    scanf("%d",&rangeMinValue);

    printf("Please Enter the rangeMaxValue for perfect number = ");
    scanf("%d",&rangeMaxValue);

    if((rangeMinValue > 0) && (rangeMaxValue > 0) && (rangeMaxValue > rangeMinValue ))
    {
        printf("List of Perfect Numbers from %d to %d are \n",rangeMinValue,rangeMaxValue);
        for(i = rangeMinValue; i <= rangeMaxValue; i++)
        {
            if(isPerfect(i))
            {
                printf("%ld ",i);
            }
        }

    }
    else
    {
        printf("Enter Valid numbers\n");
    }
    return 0;
}

Output:

Please Enter the rangeMinValue for perfect number = 1
Please Enter the rangeMaxValue for perfect number = 1000
List of Perfect Numbers from 1 to 1000 are
6 28 496