Skip to content
Snippets Groups Projects
gcd.c 686 B
Newer Older
Bruce Cowan's avatar
Bruce Cowan committed
#include <stdio.h>
#include <stdlib.h>

static int
gcd (int a, int b)
{
	static int pass = 0;
	++pass;

	printf ("\n---Pass #%d---\n", pass);
	printf ("a = %d, b = %d\n", a, b);

	if (b == 0)
		return a;
	else
		return gcd (b, a % b);
}

int
main (int argc, char **argv)
{
	int a, b, solution;

	if (argc != 3)
	{
		printf ("Input the first number ");
		scanf ("%d", &a);
		printf ("Input the second number ");
		scanf ("%d", &b);
	}
	else
	{
		a = strtol (argv[1], NULL, 0);
		b = strtol (argv[2], NULL, 0);
	}

	solution = gcd (a, b);

	printf ("The GCD of (%d, %d) is %d\n", a, b, solution);
	printf ("For example, it could be (%d, %d)\n", a / solution, b / solution);

	return 0;
}