Write a C program to solve the Greatest Common Divisor, according to the brute-force

The output should appear as the following example:

1st number = 37
2nd number = 11
The GCD of 37 and 11 is 1.
Press any key to continue_

thank you,

3
Contributors
2
Replies
6
Views
8 Years
Discussion Span
Last Post by Nick Evan

Write a C program to solve the Greatest Common Divisor, according to the brute-force

The output should appear as the following example:

1st number = 37
2nd number = 11
The GCD of 37 and 11 is 1.
Press any key to continue_

thank you,

``````#include <iostream>

int main()
{
int a,b;

std::cout << "This is application solve for GCD between two numbers";
std::cin >> a;
std::cin >> b;

std::cout << "The first number is = " << a << std::endl;
std::cout << "The second number is = " << b << std::endl;

std::cin.clear();
std::cin.sync();

if(a>b)
{
if(a%b == 0)std::cout << "The GCD = " << b << std::endl;
else{
while(a%b!=0)
{
int c = a%b;
a = b;
b = c;
}
std::cout << "The GCD = " << a << std::endl;
}
}
if(a<b)
{
if( b%a == 0)std::cout << "The GCD = " << a << std::endl;
else{
while(b%a!=0)
{
int c = b%a;
b = a;
a = c;
}
std::cout << "The GCD = " << b << std::endl;
}
}
if(a == b)std::cout << "The GCD = " << a << std::endl;

std::cout << "Press any key to exit" << std::endl;

std::cin.get();
return 0;
}``````

by Weichen

Edited by Weichen: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.