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.

advertisement
advertisement

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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Runtime Test Cases
 
Enter First Number : 220
Enter Second Number :284
They are a Pair Of Amicable Numbers

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.