Enter First Number : 220 Enter Second Number :284 They are a Pair Of Amicable Numbers
This is a C# Program to check whether the entered number is a amicable number or not.
This C# Program Checks Whether the Entered Number is a Amicable Number 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.
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(); } } } }
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.
Enter First Number : 220 Enter Second Number :284 They are a Pair Of Amicable Numbers