C program fractions.c that does the following:
1. The program starts by making the user enter a non-negative integer number a, called the first numerator. If the user enters a negative number, the program repeats the entry.
2. The program then makes the user enter a positive integer number b, called the first denominator. If the user enters a non-positive number, the program repeats the entry.
3. The program then makes the user enter a non-negative integer number c, called the second numerator. If the user enters a negative number, the program repeats the entry.
4. The program then makes the user enter a positive integer number d, called the second denominator. If the user enters a non-positive number, the program repeats the entry.
5. The four numbers entered by the user form two fractions a/b and c/d. The program then computes two integer numbers m, n such that m n = a b + c d = ad + cb bd . 6. The program then computes the integer part k ∈ N of the fraction m/n, as well as the fractional part p/q: k + p q = m n . Clearly, k is the integer division’s result of m divided by n, p is the remainder of that division and q is equal to n.
7. If p is zero, the program sets g to 1. Otherwise, the program computes g = gcd(p, q), using Euclid’s algorithm seen in class. Use caution with this step: Euclid’s algorithm destroys the original contents of the variables it is run on. As p and q are still needed in the subsequent steps, Euclid’s algorithm needs to be run on copies of p and q in two temporary variables, say t1 and t2.
8. The program then reduces the fraction p/q using g: r s = p/g q/g . Clearly, g always divides both p and q without remainder; so no special care is needed here. 1
9. The program then displays the inputs a, b, c, d as well as the output k, r, s as follows: • The program always starts by displaying: a /b + c /d = . • If k = 0 and r = 0, the program then displays 0, followed by a newline. • If k = 0 and r 6= 0, the program then displays r /s, followed by a newline. • If k 6= 0 and r = 0, the program then displays k, followed by a newline. • If k 6= 0 and r 6= 0, the program then displays k + r /s, followed by a newline. For the precise wording of the prompts and the exact output, see the examples below.
Here are examples of input and output of the program:
Please enter a first numerator (>= 0): 3
Please enter a first denominator (> 0): 4
Please enter a second numerator (>= 0): 1
Please enter a second denominator (> 0): 8
3 / 4 + 1 / 8 = 7 / 8
Below is the program code in C for fraction addition and formats the output as per question's instruction - (attached images of output console where program is tested against random inputs)
#include <stdio.h>
int main()
{
int a, b,c,d,x,y,i,gcd;
printf("Please enter a first numerator (>= 0): ");
scanf("%d",&a);
while(a<0)
{
printf("Please enter a valid first numerator (>= 0): ");
scanf("%d",&a);
}
printf("Please enter a first denominator (> 0): ");
scanf("%d",&b);
while(b<=0)
{
printf("Please enter a valid first denominator (>= 0): ");
scanf("%d",&b);
}
printf("Please enter a second numerator (>= 0): ");
scanf("%d",&c);
while(c<0)
{
printf("Please enter a valid second numerator (>= 0): ");
scanf("%d",&c);
}
printf("Please enter a second denominator (> 0): ");
scanf("%d",&d);
while(d<=0)
{
printf("Please enter a valid second denominator (>= 0): ");
scanf("%d",&d);
}
x=(a*d)+(b*c);
y=b*d;
for(i=1; i <= x && i <= y; ++i)
{
if(x%i==0 && y%i==0)
gcd = i;
}
printf("\n%d/%d + %d/%d = %d/%d ",a,b,c,d,x/gcd,y/gcd);
printf("\n");
return 0;
}
Test - 1 :
Test - 2 :(test where re-attempt of input was required)
Get Answers For Free
Most questions answered within 1 hours.