
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Write a C# program to find GCD and LCM?
GCD (Greatest Common Divisor)
GCD is the largest positive integer that divides each of the integers.
LCM (Least Common Multiple)
LCM of two numbers is the smallest integer divisible by both the numbers.
The following is an example to calculate the GCD and LCM. Here, we are calculating the LCM and GCD of 10 and 16 −
Example
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Demo { class Program { static void Main(string[] args) { int val1, val2, n1, n2, x; int resLCM, resGCD; val1 = 10; val2 = 16; n1 = val1; n2 = val2; while (n2 != 0) { x = n2; n2 = n1 % n2; n1 = x; } resGCD = n1; resLCM = (val1 * val2) / resGCD; Console.WriteLine("LCM: ", val1, val2, resLCM); Console.WriteLine("GCD: ", val1, val2, resGCD); Console.ReadKey(); } } }
Output
LCM: GCD:
- Related Articles
- C++ Program to Find the GCD and LCM of n Numbers
- Find any pair with given GCD and LCM in C++
- GCD and LCM of two numbers in Java
- C++ Program to Find GCD
- C++ Program to Find LCM
- Java Program to Find GCD of two Numbers
- Swift Program to Find GCD of two Numbers
- Kotlin Program to Find GCD of two Numbers
- Minimum LCM and GCD possible among all possible sub-arrays in C++
- Queries to update a given index and find gcd in range in C++ Program
- Haskell program to find the gcd of two numbers
- Java Program to Find LCM of two Numbers
- Swift Program to Find LCM of two Numbers
- Haskell program to find lcm of two numbers
- Kotlin Program to Find LCM of two Numbers

Advertisements