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:
Some examples of my output:Code:#include<stdio.h>
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
int addFraction(int n1, int d1, int n2,int d2,int d3,int n3)//Add
{
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;
}
Attachment 16239
Attachment 16238