Users Online

· Guests Online: 45

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C# Program to Find LCM of Two Numbers

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.

 

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

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.74 seconds
10,846,031 unique visits