Program to find if the given numbers are Friendly pair or not (Amicable or not)

Program to find if the given numbers are Friendly pair or not (Amicable or not) is discussed here. Friendly Pair are two or more numbers with a common abundance.

Input & Output format:

  • Input consists of 2 integers.
  • The first integer corresponds to number 1 and the second integer corresponds to number 2.
  • If it is a Friendly Pair display “Friendly Pair” or displays “Not Friendly Pair”.

For example, 6 and 28 are Friendly Pair.

(Sum of divisors of 6)/6 = (Sum of divisors of 28)/28.

Algorithm to check whether the given numbers are friendly pair or not

  • Input the numbers num1 and num2.
  • Initialize sum1 = sum2 = 0.
  • sum1 = sum of all divisors of num1.
  • sum2 = sum of all divisors of num2.
  • If (sum1 == num1) and (sum2 == num2), then print “Abundant Numbers”.
  • Else, print “Not Abundant Numbers”.

 

Program to check whether the given numbers are friendly pair or not

// C program to check whether the given numbers are friendly pairs

#include<stdio.h>
int main()
{
//fill the code
int num1, num2;
scanf(“%d”,&num1);
scanf(“%d”,&num2);
int sum1 = 0, sum2 = 0;
for(int i = 1; i < num1; i++)
{
if(num1 % i == 0)
{
sum1 = sum1 + i;
}
}

for(int i = 1; i < num2; i++)
{
if(num2 % i == 0)
{
sum2 = sum2 + i;
}
}
if(sum1 == num1 && sum2 == num2)
printf(“Friendly Pair”);
else
printf(“Not Friendly Pair”);
return 0;
}

// C++ program to check whether the given numbers are friendly pairs

#include<iostream>
using namespace std;

int main()
{
//fill the code
int num1, num2;
cin >> num1;
cin >> num2;
int sum1 = 0, sum2 = 0;
for(int i = 1; i < num1; i++)
{
if(num1 % i == 0)
{
sum1 = sum1 + i;
}
}

for(int i = 1; i < num2; i++)
{
if(num2 % i == 0)
{
sum2 = sum2 + i;
}
}
if(sum1 == num1 && sum2 == num2)
cout << “Friendly Pair”;
else
cout << “Not Friendly Pair”;
return 0;
}

// Java program to check whether the given numbers are friendly pairs

import java.util.*;
public class Main
{
public static void main(String[] args)
{
int num1, num2;
Scanner sc = new Scanner(System.in);
num1 = sc.nextInt();
num2 = sc.nextInt();

int sum1 = 0, sum2 = 0;
for(int i = 1; i < num1; i++)
{
if(num1 % i == 0)
{
sum1 = sum1 + i;
}
}

for(int i = 1; i < num2; i++)
{
if(num2 % i == 0)
{
sum2 = sum2 + i;
}
}
if(sum1 == num1 && sum2 == num2)
System.out.print(“Friendly Pair”);
else
System.out.print(“Not Friendly Pair”);
}
}

# Python program to check whether the given numbers are friendly pairs

num1 = int(input())
num2 = int(input())
sum1 = 0
sum2 = 0
for i in range(1,num1):
if(num1 % i == 0):
sum1 = sum1 + i
for i in range(1,num2):
if(num2 % i == 0):
sum2 = sum2 + i
if(sum1 == num1 and sum2 == num2):
print(“Friendly Pair”)
else:
print(“Not Friendly Pair”)

 

Output:

friendly pair or not