C# program to Find GCD and LCM of Two Numbers


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.


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.


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#

Find GCD and LCM of given Number using C# language


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; } } } }

Solution Worked 0 UpvotesUpvote


Solution Didn't Worked 0 DownvotesDownvote



Play 2048 Game Online

Play Duckhunt Online

Need Help?

Looking for any Software or Tutorial?
Don't Worry, we will find it for you
Contact Now

Search Tags

    LCM and GCD of two Numbers in C#

    Find GCD and LCM of two Numbers in C#

    Program to Find HCF and LCM of 2 numbers in C#