C# program to find Amicable numbers in two ways

Amicable numbers:

Two numbers are called Amicable numbers if the sum of factors of one number is equal to the other number. For example, 220 and 284 are Amicable numbers. Let me show you how these numbers are Amicable.

Factors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, and 110. The sum of these factors are 1 + 2 + 4 + 5 + 10 + 11 + 20 + 22 + 44 + 55 + 110 = 284

Similarly, the factors of 284 are 1, 2, 4, 71, and 142. The sum of these factors are 1 + 2 + 4 + 71 + 142 = 220

So, the sum of factors of one number is equal to the other. Hence, 220 and 284 are Amicable numbers.

C# program to find if two numbers are Amicable numbers or not:

To find if two given numbers are Amicable or not, we will have to find the factors of each number. Once the factors are found, we can find the sum of the factors. If the sum of the factors of one number is equal to the other number, we can print that these are Amicable numbers.

The program will use the following algorithm:

  • Take the numbers as inputs from the user.
  • Initialize two variables as 0 to hold the sum of factors of the numbers.
  • Run one loop from i = 1 to half of the first number. On each iteration, check if i can divide the number or not. If yes, add the value of i to the first sum variable. Similarly, use one more loop to find the sum of factors for the second number. The sum will be stored in the second sum variable for the second number.
  • Compare the first sum value with the second number and the second sum value with the first number.
  • If both comparisons are equal, print that the numbers are Amicable numbers. Else, print that the numbers are not Amicable numbers.

Below is the complete C# program:

using System;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            int First, Second, FirstSum = 0, SecondSum = 0;

            Console.WriteLine("Enter the first number: ");
            First = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter the second number: ");
            Second = Convert.ToInt32(Console.ReadLine());
            
            for(int i = 1; i <= First/2; i++){
                if(First % i == 0){
                    FirstSum += i;
                }
            }

            for(int i = 1; i <= Second/2; i++){
                if(Second % i == 0){
                    SecondSum += i;
                }
            }

            if(FirstSum == Second && SecondSum == First){
                Console.WriteLine("These are Amicable numbers.");
            }else{
                Console.WriteLine("These are not Amicable numbers.");
            }
        }
    }
}

Here,

  • The integer variables First and Second are used to hold the first and the second numbers. Similarly, the integer variables FirstSum and SecondSum are used to hold the sum of factors of the first and the second number. The sum variables are initialized as 0.
  • It reads the numbers as inputs from the user. We are using Convert.ToInt32 to convert the user input value to integer.
  • The for loops are used to find the sum of all factors of each number. Each loop runs from i = 1 to half of the number. On each iteration, it checks if the number is divisible by the current i value of not. If yes, i.e. if it is a factor of the number, it adds this value to the sum variable.
  • It compares the sum variables to the numbers. If the sum for the first number is equal to the second number and vice versa, it will print that these are Amicable number. Else, it will print that these are not Amicable numbers.

If you run this program, it will print output as below:

Enter the first number: 
220
Enter the second number: 
284
These are Amicable numbers.

Enter the first number: 
220
Enter the second number: 
100
These are not Amicable numbers.

C# Amicable numbers example

C# program to find Amicable numbers by using while loops:

Instead of for loops, we can also use while loops to find Amicable numbers. It will work in a similar way. Let me change the above program to use while loops to find Amicable numbers:

using System;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            int First, Second, FirstSum = 0, SecondSum = 0;

            Console.WriteLine("Enter the first number: ");
            First = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter the second number: ");
            Second = Convert.ToInt32(Console.ReadLine());
            
            int i = 1;
            while(i <= First/2){
                if(First % i == 0){
                    FirstSum += i;
                }
                i++;
            }

            i = 1;
            while(i <= Second/2){
                if(Second % i == 0){
                    SecondSum += i;
                }
                i++;
            }

            if(FirstSum == Second && SecondSum == First){
                Console.WriteLine("These are Amicable numbers.");
            }else{
                Console.WriteLine("These are not Amicable numbers.");
            }
        }
    }
}

In this example program, we changed the for loops to while loops. The functionality of the program is not changed. It is finding the sum of factors. If you run this program, it will give similar results.

Enter the first number: 
223
Enter the second number: 
211
These are not Amicable numbers.

You might also like: