PROGRAMMING:C programming experiment 4-3 Euclidean algorithm for the greatest common divisor
The greatest common divisor (GCD) of two positive integers is the largest integer that can divide the two integers. Please implement the program, using Euclidean algorithm (also known as division) to calculate the greatest common divisor of two numbers.
Euclidean algorithm: the greatest common divisor of two integers is equal to the greatest common divisor of the smaller number and the remainder.
For example, find the greatest common divisor of 252 and 105
Because 252% 105 = 147, the greatest common divisor of 252 and 105 is the greatest common divisor of 147 and 105.
Because 147% 105 = 42, the greatest common divisor of 252 and 105, that is, the greatest common divisor of 147 and 105, that is, the greatest common divisor of 105 and 42, can be obtained by repeating this way. In this process, the larger number is reduced, so continue to carry out the same calculation, you can continue to reduce these two numbers until one of them becomes zero. At this time, the remaining number that has not become zero is the greatest common divisor of two numbers.
###Program input:
Two positive integers are entered by the user
###Program output:
The greatest common divisor of two positive integers.
If the input number is not a positive integer, the program outputs:
```
Input number should be positive!
```
###Function interface definition:
```c++
int Gcd(int a, int b);
```
Where 'a' and 'B' are two positive integers entered by the user.
Function returns the greatest common divisor of 'a' and 'B'.
If 'a' or 'B' is less than or equal to 0, the function returns - 1.
###Main program example:
Here is the main function and an example of calling GCD function
```c++
#include
int Gcd(int a, int b);
int main()
{
int a, b, c;
scanf("%d %d", &a, &b);
c = Gcd(a,b);
if (c != - 1)
{
printf("%d\n", c);
}
else
{
printf("Input number should be positive!\ n");
}
return 0;
}
/*Please complete the GCD function here*/
```
###Input example:
Here is a set of inputs. For example:
```in
15 20
```
###Output example:
The corresponding output is given here. For example:
```out
five
```
answer:If there is no answer, please comment
Euclidean algorithm: the greatest common divisor of two integers is equal to the greatest common divisor of the smaller number and the remainder.
For example, find the greatest common divisor of 252 and 105
Because 252% 105 = 147, the greatest common divisor of 252 and 105 is the greatest common divisor of 147 and 105.
Because 147% 105 = 42, the greatest common divisor of 252 and 105, that is, the greatest common divisor of 147 and 105, that is, the greatest common divisor of 105 and 42, can be obtained by repeating this way. In this process, the larger number is reduced, so continue to carry out the same calculation, you can continue to reduce these two numbers until one of them becomes zero. At this time, the remaining number that has not become zero is the greatest common divisor of two numbers.
###Program input:
Two positive integers are entered by the user
###Program output:
The greatest common divisor of two positive integers.
If the input number is not a positive integer, the program outputs:
```
Input number should be positive!
```
###Function interface definition:
```c++
int Gcd(int a, int b);
```
Where 'a' and 'B' are two positive integers entered by the user.
Function returns the greatest common divisor of 'a' and 'B'.
If 'a' or 'B' is less than or equal to 0, the function returns - 1.
###Main program example:
Here is the main function and an example of calling GCD function
```c++
#include
int Gcd(int a, int b);
int main()
{
int a, b, c;
scanf("%d %d", &a, &b);
c = Gcd(a,b);
if (c != - 1)
{
printf("%d\n", c);
}
else
{
printf("Input number should be positive!\ n");
}
return 0;
}
/*Please complete the GCD function here*/
```
###Input example:
Here is a set of inputs. For example:
```in
15 20
```
###Output example:
The corresponding output is given here. For example:
```out
five
```
answer:If there is no answer, please comment