Go Program to Find the LCM of Two Numbers

1. Introduction

The Least Common Multiple (LCM) of two integers is the smallest integer that is divisible by both numbers without leaving a remainder. Finding the LCM is crucial in various mathematical computations, especially in problems related to fractions. In this guide, we'll walk you through the process of constructing a Go program that determines the LCM of two specified numbers.

2. Program Overview

Our Go program is structured to:

1. Elicit two numbers from the user.

2. Calculate the LCM of the provided numbers.

3. Display the resulting LCM to the user.

3. Code Program

// We commence our Go journey with the declaration of the main package.
package main

// We use the fmt package to coordinate input and output operations.
import "fmt"

// Function to compute the Greatest Common Divisor (GCD) using the Euclidean algorithm.
func findGCD(a, b int) int {
    for b != 0 {
        a, b = b, a%b
    return a

// Function to compute the LCM.
func findLCM(a, b int) int {
    return (a * b) / findGCD(a, b)

// The epicenter of our program is the main function.
func main() {
    var num1, num2 int

    // We prompt the user to enter two numbers.
    fmt.Print("Enter the first number: ")
    fmt.Print("Enter the second number: ")

    // The LCM of the numbers is then computed and presented.
    fmt.Printf("The LCM of %d and %d is: %d\n", num1, num2, findLCM(num1, num2))


As an illustration, if a user provides the numbers 12 and 15, the output of the program will be:
The LCM of 12 and 15 is: 60

4. Step By Step Explanation

1. Package and Import Declarations: Our journey starts with the package main statement. The fmt package is indispensable for performing I/O tasks.

2. GCD Calculation Function: Before determining the LCM, we first need to calculate the GCD. The findGCD function does precisely that, using the tried-and-true Euclidean algorithm.

3. LCM Calculation Function: The findLCM function uses a straightforward formula to find the LCM of two numbers. The formula is a multiplication of the two numbers divided by their GCD.

4. Variable Initialization and User Input: We define two integer variables, num1 and num2, to capture the user's input. Subsequently, the user is prompted to provide the two numbers.

5. LCM Computation and Display: The LCM of the provided numbers is calculated using the findLCM function and then displayed to the user.