**R Program to Find L.C.M.**

In this example, you will learn to find the L.C.M of two numbers entered by the user by creating a user-defined function. This program asks for two integers and passes them to a function which returns the L.C.M.

In the function, we first determine the greater of the two number since the L.C.M. can only be greater than or equal to the largest number.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | # Program to find the L.C.M. of two input number lcm <- function(x, y) { # choose the greater number if(x > y) { greater = x } else { greater = y } while(TRUE) { if((greater %% x == 0) && (greater %% y == 0)) { lcm = greater break } greater = greater + 1 } return(lcm) } # take input from the user num1 = as.integer(readline(prompt = "Enter first number: ")) num2 = as.integer(readline(prompt = "Enter second number: ")) print(paste("The L.C.M. of", num1,"and", num2,"is", lcm(num1, num2))) |

**Program Output**

1 2 3 | Enter first number: 24 Enter second number: 25 [1] "The L.C.M. of 24 and 25 is 600" |

If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.

- Sum of Natural Numbers Using Recursion
- r program for factorial using recursion
- R program to find L.C.M. with the output
- R program to take input from user with the output
- Convert decimal into binary using recursion in R
- Odd or Even program in R programming
- R program to check armstrong number
- R program to find the sum of natural numbers
- r practice problems for beginners
- R Program to Check for Leap Year with the output

We use cookies to ensure that we give you the best experience and use cookies in order to personalize the advertising that you see on our website. Read more about our Privacy Policy.Got it!