Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Write a C# program to find GCD and LCM?
In C#, finding the GCD (Greatest Common Divisor) and LCM (Least Common Multiple) of two numbers is a common mathematical programming task. The GCD is the largest positive integer that divides both numbers without remainder, while the LCM is the smallest positive integer that both numbers can divide evenly.
These calculations are frequently used in mathematical applications, fraction simplification, and solving problems involving ratios and proportions.
Mathematical Relationship
There's an important mathematical relationship between GCD and LCM −
GCD(a, b) × LCM(a, b) = a × b
This means once we find the GCD, we can easily calculate the LCM using the formula −
LCM(a, b) = (a × b) / GCD(a, b)
Using Euclidean Algorithm for GCD
The most efficient method to find GCD is the Euclidean algorithm, which uses the principle that GCD(a, b) = GCD(b, a mod b) −
Example
using System;
class Program {
static void Main(string[] args) {
int val1 = 10;
int val2 = 16;
int n1 = val1;
int n2 = val2;
int temp;
// Euclidean algorithm to find GCD
while (n2 != 0) {
temp = n2;
n2 = n1 % n2;
n1 = temp;
}
int gcd = n1;
int lcm = (val1 * val2) / gcd;
Console.WriteLine("Numbers: " + val1 + " and " + val2);
Console.WriteLine("GCD: " + gcd);
Console.WriteLine("LCM: " + lcm);
}
}
The output of the above code is −
Numbers: 10 and 16 GCD: 2 LCM: 80
Using Methods for Better Code Organization
Example
using System;
class MathOperations {
static int FindGCD(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
static int FindLCM(int a, int b) {
return (a * b) / FindGCD(a, b);
}
static void Main(string[] args) {
int num1 = 24;
int num2 = 36;
int gcd = FindGCD(num1, num2);
int lcm = FindLCM(num1, num2);
Console.WriteLine("First number: " + num1);
Console.WriteLine("Second number: " + num2);
Console.WriteLine("GCD: " + gcd);
Console.WriteLine("LCM: " + lcm);
Console.WriteLine("Verification: GCD × LCM = " + (gcd * lcm) + ", num1 × num2 = " + (num1 * num2));
}
}
The output of the above code is −
First number: 24 Second number: 36 GCD: 12 LCM: 72 Verification: GCD × LCM = 864, num1 × num2 = 864
Using Recursive Approach
Example
using System;
class RecursiveMath {
static int GCDRecursive(int a, int b) {
if (b == 0)
return a;
return GCDRecursive(b, a % b);
}
static int LCMRecursive(int a, int b) {
return (a * b) / GCDRecursive(a, b);
}
static void Main(string[] args) {
int x = 48;
int y = 18;
Console.WriteLine("Using recursive approach:");
Console.WriteLine("Numbers: " + x + " and " + y);
Console.WriteLine("GCD: " + GCDRecursive(x, y));
Console.WriteLine("LCM: " + LCMRecursive(x, y));
}
}
The output of the above code is −
Using recursive approach: Numbers: 48 and 18 GCD: 6 LCM: 144
Comparison of Approaches
| Approach | Time Complexity | Space Complexity | Advantages |
|---|---|---|---|
| Iterative (Euclidean) | O(log min(a,b)) | O(1) | Memory efficient, simple logic |
| Recursive | O(log min(a,b)) | O(log min(a,b)) | Cleaner code, easier to understand |
Conclusion
Finding GCD and LCM in C# can be efficiently accomplished using the Euclidean algorithm. The iterative approach is memory-efficient, while the recursive approach offers cleaner code. Both methods have the same time complexity and utilize the mathematical relationship LCM(a,b) = (a × b) / GCD(a,b).
