In this C# program, we will learn how to write a program to checks whether both the entered number is a pair of Amicable Number or Not.
Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number. In Other Words, A proper divisor of a number is a positive factor of that number other than the number itself. For example, the best possible divisors of 6 are 1, 2, and 3.
The smallest pair of amicable numbers is (220, 284). They are amicable because:
The first ten amicable pairs are: (220, 284), (1184, 1210), (2620, 2924), (5020, 5564), (6232, 6368), (10744, 10856), (12285, 14595), (17296, 18416), (63020, 76084), and (66928, 66992).
Here is the code of the program to checks whether both the entered number is a pair of Amicable Number or Not:
using System;
namespace TutorialsrackPrograms
{
class Program
{
//C# Program Checks Whether the Both the Entered Number is a Pair of Amicable Number or Not.
static void Main(string[] args)
{
int number1, number2, sum1 = 0, sum2 = 0, i;
Console.Write("Enter The First Number: ");
number1 = int.Parse(Console.ReadLine());
Console.Write("\nEnter The Second Number: ");
number2 = int.Parse(Console.ReadLine());
for (i = 1; i < number1; i++)
{
if (number1 % i == 0)
{
sum1 = sum1 + i;
}
}
for (i = 1; i < number2; i++)
{
if (number2 % i == 0)
{
sum2 = sum2 + i;
}
}
if (number1 == sum2 && number2 == sum1)
{
Console.WriteLine("Both Entered Numbers is a Pair of Amicable Numbers");
}
else
{
Console.WriteLine("Both Entered Numbers is not a pair of Amicable Numbers");
}
Console.Read();
}
}
}
Enter The First Number: 220
Enter The Second Number: 284
Both Entered Numbers is a Pair of Amicable Numbers
Comments