Construct a C program that: a) contains a function called \"gcd\" with two (inpu
ID: 3630559 • Letter: C
Question
Construct a C program that:a) contains a function called "gcd" with two (input) arguments that provides as output
the greatest common divisor of the two input values (you may restrict the size of
the two input values to be between 1 and 1000);
b) provides checks to make certain that the inputs are so restricted (if user or calling program tries to
give out-of-range values, program should gracefully decline to provide a real answer, e.g., print
"gcd called with out-of-range value -- try again" or similar output, and return -1 as an answer);
c) contains a main function that calls "gcd" with a several pairs of integers, viz.,
120 and 36, 10 and 21, 35 and 77, 153 and 105, and six other pairs of your own choosing
(one pair of values should illustrate the error message as described in b) above);
d) after each call to "gcd," provides a line of output such as:
" The greatest common divisor of 120 and 36 is 12." ;
Explanation / Answer
please rate - thanks
# include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int gcd(int,int);
int main()
{
int a,b,i,g;
for(i=0;i<10 ;i++)
{
printf("Enter b and a (each between 1 and 1000)> ");
scanf("%d",&b);
scanf("%d",&a);
g=gcd(a,b);
if(g!=-1)
printf("The GCD of %d and %d is %d ",a,b,g);
}
getch();
}
int gcd(int a, int b)
{ int big,small;
if(a<1||b<1||a>1000||b>1000)
{printf("gcd called with out-of-range value -- try again ");
return -1;
}
for(; ;)
{
if(a==0)
return b;
if(b==0)
return a;
if(b<a)
{small=b;
big=a;
}
else
{ small=a;
big=b;
}
big%=small;
a=big;
b=small;
}
}