LCM Finder Application in C# 4.0

Introduction

In my previous article I described how to determine the HCF of two numbers through C# code and in this article I am going to describe how to determine the LCM of two numbers in C#. Basically LCM is a term of mathematics and we have all read about this in an earlier class. But finding it in programing is a little complicated.

What is LCM

The Least Common Multiple (LCM) of some numbers is the smallest number that the numbers are factors of. Like the LCM of 3 and 4 is 12, because 12 is the smallest number that 3 and 4 are both factors.

C# code to determine LCM

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace Lcm_finder

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        int lcm;

        static int temp=1;

        private void button1_Click(object sender, EventArgs e)

        {

            int num = int.Parse(textBox1.Text);

            int num1 = int.Parse(textBox2.Text);

            if (num > num1)

                lcm = lcmfinder(num, num1);

            else

                lcm = lcmfinder(num1, num);

            label1.Text = lcm.ToString();

        }

        int lcmfinder(int x, int y)

        {

          

            while (x != y)

            {

                if (temp %y==0 && temp %x ==0)

                {

                    return temp ;

                }

                    temp++;

                lcmfinder (x,y);

            }

            return temp ;

        }

     }

}

 

Output

lcm.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all