C# Program to Find LCM of Two Numbers

This is a C# Program to find lcm.

Problem Description

This C# Program Finds and Display the L.C.M of a Given Number.

Problem Solution

The least common multiple (LCM) of two numbers is the smallest number that is a multiple of both.

Program/Source Code

Here is source code of the C# Program to Find and Display the L.C.M of a Given Number. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Find and Display the L.C.M of a Given Number 
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication9
{
    class Program
    {
        public static void Main(string[] args)
        {
            int num1, num2, x, y, lcm = 0;
            Console.Write("Enter the First Number : ");
            num1 = int.Parse(Console.ReadLine());
            Console.Write("Enter the Second Number : ");
            num2 = int.Parse(Console.ReadLine());
            x = num1;
            y = num2;
            while (num1 != num2)
            {
                if (num1 > num2)
                {
                    num1 = num1 - num2;
                }
                else
                {
                    num2 = num2 - num1;
                }
            }
            lcm = (x * y) / num1;
            Console.Write("Least Common Multiple is : " + lcm);
            Console.Read();
        }
    }
}
Program Explanation

In this C# program, we are reading the First Number, Second Number using ‘num1’ and ‘num2’ variables respectively. Using while loop checks the value of ‘num1’ variable is greater than the value of ‘num2’ variable. If the condition is true then execute the statement.

advertisement
advertisement

If else condition statement is used to check the value of ‘num1’ variable is greater than the value of ‘num2’ variable. If the condition is true, then execute the statement. Compute the difference between the value of ‘num1’ and ‘num2’ variables.

Otherwise, if the condition is false, then execute the else statement. Compute the difference between the value of ‘num2’ variable by the value of ‘num1’ variable. Multiply the value of ‘x’ variable with the value of ‘y’ variable. Divide the resulted value by the value of ‘num1’ variable. Print the LCM of the number.

Runtime Test Cases
 
Enter the First Number : 2
Enter the Second Number : 4
Least Common Multiple : 4

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.