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,

Recommended Answers

All 2 Replies

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::cout << "\nPlease enter your first number: ";
	std::cin >> a;
	std::cout << "Please enter your second number: ";
	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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.