Q:

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

0

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

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

All Answers

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

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);  
 }
}

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

total answers (1)

Similar questions


need a help?


find thousands of online teachers now