Skip to content

Instantly share code, notes, and snippets.

@vikychoi
Last active January 22, 2018 14:37
Show Gist options
  • Save vikychoi/548d38dc4d5a261946799f4dd75cebbd to your computer and use it in GitHub Desktop.
Save vikychoi/548d38dc4d5a261946799f4dd75cebbd to your computer and use it in GitHub Desktop.
Fraction addition
#include <stdio.h>
int main()
{
int a, b, c, d;
printf("Enter fraction 1(numerator denominator) \n");
scanf("%d %d", &a,&b);
printf("Enter fraction 2(numerator denominator) \n");
scanf("%d %d", &c, &d);
if (b == 0 || d == 0){
printf("Math Error");
return 0;
}
if (b == d){
if(b / d *d != b){
printf("%d / %d \n", a + c, b);
}
else{
printf("%d", (a+c) / b);
}
}
else {
int up, down;
up = a*d+c*b;
down =b*d;
if (up / down * up != up){
printf("%d / %d \n", up, down);
}
else{
printf("%d", up/down);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment