# LCM Of Two Numbers Java Program | 5 Ways – Programs

Java program to find the LCM of two numbers – In the below-mentioned java programs, we discuss the various methods to evaluate the LCM of the two given numbers such as using Static Method, Command Line Arguments and Recursion. We also have added the compiler to each and every program along with sample outputs with specific examples.

What is LCM:

Least Common Multiplier or LCM of any n numbers is the least value that is a multiple of the given numbers.

For example:

Let us take three numbers such as 12, 15 and 18.

• 12 = 2 * 2 * 3
• 15 = 3 * 5
• 18 = 2 * 3 * 3

So we take the common prime factors and then the rest of the ones and multiply them with each other which would give us the LCM.

LCM = 2 * 2 * 3 * 3 * 5 = 180 ## Using Static Method

1) Read the values a,b using scanner object as sc.nextInt() and store these values in the variables a,b. In this program called the static method lcmCalculation(a,b) in the main method, lcmCalculation(a,b) will calculate the lcm of two numbers.

2) The a,b values passed to n1 , n2 ,then checks the if condition ,n1>n2 then res=n1 otherwise res=n2. temp initialized to res, until the condition (res%n1!=0 || res%n2!=0) is false ,it calculates the res=temp*i;, increases the i value by 1, returns the result value.

Output:

### LCM Of Two Numbers – Using Command Line Arguments

1) The values we will pass at run-time are called command line arguments, will store at string array “String args[]” of the main method.

2) We are converting the string present at the args, in to long using the wrapper class “Long”, and method parseLong() and the value will store it in the variable n1.

3) We are converting the string present at the args, in to long using the wrapper class “Long”, and method parseLong() and the value will store it in the variable n2.

4) Checks the if condition if n1>n2 then lcm=n1 otherwise lcm=n2 and b assigned with lcm, while loop will calculate the lcm=b*i until the condition is false.

Output:

Output:

#### Using Recursion Method

1) In this program, lcmCal(long n1,long n2,long temp,long res) method calls itself as lcmCal(n1,n2,temp,res), repeats until if condition is false ,then returns the value res.

Output:
{"ft_lang_label":"__label__en","ft_lang_prob":0.5889375,"math_prob":0.993768,"size":4902,"snap":"2022-27-2022-33","text_gpt3_token_len":1640,"char_repetition_ratio":0.121478155,"word_repetition_ratio":0.31897557,"special_character_ratio":0.37780496,"punctuation_ratio":0.16583748,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9966622,"pos_list":[0,1,2],"im_url_duplicate_count":[null,4,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-07-01T22:44:42Z\",\"WARC-Record-ID\":\"<urn:uuid:95836412-5b82-4f41-884c-c7732149cc9c>\",\"Content-Length\":\"202243\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:428b2195-2ff9-4220-a359-b7adb4323a13>\",\"WARC-Concurrent-To\":\"<urn:uuid:7ed175e2-fe33-42e4-b1c9-cfe3c220ca6d>\",\"WARC-IP-Address\":\"139.59.82.24\",\"WARC-Target-URI\":\"https://javatutoring.com/java-program-lcm-of-two-numbers/\",\"WARC-Payload-Digest\":\"sha1:VJWNUBYGXPPKIJZHVD7BBQFN3AIWTL4B\",\"WARC-Block-Digest\":\"sha1:B6ZPLYW7OJAQ7J7VQW3RSMUB3P4TF4IU\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103947269.55_warc_CC-MAIN-20220701220150-20220702010150-00175.warc.gz\"}"}