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.

 

Here, in this section we will discuss the C program to check whether two numbers are friendly pair or not. Friendly pair or Amicable numbers are two different numbers related in a way such that the sum of the proper divisors of each is equal to the other number. A proper divisor is a positive factor of that number other than the number itself ,for Example proper divisors of 6 are 1, 2, and 3.

Friendly pair(Amicable Number) or not

Working:-

Step 1. Start

Step 2. Input the numbers 1 and 2.

Step 3. Initialize two variables, sum1 and sum 2 with zero.

Step 4. Assign sum 1 with the sum of all the divisors of number 1.

Step 5.  Assign sum 2 with the sum of all the divisors of number 2.

Step 6. If (sum 1==number1) and (sum 2==number 2), then print, “Friendly Numbers”

Step 7. Else print “Not Friendly Numbers”.

Step 8. Stop

Competitive Coding Techniques

C Program to test a friendly pair

#include<stdio.h> 
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;
}
//7 Check condition for friendly numbers
if((f_Num == s_DivisorSum) && (s_Num == f_DivisorSum))

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;
}

Output

Enter two numbers to check if Amicable or not : 12 13
12 and 13 are not Amicable numbers

One comment on “C Program to check if the given two numbers are friendly pair or 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;
    }