Q:

C# program to find the Least Common Multiple of two numbers

belongs to collection: C# Basic Programs | basics

0

C# program to find the Least Common Multiple of two numbers

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to find the LCM of two specified numbers is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to find the LCM of two numbers.

using System;

class Demo
{
    static void Main()
    {
        int firstNumber=0;
        int secondNumber=0;

        int temp1=0;
        int temp2=0;

        int lcm=0;

        Console.Write("Enter the value of 1st number: ");
        firstNumber = Convert.ToInt32(Console.ReadLine());

        Console.Write("Enter the value of 2nd number: ");
        secondNumber = Convert.ToInt32(Console.ReadLine());

        temp1 = firstNumber;
        temp2 = secondNumber;

        while (firstNumber != secondNumber)
        {
            if (firstNumber > secondNumber)
            {
                firstNumber = firstNumber - secondNumber;
            }
            else
            {
                secondNumber = secondNumber - firstNumber;
            }
        }
        lcm = (temp1 * temp2) / firstNumber;

        Console.WriteLine("Least Common Multiple is : " + lcm);
    }
}

Output:

Enter the value of 1st number: 9
Enter the value of 2nd number: 15
Least Common Multiple is : 45
Press any key to continue . . .

Explanation:

Here, we created a class Demo that contains a Main() method. Here we found the LCM of two numbers. The LCM is the smallest number, which is the multiple of both numbers.

Console.Write("Enter the value of 1st number: ");
firstNumber = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the value of 2nd number: ");
secondNumber = Convert.ToInt32(Console.ReadLine());

temp1 = firstNumber;
temp2 = secondNumber;

while (firstNumber != secondNumber)
{
    if (firstNumber > secondNumber)
    {
        firstNumber = firstNumber - secondNumber;
    }
    else
    {
        secondNumber = secondNumber - firstNumber;
    }
}
lcm = (temp1 * temp2) / firstNumber;

In the above code, we read the value of numbers and find the LCM of both numbers and then print the result on the console screen.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | basics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to find the cube root of a given number... >>
<< C# program to print the absolute value of a number...