Dremendo Tag Line

Input 2 integers and check amicable pair or not using while loop in Python

while Loop - Question 9

In this question, we will see how to input two integers and check if it forms an amicable pair or not in Python programming using while loop. To know more about while loop click on the while loop lesson.

Q9) Write a program in Python to input two integers and check if it forms an amicable pair or not using while loop.

An amicable pair is such that, the sum of the factors excluding itself of first number is equal to the second number and the sum of factors excluding itself of the second number is equal to the first number.

Example, (220, 284) is an amicable pair since, sum of factors excluding itself of: 220 = 1+2+4+5+10+11+20+22+44+55+110 = 284 and sum of factors excluding itself of: 284 = 1+2+4+71+142 = 220

Program

i=1; sf1=0; sf2=0
print('Enter 2 Numbers')
n1=int(input())
n2=int(input())

while i<n1:
    if n1%i==0:
        sf1=sf1+i       # sum of factors of 1st number
    i=i+1

i=1
while i<n2:
    if n2%i==0:
        sf2=sf2+i       # sum of factors of 2nd number
    i=i+1

if sf1==n2 and sf2==n1:
    print('Amicable Pair')
else:
    print('Not Amicable Pair')
    

Output

Enter 2 numbers
220
284
Amicable Pair
video-poster