Check If Two Numbers are Friendly Pair or Not in C Program
ADVERTISEMENTS
Check if two numbers are friendly pair or not in c program.
In this article, you will learn how to check whether or not numbers are friendly with each other.
Example-1
INPUT FIRST NUMBER: 6
INPUT SECOND NUMBER: 28
6 & 28 = FRIENDLY PAIRS
Example-2
INPUT FIRST NUMBER: 30
INPUT SECOND NUMBER: 140
30 & 140 = FRIENDLY PAIRS
What are the friendly numbers?
Take two input values 6
& 28
- Divisors of 6 are
1, 2, 3, 6
- Divisor of 28 are
1, 2, 4, 7, 14, 28
(1 + 2 + 3 + 6)
/6
=2
, and(1 + 2 + 4 + 7 + 14 + 28)
/28
=2
2 == 2
- It means 6 & 28 are friendly with each other
There are 4 ways to solve this problem:
- Using While loop
- Using Do-while loop
- Using For loop
- Using Function
You should know about the following topics in C programming to understand this program:
- If-else statement
- While loop in C
- Do-while loop in C
- For loop in C
- C Functions
1 - Friendly Pair or Not in C Program using For loop
// Check If Two Numbers are Friendly Pair or Not in C using For loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int num_1, num_2, sum_1 = 0, sum_2 = 0;
int i;
// Step-1 Take two input values from the User
printf("INPUT FIRST NUMBER: ");
scanf("%d", &num_1);
printf("INPUT SECOND NUMBER: ");
scanf("%d", &num_2);
// Step-2 Calculate the divisors sum of the first number
for (i = 1; i < num_1; i++)
{
if (num_1 % i == 0)
sum_1 += i;
}
// Step-3 Calculate the divisors sum of the second number
for (i = 1; i < num_2; i++)
{
if (num_2 % i == 0)
sum_2 += i;
}
// Step-4 Compare the first number with the divisor num divided by the first number with the same process as the second number
if (sum_1 / num_1 == sum_2 / num_2)
printf("\n%d & %d = FRIENDLY PAIRS\n", num_1, num_2);
else
printf("\n%d & %d = NOT FRIENDLY PAIRS\n", num_1, num_2);
return 0;
}
Output
INPUT FIRST NUMBER: 6
INPUT SECOND NUMBER: 28
6 & 28 = FRIENDLY PAIRS
2 - Friendly Pair or Not in C Program using While loop
// Check If Two Numbers are Friendly Pair or Not in C using While loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int num_1, num_2, sum_1 = 0, sum_2 = 0;
int i;
// Step-1 Take two input values from the User
printf("INPUT FIRST NUMBER: ");
scanf("%d", &num_1);
printf("INPUT SECOND NUMBER: ");
scanf("%d", &num_2);
// Step-2 Calculate the divisors sum of the first number
i = 1;
while (i < num_1)
{
if (num_1 % i == 0)
sum_1 += i;
i++;
}
// Step-3 Calculate the divisors sum of the second number
i = 1;
while (i < num_2)
{
if (num_2 % i == 0)
sum_2 += i;
i++;
}
// Step-4 Compare the first number with the divisor num divided by the first number with the same process as the second number
if (sum_1 / num_1 == sum_2 / num_2)
printf("\n%d & %d = FRIENDLY PAIRS\n", num_1, num_2);
else
printf("\n%d & %d = NOT FRIENDLY PAIRS\n", num_1, num_2);
return 0;
}
Output
INPUT FIRST NUMBER: 30
INPUT SECOND NUMBER: 140
30 & 140 = FRIENDLY PAIRS
3 - Friendly Pair or Not in C Program using Do-while loop
// Check If Two Numbers are Friendly Pair or Not in C using Do-while loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int num_1, num_2, sum_1 = 0, sum_2 = 0;
int i;
// Step-1 Take two input values from the User
printf("INPUT FIRST NUMBER: ");
scanf("%d", &num_1);
printf("INPUT SECOND NUMBER: ");
scanf("%d", &num_2);
// Step-2 Calculate the divisors sum of the first number
i = 1;
do
{
if (num_1 % i == 0)
sum_1 += i;
i++;
} while (i < num_1);
// Step-3 Calculate the divisors sum of the second number
i = 1;
do
{
if (num_2 % i == 0)
sum_2 += i;
i++;
} while (i < num_2);
// Step-4 Compare the first number with the divisor num divided by the first number with the same process as the second number
if (sum_1 / num_1 == sum_2 / num_2)
printf("\n%d & %d = FRIENDLY PAIRS\n", num_1, num_2);
else
printf("\n%d & %d = NOT FRIENDLY PAIRS\n", num_1, num_2);
return 0;
}
4 -Friendly Pair or Not in C Program using Function
// Check If Two Numbers are Friendly Pair or Not in C using Function
#include <stdio.h>
// Function to get the sum of divisors
int get_divisors_sum(int number)
{
int sum = 0;
for (int i = 1; i < number; i++)
{
if (number % i == 0)
sum += i;
}
return sum;
}
// Function to calculate and check whether the friendly or not
int check_friendly_or_not(int num_1, int num_2)
{
int sum_1 = get_divisors_sum(num_1);
int sum_2 = get_divisors_sum(num_2);
// Compare the first number with the divisor num divided by the first number with the same process as the second number
if (sum_1 / num_1 == sum_2 / num_2)
printf("\n%d & %d = FRIENDLY PAIRS\n", num_1, num_2);
else
printf("\n%d & %d = NOT FRIENDLY PAIRS\n", num_1, num_2);
return 0;
}
// It's the main function of the program
int main()
{
int num_1, num_2;
// Step-1 Take two input values from the User
printf("INPUT FIRST NUMBER: ");
scanf("%d", &num_1);
printf("INPUT SECOND NUMBER: ");
scanf("%d", &num_2);
// Step-2 Call the check_friendly_or_not(); function & print the final output of the program
check_friendly_or_not(num_1, num_2);
return 0;
}
Output
INPUT FIRST NUMBER: 80
INPUT SECOND NUMBER: 200
80 & 200 = FRIENDLY PAIRS