How to Check If Two Numbers are Affinity Numbers in Python


In this example we will show how to determine whether two digits are the affinity numbers.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

sum1 = 0
sum2 = 0
n1 = int(input("Enter the first number: "))
n2 = int(input("Enter the second number: "))
for i in range(1, n1):
    if n1 % i == 0:
        sum1 = sum1 + i
for j in range(1, n2):
    if n2 % j == 0:
        sum2 = sum2 + j
if sum1 == n2 and sum2 == n1:
    print("These two numbers are amicable numbers!")
else:
    print("These two numbers are not amicable numbers!")

Output:

Enter the first number: 284
Enter the second number: 220
These two numbers are amicable numbers!

Enter the first number: 550
Enter the second number: 480
These two numbers are not amicable numbers!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments