C# Program to Check Whether the Given Number is a Amicable Number or Not
Posted by Superadmin on August 11 2022 08:21:52

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 : ");
            num1 = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter Second Number : ");
            num2 = int.Parse(Console.ReadLine());
            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");
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("They are not Amicable Numbers");
                Console.ReadLine();
            }
        }
    }
}
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.

 

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