# C# Program to Check Whether the Given Number is a Amicable Number or Not

«
»

This is a C# Program to check whether the entered number is a amicable number or not.

Problem Description

This C# Program Checks Whether the Entered Number is a Amicable Number or Not.

Problem Solution

Amicable numbers are two numbers so related that the sum of the proper divisors of the one is equal to the other, unity being considered as a proper divisor but not the number itself.

Program/Source Code

Here is source code of the C# Program that Checks Whether the Entered Number is a Amicable Number or Not. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

```/*
* C# Program Checks Whether the Entered Number is a Amicable Number or Not
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Program
{
class Program
{
public static void Main(String[] args)
{
int num1, num2, sum1 = 0, sum2 = 0, i;
Console.WriteLine("Enter First Number : ");
Console.WriteLine("Enter Second Number : ");
for (i = 1; i < num1; i++)
{
if (num1 % i == 0)
{
sum1 = sum1 + i;
}
}
for (i = 1; i < num2; i++)
{
if (num2 % i == 0)
{
sum2 = sum2 + i;
}
}
if (num1 == sum2 && num2 == sum1)
{
Console.WriteLine("They are a Pair of Amicable Numbers");
}
else
{
Console.WriteLine("They are not Amicable Numbers");
}
}
}
}```
Program Explanation

This C# program we are reading the first and second numbers using ‘num1’ and ‘num2’ variables respectively. For loop is used to check that given number is amicable or not.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

Amicable numbers are two numbers so related that the sum of the proper divisors of the one is equal to the other, unity being considered as a proper divisor but not the number itself. If condition statement is used to check that the modulus of the value of ‘num1’ variable by the value of ‘i’ variable is equal to 0.

If the condition is true then execute the statement. Compute the summation of the value of ‘sum1’ variable with the value of ‘i’ variable. Again compute the summation of the value of ‘sum2’ variable with the value of ‘i’ variable. If else condition statement is used to check that the value of ‘num1’ variable is equal to the value of ‘sum2’ variable and the value of ‘num2’ variable is equal to the value of ‘sum1’ variable using logical AND operators.

If the condition is true then execute the statement and print the statement as amicable numbers. Otherwise, if the condition is false then execute the else statement and print the statement as not amicable numbers.

Runtime Test Cases
```
Enter First Number : 220
Enter Second Number :284
They are a Pair Of Amicable Numbers```

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.