C Program to check if the given two numbers are friendly pair or not

Program to Check if the given two numbers are friendly pair or not

We will code friendly pair in C. Friendly pair(Amicable numbers) are two different numbers related in a way such that the Ratio’s sum of the proper divisors divided by number itself for each is same

Example: 6 and 28 are friendly pairs because

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

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

1 = 1
friendly pair or not in c

Working:-

For two numbers num1 and num2

  • Initialize sum1 = 0 and sum2 = 0
  • Find the proper divisors of num1 and num2 and add to their respective sum1 and sum2
  • Check if (Sum of divisors of num1)/num1 = (Sum of divisors of num2)/num2 holds true or not
  • If same then print that they are friendly pairs
Friendly Pair in C Program

C Program to test a Friendly Pair

Run
#include <stdio.h>

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

int main ()
{
    int num1 = 6, num2 = 28;
    
    int sum1 = getDivisorsSum(num1);
    int sum2 = getDivisorsSum(num2);
    
    if(sum1/num1 == sum2/num2)
        printf("(%d, %d) are friendly pairs", num1, num2);
    else
        printf("(%d, %d) are not friendly pairs", num1, num2);

    
}
// Time complexity: O(N)
// Space complexity: O(1)

Output

(6, 28) are friendly pairs

Prime Course Trailer

Related Banners

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

7 comments on “C Program to check if the given two numbers are friendly pair or not”


  • SAHIL

    //programm for friendly no. or not:-

    #include

    int main(){

    int p,q,sum1=0,sum2=0,i,n1,t1,n2,t2;

    printf(“enter first no.”);
    scanf(“%d”,&n1);

    printf(“enter second number:”);
    scanf(“%d”,&n2);
    t1=n1;
    t2=n2;
    for(i=1;i<n1;i++){
    if(n1%i==0){
    sum1=sum1+i;
    }
    }
    p=sum1/t1;

    for(i=1;i<n2;i++){

    if(n2%i==0){
    sum2=sum2+i;
    }
    }
    q=sum2/t2;

    if(p==q)
    printf("friendly pair number");
    else
    printf("not friendly pair number");
    return 0;
    }


  • Vineeth

    Simple code using functions:

    #include

    int abundant(int num)
    {
    int i,temp,sum=0;
    temp=num;
    for(i=1;i<temp;i++)
    {
    if(temp%i==0)
    sum+=i;

    }
    return sum/num;
    }

    int main()
    {
    //enter value
    int num1,num2;
    // ai1 – abundant index of num1 and ai2 for num2
    int ai1,ai2;
    printf("Enter 2 number\n");
    scanf("%d\n%d",&num1,&num2);
    ai1=abundant(num1);
    ai2=abundant(num2);
    //checking condition
    if(ai1==ai2)
    printf("friendly pair");
    else
    printf("Not friendly pair");
    return 0;
    }


  • Itika

    #include
    void main()
    {
    int i, n1, n2, sum1=0, sum2=0;
    scanf(“%d %d”, &n1, &n2);
    for(i=1; i<n1; i++){
    if(n1%i==0){
    sum1+=i;
    }
    }
    for(i=1; i<n2; i++){
    if(n2%i==0){
    sum2+=i;
    }
    }
    if(sum1==n2 && sum2==n1)
    printf("Friendly Pair");
    else
    printf("Not Friendly");
    }


  • Raveena

    Is it correct? If it’s not, please help me to correct it.
    #include
    Int sum_of_divisors(int n);
    int main(){
    int n1,n2,sum1,sum2;
    printf(“Enter two numbers:”);
    scanf(“%d%d”, &n1,&n2);
    sum1=sum_of_divisors(n1);
    sum2=sum_of_divisors(n2);
    if(sum1/n1==sum2/n2){
    printf(“%d and %d are amicable”, n1,n2);
    }else{
    printf(“%d and %d are not amicable”, n1,n2);
    }
    return 0;
    }
    int sum_of_divisors(int n){
    int i, sum=0;
    for(i=1;i<=n;i++){
    if(n%i==0){
    sum=sum+i;
    }
    }
    return sum;
    }


  • Sadique Gametor

    /******************************************************************************

    Welcome to GDB Online.
    GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
    C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS
    Code, Compile, Run and Debug online from anywhere in world.

    *******************************************************************************/
    #include

    int main()
    {
    int a,b,i,j,k=0,k2=0,arr[100],arr2[100],sum=0,sum2=0;
    scanf(“%d %d”,&a,&b);
    for(i=1;i<=a;i++)
    {
    if(a%i==0)
    {
    arr[k]=i;
    k++;
    }

    }

    for(j=1;j<=b;j++)
    {
    if(b%j==0)
    {
    arr2[k2]=j;
    k2++;

    }

    }

    for(i=0;i<k;i++){
    sum=sum+arr[i];

    }
    for(i=0;i<k2;i++){
    sum2=sum2+arr2[i];

    }

    if((sum/a)==(sum2/b))
    {
    printf("friendly");
    }
    else
    printf("not");

    }


  • Pavendhan

    Above code is incomplete.

    #include
    int main()
    {
    //1 Create two variables to use in first and second numbers
    int i;
    int f_Num,s_Num;
    //2 two more variables created to store the sum of the divisors
    int f_DivisorSum = 0;
    int s_DivisorSum = 0;

    //3 Asking user to enter the two numbers
    printf(“Enter two numbers to check if Amicable or not : “);
    scanf(“%d %d”,&f_Num,&s_Num);

    //4 Using one variable for loop and second to check for each number
    for(int i=1;i<f_Num;i++)
    {
    //5 Condition check
    if(f_Num % i == 0)
    f_DivisorSum = f_DivisorSum + i;
    }
    //6 Calculating the sum of all divisors
    for(int i=1;i<s_Num;i++)
    {
    if(s_Num % i == 0)
    s_DivisorSum = s_DivisorSum + i;
    }
    int fai=(f_DivisorSum/f_Num);
    int sai=(s_DivisorSum/s_Num);
    //7 Check condition for friendly numbers
    if(fai==sai)
    {
    printf("%d and %d are Amicable numbers\n",f_Num,s_Num);
    }
    else
    {
    printf("%d and %d are not Amicable numbers\n",f_Num,s_Num);
    }
    return 0;
    }


    • nagini

      The smallest pair of amicable numbers is (220, 284). They are amicable because the sum of proper divisors of 220 (1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110) is 284; and the sum of proper divisors of 284 (1, 2, 4, 71 and 142) is 220.
      the above program is correct, except in the if statement …..after the if statement there should be printf function. that’s it.
      the above explanation is wrong, 6 and 28 are not Amaicable numbers.
      Amaicable numbers have the sum of the proper divisors of each is equal to the other number.