Write a function int GetGCD (inta, intb) that returns by calculating the maximum common divisor of two positive integers a, b Use this to enter positive integers until the user enters zero and the maximum number of pledges of those numbers Write a program that prints.
int GetGCD(int x, int y) {
int a, b, c;
a = (x >= y) ? x : y;
b = (x >= y) ? y : x;
while (b != 0)
{
c = a % b;
a = b;
b = c;
}
return a;
}
I wrote a function to find the maximum common divisor in Euclidean way, but I don't know how to write the main function.ㅠ<
c
#pragma warning (disable:4996)
#include <stdio.h>
int GetGCD (int x, int y); // user function declaration
int main()
{
declaration of local variables within int x, y, result; //main function
x = y = -1;
result = 0;
When (!(x == 0 & & y == 0)) // If both x and y are 0, exit the while statement; if not, repeat
{
printf("Please enter two numbers: ");
scanf("%d %d", &x, &y);
result = GetGCD(x, y);
printf("%d, %d maximum number of pledges = %d\n", x, y, result);
}
return 0;
}
int GetGCD(int x, int y) {
int a, b, c;
a = (x >= y) ? x : y;
b = (x >= y) ? y : x;
while (b != 0)
{
c = a % b;
a = b;
b = c;
}
return a;
}
#pragma warning (disable:4996)
#include <stdio.h>
int GetGCD (int x, int y); // user function declaration
int main()
{
declaration of regional variables within int result; //main function
int tmp = 0;
int num = -1;
printf("Enter at least two non-negative integers" [Only the number before entering 0 is valid] : ");
while(1)
{
if (tmp == 0)
{
scanf("%d", &result);
tmp++;
}
else
{
scanf(" %d", &num);
tmp++;
}
if (num == 0)
break;
if (tmp >= 2)
result = GetGCD(result, num);
}
rewind(stdin);
getchar();
printf("Maximum number of pledges = %d\n\n", result);
printf ("Press any key to exit";
getchar();
return 0;
}
int GetGCD(int x, int y) {
int a, b, c;
a = (x >= y) ? x : y;
b = (x >= y) ? y : x;
c = a % b;
a = b;
b = c;
if (c == 0)
return a;
else return GetGCD(a, b); call the recursive function until the value of //c becomes 0, and return the maximum number of pledges only when the value of c becomes 0
}
© 2024 OneMinuteCode. All rights reserved.