Calculate GCD and LCM of Two Numbers in C

 

Introduction

The Greatest Common Divisor (GCD) and Least Common Multiple (LCM) are important mathematical concepts used to solve various problems in number theory and other areas of mathematics.
The GCD of two numbers is the largest number that divides both of them, while the LCM is the smallest number that is a multiple of both numbers.

In this program, we will calculate both the GCD and LCM of two numbers provided by the user using C programming. We will employ the Euclidean algorithm for calculating the GCD, and once the GCD is known, we will compute the LCM using the formula:

LCM(a, b) = (a * b) / GCD(a, b)

Objective

The objective of this program is to:

  • Accept two integers as input from the user.
  • Compute the GCD using the Euclidean algorithm.
  • Compute the LCM based on the relationship between GCD and LCM.

Code

#include 

// Function to calculate GCD using Euclidean Algorithm
int gcd(int a, int b) {
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

// Function to calculate LCM
int lcm(int a, int b, int gcd_val) {
    return (a * b) / gcd_val;
}

int main() {
    int num1, num2;
    
    // Taking input from the user
    printf("Enter two positive integers: ");
    scanf("%d %d", &num1, &num2);
    
    // Ensure the user entered positive integers
    if (num1 <= 0 || num2 <= 0) {
        printf("Please enter positive integers.\n");
        return 1;
    }

    // Calculate GCD
    int gcd_val = gcd(num1, num2);
    
    // Calculate LCM
    int lcm_val = lcm(num1, num2, gcd_val);

    // Display results
    printf("GCD of %d and %d is: %d\n", num1, num2, gcd_val);
    printf("LCM of %d and %d is: %d\n", num1, num2, lcm_val);
    
    return 0;
}

Explanation

In the above program:

  • The function gcd(int a, int b) calculates the greatest common divisor using the Euclidean algorithm. It iteratively divides the larger number by the smaller number until the remainder is zero, with the last non-zero remainder being the GCD.
  • The function lcm(int a, int b, int gcd_val) calculates the least common multiple using the formula LCM = (a * b) / GCD(a, b).
  • In the main() function, we prompt the user to enter two integers. We then call the GCD function, followed by the LCM function, and print the results.

How to Run the Program

To run this program, follow these steps:

  1. Install a C compiler (such as GCC) on your system.
  2. Save the code in a file, for example, gcd_lcm.c.
  3. Open the terminal (or command prompt) and navigate to the directory where the file is saved.
  4. Compile the code by typing gcc -o gcd_lcm gcd_lcm.c in the terminal.
  5. Run the program by typing ./gcd_lcm on Linux/macOS or gcd_lcm.exe on Windows.
  6. Enter two positive integers when prompted to get the GCD and LCM of the numbers.
© 2024 Learn Programming. All Rights Reserved.

 

One Reply to “Calculate GCD and LCM of Two Numbers in C”

Leave a Reply

Your email address will not be published. Required fields are marked *