Q. Write an algorithm and program to check whether the given two numbers are friendly pair or not?

Program To Check Friendly Pair : This section focuses on Friendly Pair algorithm and program. The programs should be practiced to improve the coding skills required for various interviews (campus interviews, walk-in interviews), coding rounds etc.

Abundancy Pair : An Abundancy Pair is a number for which the sum of its proper divisors is greater than the number itself.
Friendly Pair : Two numbers are said to be friendly pairs if they have common abundancy index.
Given an number m and n and our task is to find Abundancy index of both the number and then need to check if they have common abundancy index or not.

For Example :-

Input : m= 28, n = 6
Output : Yes, The number is Friendly Pair
Explanation : As we can see that input number m = 28 and n = 6. The proper divisors of 6 is 1, 2, 3, 6 and proper divisors of 28 is 1, 2, 4, 7, 14, 28. The sum of proper divisors is 1 + 2 + 3 + 6 = 12 and 1 + 2 + 4 + 7 + 14 + 28 = 56. Abundancy index of 6 and 28 are 2. So they are friendly pair.

Algorithm To Check Friendly Pair

\\Algorithm To Check Friendly Pair
START
Step 1 : Initilize numbers m and n.
Step 2 : Initialize two variables, sum1 and sum 2 with zero
Step 3 : Assign sum 1 with the sum of all the divisors of number m
Step 4 : Assign sum 2 with the sum of all the divisors of number n
Step 5 : If (sum 1==number1) and (sum 2==number 2), then print, "Friendly Numbers"
Step 6 : Else print "Not Friendly Numbers"
Stop


