Find the greatest common factor of two integers.
/***************************************************************************************************
* cfact
*
* Author: Stefan Willmert
*
* Calculates the greatest common factor between two integers
*
* Build: gcc -o cfact cfact.c
*
*
***************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
int gcd(long, long);
void Usage();
int main(int argc, char *argv[])
{
long input = 0;
long input2 = 0;
if (argc != 3)
{
Usage();
return 0;
}
input = atol(argv[1]);
input2 = atol(argv[2]);
if (input > input2)
printf("%d\n", gcd(input, input2));
else
printf("%d\n", gcd (input2, input));
return 0;
}
void Usage()
{
printf("\n");
printf("Returns the greatest common factor between two integer numbers\n\n");
printf("Usage: cfact number1 number2\n\n");
printf(" number1 first number to get a factor\n");
printf(" number2 second number to get a factor\n");
printf("\n");
}
int gcd(long x, long y)
{
return y ? gcd(y, x % y) : x;
}