1. ## Simplifying fractions

I have compiled a database for rational numbers. Integers are used to represent the numerator and denominator of rational numbers. Subtractions, additions, multiplications, and divisions can be performed. Can anyone please help me include simplication code for the output answers? In addition, the answer for 1/2-1/2 is 0/4, which is quite weird.

Any help would be greatly appreciated.

My code:

Code:
```#include<stdio.h>

int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
{
d3 = gcd(d1,d2);
d3 = (d1*d2) / d3;
n3 = (n1)*(d3/d1) + (n2)*(d3/d2);
printf("\n %d/%d + %d/%d \t = \t %d/%d\n", n1, d1,n2, d2, n3, d3);
return 0;
}
int subFraction(int n1, int d1, int n2,int d2,int d3,int n3)//Subtract
{
d3 = d1*d2;
n3 = (n1*d2 - n2*d1);
printf("\n %d/%d - %d/%d \t = \t %d/%d\n", n1, d1,n2, d2, n3, d3);
return 0;
}
int mulFraction(int n1, int d1, int n2,int d2,int d3,int n3)//Multiply
{
n3 = n1*n2;
d3=d1*d2;
printf("\n %d/%d * %d/%d \t = \t %d/%d\n", n1, d1,n2, d2, n3, d3);
return 0;
}
int divFraction(int n1, int d1, int n2,int d2,int d3,int n3)//division
{
n3 = n1*d2;
d3=d1*n2;
printf("\n %d/%d / %d/%d \t = \t %d/%d\n", n1, d1,n2, d2, n3, d3);
return 0;
}
int main()
{
int n1, d1, n2, d2, d3 = 0, n3 = 0;
printf("\n Enter the values of numerator 1 and denominator 1: ");
scanf("%d %d",&n1,&d1);
printf("\n Enter the values of numerator 2 and denominator 2: ");
scanf("%d %d",&n2,&d2);
addFraction(n1, d1, n2, d2,d3,n3); //calling the function to perform addition of rational values
subFraction(n1, d1, n2, d2,d3,n3); //calling the function to perform subtraction of rational values
mulFraction(n1, d1, n2, d2,d3,n3); //calling the function to perform multiplication of rational values
divFraction(n1, d1, n2, d2,d3,n3); //calling the function to perform division of rational values
return 0;
}```
Some examples of my output:

2. You already have a function for computing GCD, so given a numerator and denominator, you compute the GCD and then divide both by the GCD, simplifying the fraction.

Originally Posted by Eve Wein
In addition, the answer for 1/2-1/2 is 0/4, which is quite weird.
In addFraction, you're already doing some simplification for the resulting denominator. You just didn't do the same for subFraction.

A few other things to note:
• Indent your code more consistently.
• Putting a blank line or two in between function definitions can help make it easier to read your code. You can also use a single blank line sparingly to separate parts of your code within a function.
• Since you're not using them as what we call output parameters (or out parameters), your n3 and d3 parameters really should be local variables instead.
• Do you plan to make use of the return value of these functions? If not, declare their return type as void instead of pointlessly returning an unused 0.
• You don't need comments that basically say what the function name already tells the reader. You could use comments to do things like say, document that the denominator cannot be 0.