write a program to determine whether a number n is equal to the sum of its proper positive divisors

 Write a program to determine whether a number n is equal to the sum of its proper positive divisors. 

Certainly! Here's a Python program that determines whether a given number `n` is equal to the sum of its proper positive divisors:

```python

def is_perfect_number(n):

    # Calculate the sum of proper positive divisors

    divisors_sum = 0

    for i in range(1, n):

        if n % i == 0:

            divisors_sum += i


    # Check if the number is equal to the sum of its proper positive divisors

    if divisors_sum == n:

        return True

    else:

        return False


# Test the function

number = int(input("Enter a number: "))

if is_perfect_number(number):

    print(f"{number} is a perfect number.")

else:

    print(f"{number} is not a perfect number.")

```

In this program, the function `is_perfect_number` takes an integer `n` as input and calculates the sum of its proper positive divisors by iterating over numbers from 1 to `n-1` and checking if they are divisors of `n`. If the sum of divisors is equal to `n`, it returns `True`, indicating that `n` is a perfect number. Otherwise, it returns `False`.


You can run this program and enter a number to check whether it is a perfect number or not.

Post a Comment

Previous Post Next Post

Ads

Ad