Returns the least common multiple of two numbers - CSharp System

CSharp examples for System:Math Number

Description

Returns the least common multiple of two numbers

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*from  www. j  av a2s .  c  om*/

public class Main{
        /// <summary>
        /// Returns the least common multiple of two numbers
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static int LCM(int a, int b)
        {
            return Math.Abs(a * b) / GCD(a, b);
        }

        /// <summary>
        /// Returns the greatest common divisor of two numbers
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static int GCD(int a, int b)
        {
            if (b == 0) return a;
            else return GCD(b, a % b);
        }
}

Related Tutorials