/*-*-mode:c-*-
***************************************************************************
CSN381: Project 2, problem 1
http://homepage.mac.com/hteric/Project2.pdf
***************************************************************************
File : gcd.c
Author : Jeremy Russell
Date : September 23, 2002
Description : This problem requires you to compute GCD (greatest common
divisor) using recrussion.
(a) Write a function int rem(int a, int b) assuming that
a is greater than b, returns a the remainder of a
divided by b, e.g. for a = 13, b = 5 rem(a,b) = 3.
***************************************************************************/
#include<stdio.h>
int rem ( int a, int b ); // Returns the remainder of a / b.
int gcd ( int a, int b ); // Returns the greatest common divisor of a and b.
/***************************************************************************
Function : main
Author : Jeremy Russell
Date : September 23, 2002
Description : Main function of the isodd program. Prints out all odd
numbers between 1 and 200, inclusive.
Arguments : void
Returns : void
Notes : None
See Also : None
***************************************************************************/
int main ( void )
{
int a, b, tmp;
char again;
do
{
printf ("Please input an integer: ");
scanf ("%d", &a);
printf ("Please input another integer: ");
scanf ("%d", &b);
// printf ("A: %d, B: %d", a, b);
// return 1;
// Make sure a is the breater of a and b.
if (b > a)
{
tmp = a;
a = b;
b = tmp;
}
printf ("The greatest common divisor of %d and %d is %d.\n", a, b, gcd(a,b));
printf("Do it again (n/y) [n]: ");
scanf ("\n%c", &again);
} while ( again == 'y');
return 0;
}
/***************************************************************************
Function : rem
Author : Jeremy Russell
Date : September 23, 2002
Description : Returns the remainder of a / b assuming, a > b.
Arguments : int a, int b
Returns : int
Notes : None
See Also : None
***************************************************************************/
int rem ( int a, int b )
{
return a % b;
}
/***************************************************************************
Function : gcd
Author : Jeremy Russell
Date : September 23, 2002
Description : Finds the greatest common divisor.
Arguments : int a, int b
Returns : int
Notes : None
See Also : None
***************************************************************************/
int gcd ( int a, int b )
{
int divisor, remainder;
remainder = rem(a, b);
if (remainder)
{
divisor = gcd( b, remainder);
}
else
{
divisor = b;
}
return divisor;
}