Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Friendly pair or not (amicable or not) using Java

Friendly pair or not(amicable or not) using Java : 

Friendly Pair are two natural numbers whose sum of factors of first number and sum of factors of second number is equal to second number and first number respectively.

In other words, Sum of factors of first number is equal to the second number and Sum of factors of second number is equal to the first number.

For better understanding let us consider an example.

 

Example :

  • Suppose two numbers 220 and 284.
  • Factors of both numbers :

– Factors of 220 = 1 , 2 , 4 , 5 , 10 , 11 , 20 , 22 , 44 , 55 and 110 ;

– Factors of 284 = 1 , 2 , 4 , 71 and 142.

  • Sum of factors of both numbers :

– Sum of factors of 220 =

1 + 2 + 4 + 5 + 10 + 11 + 20 + 22 + 44 + 55 + 110 = 284

– Sum of factors of 284 =

1 + 2 + 4 + 71 + 142 = 220

  • Here we see that Sum of factors of first number is equal to the second number and Sum of factors of second number is equal to the first number. Hence, these both numbers are Friendly Pair to each other.
Friendly pair or not (amicable or not) using Java

Working :

Step 1 : Ask the user to enter two different numbers.

Step 2 : Declare two variables for storing sum of factors of two numbers entered by the user.

Step 3 : Use the logic for finding factors and calculate the sum of factors of the first number and the second number.

Step 4 : Compare whether the sum of factors of first number is equal to the second number and the sum of factors of second number is equal to the first number.

  • If this gets true , then the given number is Friendly Pair.
  • Else the number is not a Friendly Pair. 

Code in Java :

//Java program to check whether a number is friendly pair or not
import java.util.Scanner;
public class friendly_pair_or_not
{
public static void main(String[] args)
{
//scanner class declaration
Scanner sc = new Scanner(System.in);
//input from user
System.out.print("Enter First number : ");
int number1 = sc.nextInt();
System.out.print("Enter Second number : ");
int number2 = sc.nextInt();
//declare two variables to store the addition of factors of both numbers which are entered by user
int add1 = 0, add2 = 0;
//logic for finding factors and calculating sum of all those factors for number1
for(int i = 1 ; i < number1 ; i++)
{
if(number1 % i == 0)
add1 = add1 + i;
}
//logic for finding factors and calculating sum of all those factors for number2
for(int i = 1 ; i < number2 ; i++)
{
if(number2 % i == 0)
add2 = add2 + i;
}
//condition for friendly pair number
if(number1 == add2 && number2 == add1)
System.out.println("Number is Friendly Pair");
else
System.out.println("Number is not Friendly Pair");
//closing scanner class(not compulsory, but good practice)
sc.close();
}
}

Output :

Enter First number : 220
Enter Second number : 284
Number is Friendly Pair


Enter First number : 3453
Enter Second number : 5545
Number is not Friendly Pair