demo/simplify_the_denominator.c
2019-11-03 19:32:38 +08:00

23 lines
437 B
C

#include <stdio.h>
int gcd(int a, int b)
{
return (b == 0) ? a : gcd(b, a % b);
}
int show_simplified(int a, int b)
{
if (!a || !b) return printf("a, b should not be zero!\n");
int sign = (a > 0) ^ (b > 0);
a = (a > 0) ? a : -a;
b = (b > 0) ? b : -b;
int d = gcd(a, b);
return printf("simplified is: %s%d/%d\n", sign ? "-" : "", a / d, b / d);
}
int main()
{
show_simplified(123, 333);
return 0;
}