My intent for this article is to explain how to answer a question that is often asked in an interview, which is: Write a program to determine the LCM of two given numbers using C#.
So considering the preceding requirement and numerous emails sent to me from beginners I have decided to write this article with the basics and specially focusing on the beginner, student and whoever might face this type of question in an interview.
So let us start with the basics.
What is a LCM ?
LCM Stands for Lowest Common Multiple of two numbers.
First Number : 2 -- 2,4,6,8,10,12,14,16,18,20...
Second No : 3 -- 3,6,9,12,15,18,21,24,27,30...
Then the LCM of 2 and 3 is: 6.
In the example above you clearly see that two numbers (2 and 3) are used and I have multiplied them with their same range number that is 2,4,6..or 3,6,9.. etc. Then in the example above the Lowest Common Multiples between the numbers 2 and 3 are 6.
Now I hope that we completely understand what is meant by LCM, next I will explain how to do it step-by-step, as in the following.
Open Visual Studio from Start - - All programs -- Microsoft Visual Studio.
Then go to to "File" -> "New" -> "Project..." then select "Visual C#" -> "Windows" -> "Console application".
After that specify the name such as LCM two_numbers or whatever name you wish and the location of the project and click on the OK button. The new project is created.
And use the following code in the program.cs class file:
In the program above I have clearly explained in comments which statement is used for which purpose. Now run the program with the two integers 2 and 3 as input; then the output will be as follows:
From all the examples above I have explained how to determine the LCM of two numbers. I hope this article is useful for beginners and anyone preparing for an interview. If you have any suggestions and feedback then please contact me.