Showing posts with label find the LCM of two number. Show all posts
Showing posts with label find the LCM of two number. Show all posts

Tuesday, June 7, 2016

Write program to find LCM of 2 numbers in C# .NET

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication15
{
    public class LCM
    {
        public static int dLCM(int a, int b)
        {
            int n1, n2;
            if (a > b)
            {
                n1 = a; n2 = b;
            }
            else
            {
                n1 = b; n2 = a;
            }
 
            for (int i = 1; i <= n2; i++)
            {
                if ((n1 * i) % n2 == 0)
                {
                    return i * n1;
                }
            }
            return n2;
        }
 
        public static void Main(String[] args)
        {
            int n01, n02;
 
            Console.WriteLine("Enter 2 numbers :");
 
            n01 = int.Parse(Console.ReadLine());
            n02 = int.Parse(Console.ReadLine());
 
            int l = dLCM(n01, n02);
 
            Console.WriteLine("LCM of {0} and {1} is {2}", n01, n02, l);
            Console.Read();
        }
    }

}

Output :