Python Program to Check If Two Numbers are Amicable Numbers or Not

This is a Python Program to check if two numbers are amicable numbers.

Problem Description

The program takes two numbers and checks if they are amicable numbers.

Problem Solution

1. Take in both the integers and store it in separate variables.
2. Find the sum of the proper divisors of both the numbers.
3. Check if the sum of the proper divisors is equal to the opposite numbers.
4. If they are equal, they are amicable numbers.
5. Print the final result.
6. Exit.

Program/Source Code

Here is the source code of the Python Program to check if two numbers are amicable numbers. The program output is also shown below.

x=int(input('Enter number 1: '))
y=int(input('Enter number 2: '))
sum1=0
sum2=0
for i in range(1,x):
    if x%i==0:
        sum1+=i
for j in range(1,y):
    if y%j==0:
        sum2+=j
if(sum1==y and sum2==x):
    print('Amicable!')
else:
    print('Not Amicable!')
Program Explanation

1. User must enter both the numbers and store it in separate variables.
2. Using a for loop and an if statement, find the proper divisors of both the numbers.
3. Find the sum of the proper divisors of both the numbers.
4. The if statement is used to check if the sum of proper divisors of the number is equal to the other number and vice-versa.
5. If it is equal, both the numbers are amicable numbers.
6. The final result is printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter number 1: 220
Enter number 2: 284
Amicable!
 
Case 2:
Enter number 1: 349
Enter number 2: 234
Not Amicable!

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.