In this WSQ I had to write a code in which I could use the already known Euclids Algorithm to find the Greatest Common Divisior of two numbers that will be asked to the user.

Euclids Algorithm:

The Euclidean algorithm is based on the principle that the greatest common divisor of two numbers does not change if the larger number is replaced by its difference with the smaller number. For example, 21 is the GCD of 252 and 105 (252 = 21 × 12 and 105 = 21 × 5), and the same number 21 is also the GCD of 105 and 147 = 252 − 105.

Here is the code of the program.

#include <iostream>
using namespace std;

int gcd(int a, int b){//Making the function
	int g=a;
	int h,l;
	int f=2;
	if(a==0){
		return a;
	}
		while(g>0){//Doing a loop
		if(g==0){
			goto exit;
		}
		g=a%b;
		a=b;
		b=g;
		}
	if (l=1000){
	exit:
	g=a;
	}
	return g;
}

int main(){
int a, b;
cout<<"Please introduce the first number"<<endl;
cin>>a;
cout<<"Please introduce the second number"<<endl;
cin>>b;
cout<<endl;

cout<<"Your Greatest Common Divisor is: "<<gcd(a,b)<<endl;

return 0;
}

 

Also, Here is the link to the code in Github: BATMAN.

And of course, as always, the picture of the execution of the program on Cygwin.

Euclids execution

-The Admin.

 

CC BY 4.0 Euclids Algorithm by esaupreciado is licensed under a Creative Commons Attribution 4.0 International License.