Introdução
Na matemática, GCD (Greatest Common Divisor) e LCM (Least Common Multiple) são conceitos úteis. O Maior Divisor Comum (Greatest Common Divisor), também conhecido como maior fator comum, de dois números é o maior número que é um fator de ambos os números. Por outro lado, o Mínimo Múltiplo Comum (Least Common Multiple) de dois ou mais inteiros é o menor inteiro positivo que é um múltiplo de todos eles.
Configure o Diretório do Seu Projeto
Primeiramente, crie uma nova pasta usando o comando mkdir e configure um projeto C++ no diretório com o comando cd. Por exemplo:
mkdir ~/project
cd ~/project
Crie um Novo Arquivo C++
Crie um novo arquivo chamado main.cpp usando seu editor de texto favorito. Por exemplo:
touch main.cpp
Escreva o Código C++
Copie e cole o seguinte código C++ no seu arquivo main.cpp:
#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 e Execute o Código
Para compilar e executar o código, use o seguinte comando no terminal:
g++ main.cpp -o main && ./main
Código Final
Aqui está o código completo novamente, tudo em um só lugar:
#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;
}
Resumo
Parabéns, você aprendeu código C++ para calcular o MDC (Máximo Divisor Comum) e o MMC (Mínimo Múltiplo Comum) de dois números. Com essas habilidades, você pode implementá-las em diversas aplicações.



