Find GCD and LCM

C++C++Beginner
Practice Now

Introduction

In Mathematics, GCD (Greatest Common Divisor) and LCM (Least Common Multiple) are useful concepts. The Greatest Common Divisor, also known as the greatest common factor, of two numbers is the largest number that is a factor of both numbers. Conversely, the Least Common Multiple of two or more integers is the smallest positive integer that is a multiple of all of them.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/SyntaxandStyleGroup(["`Syntax and Style`"]) cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp(("`C++`")) -.-> cpp/FunctionsGroup(["`Functions`"]) cpp/SyntaxandStyleGroup -.-> cpp/comments("`Comments`") cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/ControlFlowGroup -.-> cpp/conditions("`Conditions`") cpp/ControlFlowGroup -.-> cpp/for_loop("`For Loop`") cpp/FunctionsGroup -.-> cpp/function_parameters("`Function Parameters`") subgraph Lab Skills cpp/comments -.-> lab-96188{{"`Find GCD and LCM`"}} cpp/variables -.-> lab-96188{{"`Find GCD and LCM`"}} cpp/data_types -.-> lab-96188{{"`Find GCD and LCM`"}} cpp/operators -.-> lab-96188{{"`Find GCD and LCM`"}} cpp/conditions -.-> lab-96188{{"`Find GCD and LCM`"}} cpp/for_loop -.-> lab-96188{{"`Find GCD and LCM`"}} cpp/function_parameters -.-> lab-96188{{"`Find GCD and LCM`"}} end

Set Up Your Project Directory

First, create a new folder using the mkdir command, and set up a C++ project in the directory with the cd command. For example:

mkdir ~/project
cd ~/project

Create a New C++ File

Create a new file named main.cpp using your favorite text editor. For example:

touch main.cpp

Write the C++ Code

Copy and paste the following C++ code into your main.cpp file:

#include <iostream>
using namespace std;

int main()
{
    cout << "\n\nWelcome to the GCD and LCM calculator!\n\n\n";
    cout << "===== Program to find the GCD and LCM of two numbers =====\n\n";

    //variable declaration
    int n1, n2, i;

    //variable declaration and initialization
    int gcd = 1, lcm = 1;

    //taking input from the command line (user)
    cout << "Enter two numbers to find their GCD and LCM (separated by a space): \n\n";
    cin >> n1 >> n2;

    //logic to calculate the GCD and LCM of the two numbers
    for ( i = 1; i <= n1 && i <= n2; i++)
    {
        //i is the least value that perfectly divides both the numbers and hence the GCD
        if ((n1 % i == 0) && (n2 % i == 0))
        {
            gcd = i;
        }
    }

    lcm = (n1 * n2) / gcd;

    //print the GCD and LCM of the two numbers on the command line
    cout << "\nThe GCD of the two numbers " << n1 << " and " << n2 << " is : " << gcd << endl;
    cout << "\nThe LCM of the two numbers " << n1 << " and " << n2 << " is : " << lcm << endl;
    cout << "\nThanks for using GCD and LCM calculator!\n\n\n";

    return 0;
}

Compile and Run the Code

To compile and run the code, use the following command in the terminal:

g++ main.cpp -o main && ./main

Final Code

Here's the full code again, all in one place:

#include <iostream>
using namespace std;

int main()
{
    cout << "\n\nWelcome to the GCD and LCM calculator!\n\n\n";
    cout << "===== Program to find the GCD and LCM of two numbers =====\n\n";

    //variable declaration
    int n1, n2, i;

    //variable declaration and initialization
    int gcd = 1, lcm = 1;

    //taking input from the command line (user)
    cout << "Enter two numbers to find their GCD and LCM (separated by a space): \n\n";
    cin >> n1 >> n2;

    //logic to calculate the GCD and LCM of the two numbers
    for ( i = 1; i <= n1 && i <= n2; i++)
    {
        //i is the least value that perfectly divides both the numbers and hence the GCD
        if ((n1 % i == 0) && (n2 % i == 0))
        {
            gcd = i;
        }
    }

    lcm = (n1 * n2) / gcd;

    //print the GCD and LCM of the two numbers on the command line
    cout << "\nThe GCD of the two numbers " << n1 << " and " << n2 << " is : " << gcd << endl;
    cout << "\nThe LCM of the two numbers " << n1 << " and " << n2 << " is : " << lcm << endl;
    cout << "\nThanks for using the GCD and LCM calculator!\n\n\n";

    return 0;
}

Summary

Congratulations, you've learned C++ code to calculate the GCD and LCM of two numbers. With these skills, you can implement them in various applications.

Other C++ Tutorials you may like