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

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

This is one of the popular number and series questions asked in coding 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.

Now let’s back to the question.

You can also watch the video where I have explained this tutorial. Or keep continue reading.

What is a Perfect Number?

If the sum of the factors of the given 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",&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 a 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:
    print(f"{n} is not a Perfect number");

As 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 is 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.

You can also write this program more programmatic way.

Problem statement: Write a function that returns True if the given number is a perfect number, else False.

def is_perfect(x):
    sum = 0
    for i in range(1, x):
        if x%i == 0:
            sum += i
    if sum == x:
        return True
    else:
        return False

out = is_perfect(6)
print(out)

Output:

True

Complexity

As we are traversing the 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",&start);
  printf("Enter the ending number of the range");
  scanf("%d",&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.

If you are asked in the coding interview you should follow the standard practice.

Problem statement: Write a function that prints all the perfect numbers in the range of 1 to 100.

Here we are utilizing the above function is_perfect() to check if the given number is perfect or not.

def is_perfect(x):
    sum = 0
    for i in range(1, x):
        if x%i == 0:
            sum += i
    if sum == x:
        return True
    else:
        return False

def print_perfect_numbers(a, b):
    for i in range(a, b+1):
        if is_perfect(i):
            print(i)


print_perfect_numbers(1, 100)

Output:

6
28

Interviewers can also ask you to count the perfect numbers in the given range.

2 Comments

Leave a Reply

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