### C# Basic

### C# Interview Programs

- C# Interview Programs
- C# Abundant Number
- C# Amicable Number
- C# Armstrong Number
- C# Average of N Numbers
- C# Automorphic Number
- C# Biggest of three numbers
- C# Binary to Decimal
- C# Common Divisors
- C# Composite Number
- C# Condense a Number
- C# Cube Number
- C# Decimal to Binary
- C# Decimal to Octal
- C# Disarium Number
- C# Even Number
- C# Evil Number
- C# Factorial of a Number
- C# Fibonacci Series
- C# GCD
- C# Happy Number
- C# Harshad Number
- C# LCM
- C# Leap Year
- C# Magic Number
- C# Matrix Addition
- C# Matrix Division
- C# Matrix Multiplication
- C# Matrix Subtraction
- C# Matrix Transpose
- C# Maximum Value of an Array
- C# Minimum Value of an Array
- C# Multiplication Table
- C# Natural Number
- C# Number Combination
- C# Odd Number
- C# Palindrome Number
- C# Pascalβs Triangle
- C# Perfect Number
- C# Perfect Square
- C# Power of 2
- C# Power of 3
- C# Pronic Number
- C# Prime Factor
- C# Prime Number
- C# Smith Number
- C# Strong Number
- C# Sum of Array
- C# Sum of Digits
- C# Swap Two Numbers
- C# Triangular Number

# C# Program to Check Amicable Number

Photo Credit to CodeToFun

## π Introduction

In the realm of programming, exploring number properties is a fascinating journey. One interesting concept is that of amicable numbers.

Amicable numbers are two numbers that share a unique relationship: the sum of the proper divisors of each number is equal to the other number.

In simpler terms, the sum of the divisors of one number is the other number itself.

In this tutorial, we'll explore a C# program designed to check whether two given numbers are amicable or not.

The program will identify and compare the sum of proper divisors to determine if the numbers form an amicable pair.

## π Example

Let's take a look at the C# code that accomplishes this task.

```
using System;
class Program {
// Function to calculate the sum of proper divisors of a number
static int SumOfDivisors(int num) {
int sum = 1; // Start with 1 as every number is divisible by 1
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0) {
sum += i;
if (i != num / i) {
sum += num / i;
}
}
}
return sum;
}
// Function to check if two numbers are amicable
static bool AreAmicable(int num1, int num2) {
return SumOfDivisors(num1) == num2 && SumOfDivisors(num2) == num1;
}
// Driver program
static void Main() {
// Replace these values with your desired numbers
int number1 = 220;
int number2 = 284;
// Check if the numbers are amicable
if (AreAmicable(number1, number2)) {
Console.WriteLine($"{number1} and {number2} are amicable numbers.");
} else {
Console.WriteLine($"{number1} and {number2} are not amicable numbers.");
}
}
}
```

### π» Testing the Program

To test the program with different numbers, replace the values of number1 and number2 in the Main method.

220 and 284 are amicable numbers.

Compile and run the program to check if the numbers form an amicable pair.

### π§ How the Program Works

- The program defines a class Program containing static methods SumOfDivisors and AreAmicable to calculate the sum of proper divisors and check if two numbers are amicable, respectively.
- The Main method tests the amicability of two numbers and prints the result.

## π§ Understanding the Concept of Amicable Numbers

Amicable numbers exhibit a unique relationship where the sum of the proper divisors of each number is equal to the other number.

For example, the pair (220, 284) is amicable because the sum of divisors of 220 is 284, and the sum of divisors of 284 is 220.

## π’ Optimizing the Program

While the provided program is effective, you may explore optimizations such as caching the results of the sum of divisors to enhance performance for repeated calculations.

Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!

## π¨βπ» Join our Community:

## Author

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee
If you have any doubts regarding this article (

C# Program to Check Amicable Number), please comment here. I will help you immediately.