Program to Check and Print All Perfect Numbers in the Given Range

This is one of the popular number questions asked in interviews.

There are many students who are interested to become software developers so the best way to crack the SDE roles is to practice programming. This is one of the common basic questions that are asked in the Interviews in some service and product-based companies.

Whats a Perfect Number?

If the sum of the factors of the give number is equal to the same number then it is known as a perfect number.

Example

6 is a perfect number.

Factors of 6 = 1, 2,3

Sum of Factor = Given number

1+2+3 = 6

6 = 6 (Therefore, 6 is a perfect number.)

Check Whether the Given Number is a Perfect Number or Not

Algorithm

  1. Start the Program
  2. Declare the necessary variable n denotes the number, sum to store the sum of the factors of a number and i denotes the iteration variable
  3. Take the input from the user to check whether it is a perfect number or not
  4. Find the factors of the number to add it to a sum variable
  5. Compare the given number with the sum of the variable and display the result
  6. End the Program

C++ Program

Prerequisite

C++ Code

#include<stdio.h>
int main()
{
  int n, i, sum;
  printf("Enter a number");
  scanf("%d",&amp;n);
  for(i=1;i<n;i++)
  {
    if(n%i==0)
    {
      sum=sum+i;
    }
  }
  if(n==sum)
    printf("%d is a Perfect number",n)
  else
    printf("%d is not a Perfect number", n);
}

I have used for loop here. You can also implement this logic using while or do-while loop.

Python Program

Prerequisite

Python Code

n = int(input("Enter a number"))
sum = 0

for i in range(1, n):
    if n%i == 0:
        sum += i

if n == sum:
    print(f"{n} is a Perfect number")
else:
    printf(f"{n} is not a Perfect number");

As like in C++ and Python, you can also implement the same program in Java.

Output

Testcase 1:

Enter a number 10
10 is not a Perfect number

Explanation:

Factors of 10 = 1, 2, 5

Sum of Factors 1+2+5 = 8

8 not equal to 10. So, 10 is not a perfect number.

Testcase 2:

Enter a number 28
28 is a Perfect number

Explanation:

Factors of 28 = 1, 2, 4, 7, 14

Sum of Factors 1+2+4+7+14 = 28

28 = 28. So, 28 is a perfect number.

Complexity

As we are traversing for-loop from 1 to n-1, the complexity of this code is O(n). So, this algorithm takes linear time.

We are using one integer constant ‘sum’ to store the sum of the factors of the given number. It has constant space complexity.

Find and Print all the Perfect Numbers in the Given Range

This is similar to the above problem so in the above problem you were asked to check whether the given number is a perfect number or not but in this problem you have to print all the perfect numbers within the given range

C++ Program

#include<stdio.h>
int main()
{
  int start, end, n, i, sum;
  printf("Enter the starting number of the range");
  scanf("%d",&amp;start);
  printf("Enter the ending number of the range");
  scanf("%d",&amp;end);
  for(n=start;n<=end;n++)
  {
    sum=0;
    for(i=1;i<n;i++)
    {
      if(n%i==0)
      {
        sum=sum+i;
      }
    }
    if(n==sum)
    {
      printf("%d",n);
    }
  }
}

Python Program

start = int(input("Enter the starting number of the range"))
end = int(input("Enter the ending number of the range"))

for n in range(start, end+1):
    sum = 0

    for i in range(1, n):
        if n%i == 0:
            sum += i
    #print(n, sum)

    if n == sum:
        print(n)

Output 

Enter the starting number of the range 1
Enter the ending number of the range  1000

6
28
496

Note: There are only three perfect numbers in the range of 1 to 1000. You can also be asked to count the perfect numbers in the given range.

Leave a Reply