Program to delete duplicates from a link list

Program to check Friendly pair

Before going to write a program  first we have to know what is friendly pair.

what is friendly pair :-

Two number n1 and n2 is said to friendly if (sum of divisor of n1/ n1 ) is equal to the (sum of divisor of n2/ n2) .

For example: –

(sum of divisors of 6 / 6 ) = (sum of divisors of 28 / 28)

(1+2+3)/6 = (1+2+4+7+14)/28

1 = 1

so (6,28) is a friendly pair.

Program to check Friendly pair :-

C++ Code:- 

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n1,n2,sum_d1=0,sum_d2=0;
    cout<<"Enter first number"<<endl;
    cin>>n1;
    cout<<"Enter second number"<<endl;
    cin>>n2;
    
    //find sum of divisors of first number
    for(int i=1;i<n1;i++)
    {
        if(n1%i==0)
          sum_d1+=i;
        
    }
    //find sum of divisors of second number
    for(int i=1;i<n2;i++)
    {
        if(n2%i==0)
          sum_d2+=i;
        
    }
    
    if((sum_d1/n1)==(sum_d2/n2))
      cout<<"friendly pair"<<endl;
    else
      cout<<"Not a friendly pair"<<endl;
    
     return 0;
}

Output:-

Enter first number
6
Enter second number
28
friendly pair

python code:-

def checkperfect(x):
    sum=0
    for i in range(1,x):
        if x%i==0:
            sum=sum+i 
    if sum==x:
        return 1 
    else:
        return 0
a,b=map(int,input().split())
r1=checkperfect(a)
r2=checkperfect(b)
if r1==1 and r2==1:
    print("Yes, they are friendly pair" )
else:
    print("No, they are not friendly pair")

output:-

6 28
Yes, they are friendly pair

 

Recommended post:-

codechef problems:-

Wipro :-

Infytq :-

Key Points;-

Hackerrank:-

C-tutorial:-

See more:-

Leave a Reply

Your email address will not be published.