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:-
- Primary test
- Sum or difference
- point and line
Wipro :-
- Update the booking ID | Wipro previous year question paper solution
- Pages in PDF
- Find the location id
- Find the odd digits
- Find the Product ID
Infytq :-
Key Points;-
Hackerrank:-
- Python : missing characters : hackerrank solution
- Python : string transformation | Hackerrank solution
- Active Traders certification test problem | Hackerrank Solution
- Usernames changes certification test problem | Hackerrank Solution
- string Representation of objects certification test hackerrank solution
- Average Function | hackerrank certification problem solution
C-tutorial:-
- Micros in C
- Pointer in c
- Function declaration
- Types of user define function
- return type of function
- 2D array
See more:-
- c program to convert specified days into years weeks and days
- Print Reverse Hollow Pyramid
- Update the booking ID | Wipro previous year question paper
- Pages in PDF | Wipro previous year question paper
- Sparse Matrix in data structure
- Find the location ID | Wipro previous year Coding question
- find the odd digits | Wipro Coding question
- Find the product id | Wipro Coding question
- Difference between static and dynamic memory allocation
- What is asymptotic Notation