Home » .Net » C# programs

# C# program to check given numbers are the pair of amicable numbers or not

Here, we are going to learn **how to check given numbers are the pair of amicable numbers or not in C#?**

Submitted by Nidhi, on September 21, 2020

Here we will enter two integer numbers from the keyboard and then checked the entered numbers are amicable or not.

**Amicable numbers:**

Amicable numbers are pair of two numbers; here some of the proper divisors of both numbers are equal. The same two numbers are not considered as amicable.

**Program:**

The source code to check given numbers are the pair of amicable numbers or not is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to check given numbers are //the pair of amicable numbers or not. using System; class Demo { static bool IsAmicable(int number1, int number2) { int sum1 = 0; int sum2 = 0; int X = 0; for (X = 1; X < number1; X++) { if (number1 % X == 0) { sum1 = sum1 + X; } } for (X = 1; X < number2; X++) { if (number2 % X == 0) { sum2 = sum2 + X; } } if (number1 == sum2 && number2 == sum1) return true; return false; } static void Main(string[] args) { int number1=0; int number2=0; Console.Write("Enter 1st Number : "); number1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter 2nd Number : "); number2 = Convert.ToInt32(Console.ReadLine()); if (IsAmicable(number1, number2)) Console.WriteLine("Numbers are the pair of Amicable numbers"); else Console.WriteLine("Numbers are not the pair of Amicable numbers"); } }

**Output:**

Enter 1st Number : 220 Enter 2nd Number : 284 Numbers are the pair of Amicable numbers Press any key to continue . . .

**Explanation:**

Here, we created a class *Demo* that contains two methods *IsAmicable()* and *Main()*. In the *IsAmicable()*, we checked amicable numbers from two numbers.

**Amicable numbers** are pair of two numbers; here some of the proper divisors of both numbers are equal. The same two numbers are not considered as amicable.

In the *Main()* method, we read the values of two integer numbers and then checked pair of **amicable numbers**. Then printed the corresponding message according to the return value of the *IsAmicable()* method on the console screen.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.