Introduction
The Least Common Multiple (LCM) of two numbers is the smallest positive number that is divisible by both numbers. This guide will walk you through writing an R program that calculates the LCM of two numbers using the relationship between the LCM and the GCD (Greatest Common Divisor).
Problem Statement
Create an R program that:
- Prompts the user to enter two numbers.
- Calculates the LCM of the two numbers.
- Displays the LCM.
Example:
- Input:
12and18 - Output:
LCM: 36
Solution Steps
- Read the Two Numbers: Use the
readline()function to take the two numbers as input from the user. - Convert the Inputs to Numeric: Convert the inputs from character strings to numeric values using the
as.numeric()function. - Define a Function to Calculate GCD: Implement the Euclidean algorithm to find the GCD.
- Define a Function to Calculate LCM: Use the relationship
LCM(a, b) = abs(a * b) / GCD(a, b)to calculate the LCM. - Call the LCM Function: Use the function to calculate the LCM of the two numbers.
- Display the LCM: Use the
print()function to display the result.
R Program
# R Program to Find the LCM of Two Numbers
# Author: https://www.javaguides.net/
# Step 1: Read the first number from the user
num1 <- as.numeric(readline(prompt = "Enter the first number: "))
# Step 2: Read the second number from the user
num2 <- as.numeric(readline(prompt = "Enter the second number: "))
# Step 3: Function to calculate GCD using Euclidean algorithm
gcd <- function(a, b) {
while (b != 0) {
temp <- b
b <- a %% b
a <- temp
}
return(a)
}
# Step 4: Function to calculate LCM using the GCD
lcm <- function(a, b) {
return(abs(a * b) / gcd(a, b))
}
# Step 5: Calculate the LCM of the two numbers
result <- lcm(num1, num2)
# Step 6: Display the LCM
print(paste("LCM:", result))
Explanation
Step 1: Read the First Number
- The
readline()function prompts the user to enter the first number. The input is read as a string, so it is converted to a numeric value usingas.numeric().
Step 2: Read the Second Number
- Similarly, the
readline()function is used to prompt the user for the second number, which is also converted to a numeric value.
Step 3: Define the GCD Function
- The
gcd()function implements the Euclidean algorithm to calculate the Greatest Common Divisor (GCD) of two numbers.
Step 4: Define the LCM Function
- The
lcm()function calculates the Least Common Multiple (LCM) using the formulaLCM(a, b) = abs(a * b) / GCD(a, b).
Step 5: Calculate the LCM
- The program calls the
lcm()function with the two input numbers and stores the result in theresultvariable.
Step 6: Display the LCM
- The
print()function is used to display the calculated LCM.
Output Example
Example:
Enter the first number: 12
Enter the second number: 18
[1] "LCM: 36"
Conclusion
This R program demonstrates how to calculate the LCM of two numbers using the relationship between the LCM and the GCD. It covers essential programming concepts such as functions, arithmetic operations, and user input, making it a useful example for beginners learning R programming.