Program To Check Friendly Pair

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • //C Program To Check Friendly Pair
    #include <stdio.h> 
      
    int main()
    {
       //Create two variables to use in first and second numbers
          int i;
          int num1 = 6,num2 = 28;
        //two more variables created to store the sum of the divisors 
          int sum1 = 0;
          int sum2 = 0;
           
        // Using one variable for loop and second to check for each number
          for(int i=1;i<num1;i++)
          {
               // Condition check
               if(num1 % i == 0)
                   sum1 = sum1 + i;
          }
          // Calculating the sum of all divisors
         for(int i=1;i<num2;i++)
         {
              if(num2 % i == 0)
                  sum2 = sum2 + i;
         }
    
         // Check condition for friendly numbers
         if((num1 / num2) == (sum1 / sum2))
         {
              printf("Yes, The number is Friendly Pair\n");
         }
         else
         { 
             printf("No, The number is not Friendly Pair\n");
         }
    
        return 0;
    }
    
    //C++ Program To Check Friendly Pair
    #include <iostream>
    using namespace std;
    
    int main()
    {
        //Create two variables to use in first and second numbers
          int i;
          int num1 = 6,num2 = 28;
        //two more variables created to store the sum of the divisors 
          int sum1 = 0;
          int sum2 = 0;
           
        // Using one variable for loop and second to check for each number
          for(int i=1;i<num1;i++)
          {
               // Condition check
               if(num1 % i == 0)
                   sum1 = sum1 + i;
          }
          // Calculating the sum of all divisors
         for(int i=1;i<num2;i++)
         {
              if(num2 % i == 0)
                  sum2 = sum2 + i;
         }
    
         // Check condition for friendly numbers
         if((num1 / num2) == (sum1 / sum2))
         {
              cout<<"Yes, The number is Friendly Pair\n";
         }
         else
         { 
             cout<<"No, The number is not Friendly Pair\n";
         }
    
        return 0;
    }
    
    //Java Program To Check Friendly Pair
    
    public class LFC
    {
        public static void main(String[] args) {
           //Create two variables to use in first and second numbers
              int i;
              int num1 = 6,num2 = 28;
            //two more variables created to store the sum of the divisors 
              int sum1 = 0;
              int sum2 = 0;
               
            // Using one variable for loop and second to check for each number
              for( i=1;i<num1;i++)
              {
                   // Condition check
                   if(num1 % i == 0)
                       sum1 = sum1 + i;
              }
              // Calculating the sum of all divisors
             for( i=1;i<num2;i++)
             {
                  if(num2 % i == 0)
                      sum2 = sum2 + i;
             }
        
             // Check condition for friendly numbers
             if((num1 / num2) == (sum1 / sum2))
             {
                  System.out.println("Yes, The number is Friendly Pair\n");
             }
             else
             { 
                 System.out.println("No, The number is not Friendly Pair\n");
             }
            
        }
    }
    
    
    
    #Python Program To Check Friendly Pair
    
    #Enter the numbers to check
    num1 = 6
    num2 = 28
    import math 
    sum1=1 + num1  #sum of divisor of num1
    sum2=1 + num2  #sum of divisor of num2
    i=2 
    j=2 
    #finding divisor
    while(i<=math.sqrt(num1)):
        if(num1%i==0):
            if(num1//i==i):
                sum1+=i 
                
            else:
                sum1+=i + num1//i 
                
        i=i+1 
        
    while(j<=math.sqrt(num2)):
        if(num2%j==0):
            if(num2//j==j):
                sum2+=j
                
            else:
                sum2+=j + num2//j 
                
        j=j+1 
    if(sum1/num1==sum2/num2):
        print("Yes, The number is Friendly Pair") 
    else:
        print("No, The number is not Friendly Pair")
    
    //C# Program To Check Friendly Pair
    
    using System;
    class LFC {
      static void Main() {
            //Create two variables to use in first and second numbers
              int i;
              int num1 = 6,num2 = 28;
            //two more variables created to store the sum of the divisors 
              int sum1 = 0;
              int sum2 = 0;
               
            // Using one variable for loop and second to check for each number
              for( i=1;i < num1;i++)
              {
                   // Condition check
                   if(num1 % i == 0)
                       sum1 = sum1 + i;
              }
              // Calculating the sum of all divisors
             for( i=1;i < num2;i++)
             {
                  if(num2 % i == 0)
                      sum2 = sum2 + i;
             }
        
             // Check condition for friendly numbers
             if((num1 / num2) == (sum1 / sum2))
             {
                  Console.WriteLine("Yes, The number is Friendly Pair\n");
             }
             else
             { 
                 Console.WriteLine("No, The number is not Friendly Pair\n");
             }
        
      }
    }
    
    <?php
    //PHP Program To Check Friendly Pair
    
    
          $num1 = 6;
          $num2 = 28;
        //two more variables created to store the sum of the divisors 
          $sum1 = 0;
          $sum2 = 0;
           
        // Using one variable for loop and second to check for each number
          for($i=1;$i<$num1;$i++)
          {
               // Condition check
               if($num1 % $i == 0)
                   $sum1 = $sum1 + $i;
          }
          // Calculating the sum of all divisors
         for($i=1;$i<$num2;$i++)
         {
              if($num2 % $i == 0)
                  $sum2 = $sum2 + $i;
         }
    
         // Check condition for friendly numbers
         if(($num1 / $num2) == ($sum1 / $sum2))
         {
              echo "Yes, The number is Friendly Pair\n";
         }
         else
         { 
             echo "No, The number is not Friendly Pair\n";
         }
    
    //C Program To Check Friendly Pair
    #include <stdio.h> 
      
    int main()
    {
       //Create two variables to use in first and second numbers
          int i;
          int num1 = 6,num2 = 28;
        //two more variables created to store the sum of the divisors 
          int sum1 = 0;
          int sum2 = 0;
           
        // Using one variable for loop and second to check for each number
          for(int i=1;i<num1;i++)
          {
               // Condition check
               if(num1 % i == 0)
                   sum1 = sum1 + i;
          }
          // Calculating the sum of all divisors
         for(int i=1;i<num2;i++)
         {
              if(num2 % i == 0)
                  sum2 = sum2 + i;
         }
    
         // Check condition for friendly numbers
         if((num1 / num2) == (sum1 / sum2))
         {
              printf("Yes, The number is Friendly Pair\n");
         }
         else
         { 
             printf("No, The number is not Friendly Pair\n");
         }
    
        return 0;
    }
    

    Output

    Yes, The number is Friendly Pair
    

    Recommended Programs

       Program to find all pairs of elements in an integer array
       Program to find square root of a number