Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Determine the LCM of two numbers

C# Sharp For Loop: Exercise-45 with Solution

Write a program in C# Sharp to find LCM of any two numbers.

Pictorial Presentation:

C# Sharp Exercises: Determine the LCM of two numbers

Sample Solution:

C# Sharp Code:

using System;  
public class Exercise45
{  
    public static void Main() 
{  
    int i, n1, n2, max, lcm=1;  
	Console.Write("\n\n");
    Console.Write("Determine the LCM of two numbers:\n");
    Console.Write("-----------------------------------");
    Console.Write("\n\n");	
    Console.Write("Input 1st number for LCM: ");  
    n1 = Convert.ToInt32(Console.ReadLine());	
    Console.Write("Input 2nd number for LCM: ");  
    n2 = Convert.ToInt32(Console.ReadLine());	
    max = (n1>n2) ? n1 : n2;  
    for(i=max;  ; i+=max)  
    {  
        if(i%n1==0 && i%n2==0)  
        {  
            lcm = i;  
            break;  
        }  
    }  
    Console.Write("\nLCM of {0} and {1} = {2}\n\n", n1, n2, lcm);  
 }
}

Sample Output:

Determine the LCM of two numbers:                                                                           
-----------------------------------                                                                         
Input 1st number for LCM: 6                                                                                 
Input 2nd number for LCM: 8                                                                                 
LCM of 6 and 8 = 24

Flowchart:

Flowchart : Determine the LCM of two numbers using HCF

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to find LCM of any two numbers using HCF.
Next: Write a program in C# Sharp to convert a binary number into a decimal number using math function.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.