# C# program to Find GCD and LCM of Two Numbers

[14262 views]

### What is LCM?

LCM (Least common multiple) or LCD (lowest common denominator) is the smallest positive integer which is divisible by all numbers that is given to find LCM.

Example:

LCM of 8, 9, 21 is 504, Because 504 is the least common positive integer number which is divided by all given numbers i.e 8, 9, 21.

### What is GCD?

GCD (Greatest common divisor) or HCF (Highest common factor) of two numbers is the largest positive integer that divides the two numbers without any remainder.

Example:

GCD (8,12) is 4 because 4 is the highest number which divides both the numbers i.e 8 and 12 completely.

Problem Statement: Calculate the GCD and LCM of two given numbers using C#

### Code:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GCD_LCM { class Program { static void Main(string[] args) { Console.WriteLine( " #The GCD and LCM of Two Numbers#\n"); /* * Input 1st Number : * then Enter * Input 2nd Number : * PRESS SUBMIT */ int x = int.Parse(Console.ReadLine()); int y = int.Parse(Console.ReadLine()); Console.WriteLine( " The GCD of Two Numbers is : "+ gcd(x, y)); Console.WriteLine( " The LCM of Two Numbers is : "+ lcm(x, y)); } static int gcd(int x, int y) { int r=0, a, b; // a is greater number a = (x > y) ? x : y; // b is smaller number b = (x < y) ? x : y; r = b; while (a % b != 0) { r = a % b; a = b; b = r; } return r; } static int lcm(int x, int y) { int a; // a is greater number a = (x > y) ? x : y; while (true) { if (a % x == 0 && a % y == 0) return a; ++a; } } } }

##### Thinking of Buying Products Online??

Use Below Search to get Best Discounts Online