Friendly pair or not (amicable or not) using Java

Check Whether or Not the Two Numbers are a Friendly Pair in Java

Given Two integer inputs as the numbers, the objective is to check whether the ratio of the sum of the factors of the number except the number itself upon the number, of the both numbers, matches or not. Therefore, we’ll write a code to Check Whether or Not the Two Numbers are a Friendly Pair in Java Language.
Example
Input : 6 12
Output : Yes, they are a friendly pair.
friendly pair or not in java

Check Whether or Not the Two Numbers are a Friendly Pair in Java Language

Given two integer as the numbers, the objective is to check whether the ratio of the sum of the factors of a number except the number itself upon the number, for both numbers, matches or not using Java Language. To do so we’ll firstly find all the factors of the two numbers given as the input. Then we divide the sum with the numbers to get the ratio. Now we’ll compare both the ratios. For any pair of numbers to be a Friendly Pair, the ratio must match. Here are a few methods to check for Friendly Pairs using Java Language,

  • Method 1: Using the Range until Number
  • Method 2: Using the Range until Sqrt( Number )

We’ll discuss the above mentioned methods in detail in the upcoming sections. Do check the blue box mentioned below for better understanding. Check out the Java page to Find all the Factors of a Number for better understanding.

Method 1: Using the Range until Number

Java Code

Run
public class Main
 {
   public static void main (String[]args)
   {

     int num1 = 30, num2 = 140;

     int sum1 = getDivisorsSum (num1);
     int sum2 = getDivisorsSum (num2);

     if (sum1 / num1 == sum2 / num2)
       System.out.println (num1 + " & " + num2 + " are friendly pairs");
     else
         System.out.println (num1 + " & " + num2 + " are not friendly pairs");
   }

   static int getDivisorsSum (int num)
   {

     int sum = 0;

     for (int i = 1; i < num; i++)
       {
 	    if (num % i == 0)
 	    sum = sum + i;
       }
     return sum;
   }
 }

Output

30 & 140 are friendly pairs

Method 2: Using the Range until Sqrt( Number )

Java Code

Run
public class Main
{
  public static void main (String[]args)
  {

    int num1 = 30, num2 = 140;

    int sum1 = getDivisorsSum (num1);
    int sum2 = getDivisorsSum (num2);

    if (sum1 / num1 == sum2 / num2)
      System.out.println (num1 + " & " + num2 + " are friendly pairs");
    else
        System.out.println (num1 + " & " + num2 + " are not friendly pairs");
  }

  static int getDivisorsSum (int n)
  {

    int sum = 0;
    
    for(int i = 1; i < Math.sqrt(n); i++) 
    { 
        if (n % i == 0) 
        { 
            // For n : (1, n) will always be pair of divisor 
            // acc to def., we must ignore adding n itself as divisor 
            // when calculating for abundant number 
            if(i == 1) 
                sum = sum + i; 

            // Example For 100 (10,10) will be one of the pair 
            // But, we should add value 10 to the sum just once 
            else if(i == n/i) 
                sum = sum + i; 

            // add both pairs as divisors 
            // For any divisor i, n/i will also be a divisor 
            else 
                sum = sum + i + n/i; 
        } 
    } 
    return sum; 
  }
}

Output

30 & 140 are friendly pairs

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

8 comments on “Friendly pair or not (amicable or not) using Java”


  • Radha

    public class Main
    {
    public static void main (String[]args)
    {

    int n=6,m=28;
    int sumn=0;
    int summ=0;
    for(int i=1;i<=n/2;i++)
    {
    if(n%i==0)
    {
    sumn+=i;
    }
    }
    System.out.println(sumn);

    for(int i=1;i<=m/2;i++)
    {
    if(m%i==0)
    {
    summ+=i;
    }
    }
    System.out.println(summ);
    if(sumn/n==summ/m)
    {
    System.out.println(n+" "+m+"Friendly Pair");
    }
    else
    {
    System.out.println(n+" "+m+" Not Friendly Pair");
    }
    }
    }


  • commander

    public class Main
    {
    public static void main(String[] args) {
    java.util.Scanner Scan = new java.util.Scanner (System.in) ;
    int a=Scan.nextInt(),b=Scan.nextInt();
    if(factor(a)/a == factor(b)/b)
    System.out.println(“Friendly Pair”);
    else
    System.out.println(“Not Friendly Pair”);
    }

    static int factor(int n)
    {
    int sum=0;
    for(int i=1;i<n;i++)
    {
    if(n%i==0)
    sum=i+sum;
    }
    return sum;
    }
    }


  • Play Station

    import java.util.Scanner;
    public class friendly_pair {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner in = new Scanner(System.in);
    int num1=in.nextInt();
    int num2=in.nextInt();

    int sum1=0;
    int sum2=0;
    System.out.print(“Factors of “+num1+” are: “);
    for(int i=1;i<num1;i++) {
    if(num1%i==0) {
    System.out.print(i+" ");
    sum1=sum1+i;
    }
    }
    System.out.print("\nFactors of "+num2+" are: ");
    for(int i=1;i<num2;i++) {
    if(num2%i==0) {
    System.out.print(i+" ");
    sum2=sum2+i;
    }
    }
    System.out.print("\nThe Result is: ");
    if((num1%sum1==0)&&(num2%sum2==0)) {
    System.out.println("friendly pair");
    }
    else {
    System.out.println("Not a friendly pair");
    }
    }

    }


  • CHINTU KOMMALA

    import java.util.*;
    class HelloWorld {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n=sc.nextInt();
    int m=sc.nextInt();
    int num1=factSum(n);
    int num2=factSum(m);
    if(n%num1==m%num2)
    System.out.println(n+” and “+m+” are Friendly Pair number.”);
    else
    System.out.println(n+” and “+m+” are Not Friendly Pair Number.”);

    }
    static int factSum(int num)
    {
    int sum=0;
    for(int i=1;i<num ;i++){
    if(num % i==0)
    sum+=i;
    }
    return sum;
    }

    }


  • pavan

    import java.util.Scanner;
    public class Main{
    public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println(“Enter a number : “);
    int n = scan.nextInt();
    System.out.println(“Enter second number :”);
    int m = scan.nextInt();
    int sum=0,sum1=0;

    for(int i = 1; i<n;i++)
    {
    if(n%i==0)
    sum=sum+i;
    }
    int div = sum/n;
    for(int i=1;i<m;i++)
    {
    if(m%i==0)
    sum1=sum1+i;
    }
    int div2 = sum1/m;
    if(div==div2) {
    System.out.print("Friendly pair");
    }
    else {
    System.out.println("Not");
    }
    }
    }