Question

C program fractions.c that does the following: 1. The program starts by making the user enter...

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

Homework Answers

Answer #1

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)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program that prompts the user to enter an integer number between 1 and 999....
Write a program that prompts the user to enter an integer number between 1 and 999. The program displays the sum of all digits in the integer if the input is valid; otherwise, it displays a message indicating that the integer is not between 1 and 999 and hence, is invalid. Name the program file Q1.cpp Example: if the user enters 12, sum of digits is 3. If the user enters 122, sum of digits is 5.
draw flowchart of: 1-algorithm that asks the user to enter three numbers and computes and prints...
draw flowchart of: 1-algorithm that asks the user to enter three numbers and computes and prints out the largest number. 2- an algorithm that asks the for an integer N from the user. The algorithm should find whether the number is positive, negative, or ZERO. 3-an algorithm that asks the user to enter a positive integer N. You then need to calculate the sum of all values from 1 to N.
Write a complete C++ program asking the user to enter numbers one by one. User enters...
Write a complete C++ program asking the user to enter numbers one by one. User enters only whole numbers 0 and 10. Once the user enters 0, then the program should print sum of only odd numbers entered by user prior to 0. A valid scenario given below: Enter a number: 5 Enter a number: 3 Enter a number: 2 Enter a number: 3 Enter a number: 8 Enter a number: 0 Sum of the odd numbers you entered is...
design a program that prompts the user to enter a positive integer number and reads the...
design a program that prompts the user to enter a positive integer number and reads the user’s input. If the user input is not a positive number, the program should prompt them repeatedly until they enter a valid input. Once a valid input is received ,the program uses a loop to calculate the sum of the digits of the user’s input and displays the sum. For example, if the user enters the number 94311, the program should print the sum...
Q :     Write a C++ program that asks the user to enter three integers and...
Q :     Write a C++ program that asks the user to enter three integers and then displays the largest one. Example : if the user enter ( 7, 2 ,5) the largest number will be 7 Or if user enter ( 2 , 5 , 7 ) the largest number will be 7 Or if user enter ( 7, 5 ,2) the largest number will be 7 In general it does not matter the order of the entered numbers...
1. Write a complete program in C++ that does the following: [2] asks a user to...
1. Write a complete program in C++ that does the following: [2] asks a user to enter two integer numbers (display a warning message that the second number should be different from zero) [3] reads the two numbers from the keyboard; [4] displays the product, the sum, the quotient and the remainder of integer division of the first one by the second one. [3] displays the result of floating-point division. Make sure to follow programming style guidelines.
in this assignment, you will re-fine your digit-switch program C# − You should revise the program...
in this assignment, you will re-fine your digit-switch program C# − You should revise the program as follows: • If the user enters a negative number, your program displays “Invalid input! Please enter a non-negative number.” • If the user enters a one-digit or three-digit or four-digit number, your program displays “Calculation overloaded! Please enter a two-digit number”. • Hints: 9/10 = 0; 10/10 = 1; 99/10 = 9; 100/10 = 10; 1000/10 = 100;
Write a C program that prompts the user to input as many unsigned(n) as the user...
Write a C program that prompts the user to input as many unsigned(n) as the user wants to type, terminated by non negative number You may assume without checking that when the user is supposed to type an unsigned, he nevertypes a double You may assume without checking that when the user is supposed to type an unsigned, he nevertypes a negative number. For each number the user types, output whether that number is prime or not. You won't need...
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
PROGRAM PYHTON Write a function called "calculateInput" that when called, it asks the user to enter...
PROGRAM PYHTON Write a function called "calculateInput" that when called, it asks the user to enter an number. Then, it asks for a second number. These will use the "input" command. Store these as two separate variables. This function takes NO arguments and is also a VOID function, there is no return statement. The function should print the result of these two numbers multiplied together. The program should be able to multiply floats. For example: Test Input Result calculateInput() 5...