Learning coding means GreatToCode Be more than a Coder ! Greattocode , Join GreatToCode Community,1000+ Students Trusted On Us .If You want to learn coding, Then GreatToCode Help You.No matter what It Takes !


CODE YOUR WAY TO A MORE FULFILLING And HIGHER PAYING CAREER IN TECH, START CODING FOR FREE Camp With GreatToCode - Join the Thousands learning to code with GreatToCode
Interactive online coding classes for at-home learning with GreatToCode . Try ₹Free Per Month Coding Classes With The Top Teachers . five Python programs to determine whether the input number is a perfect number or not

five Python programs to determine whether the input number is a perfect number or not

Become More Then A coder | Learn & Start Coding Now.


Here are five Python programs to determine whether the input number is a perfect number or not:


### 1. Using Iterative Approach:

```python
# Program 1: Checking if a number is perfect using iterative approach
def is_perfect_number(n):
    divisors_sum = 0
    for i in range(1, n):
        if n % i == 0:
            divisors_sum += i
    return divisors_sum == n

number = 28
print(f"{number} is a perfect number:", is_perfect_number(number))
```

### 2. Using List Comprehension:

```python
# Program 2: Checking if a number is perfect using list comprehension
def is_perfect_number(n):
    divisors_sum = sum([i for i in range(1, n) if n % i == 0])
    return divisors_sum == n

number = 28
print(f"{number} is a perfect number:", is_perfect_number(number))
```

### 3. Using Functional Programming (Filter):

```python
# Program 3: Checking if a number is perfect using functional programming (filter)
def is_perfect_number(n):
    divisors_sum = sum(filter(lambda x: n % x == 0, range(1, n)))
    return divisors_sum == n

number = 28
print(f"{number} is a perfect number:", is_perfect_number(number))
```

### 4. Using Recursion:

```python
# Program 4: Checking if a number is perfect using recursion
def sum_of_divisors(n, i=1, sum=0):
    if i == n:
        return sum
    if n % i == 0:
        sum += i
    return sum_of_divisors(n, i + 1, sum)

def is_perfect_number(n):
    return sum_of_divisors(n) == n

number = 28
print(f"{number} is a perfect number:", is_perfect_number(number))
```

### 5. Using While Loop:

```python
# Program 5: Checking if a number is perfect using while loop
def is_perfect_number(n):
    divisors_sum, i = 0, 1
    while i < n:
        if n % i == 0:
            divisors_sum += i
        i += 1
    return divisors_sum == n

number = 28
print(f"{number} is a perfect number:", is_perfect_number(number))
```

These programs determine whether the input number (in this case, 28) is a perfect number using different approaches - iterative, list comprehension, functional programming with filter, recursion, and while loop. Feel free to replace the `number` variable with any desired number to check its perfection.

Post a Comment

0 Comments

•Give The opportunity to your child with GreatToCode Kid's • Online Coding Classes for Your Kid • Introduce Your kid To the world's of coding
•Fuel You Career with our 100+ Hiring Partners, Advance Your Career in Tech with GreatToCode. •Join The Largest Tech and coding Community and Fast Forward Your career with GreatToCode. •10000+ Learner's+ 90 % placement Guarantee. • Learning Coding is Better with the GreatToCode community .
•Greattocode Kid's •GreatToCode Career •GreatToCode Interview •GreatToCode Professional •GreatToCode for schools •GreatToCode For colleges •GreatToCods For Businesses.
Are you ready to join the millions of people learning to code? GreatToCode Pass is your one-stop-shop to get access to 1000+ courses, top-notch support, and successful job placement. What are you waiting for? Sign up now and get your future in motion with GreatToCode Pass.