Write a C# program to find GCD and LCM?

CsharpProgrammingServer Side Programming

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 −


 Live Demo

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


Published on 16-Aug-2018 12:20:39