Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that lets the user perform arithmetic operations on fractions. F

ID: 3761082 • Letter: W

Question

Write a program that lets the user perform arithmetic operations on fractions. Fractions are of the form a/b, in which a and b are integers and b != 0. Your program must be menu driven, allowing the user to select the operation (+, -, *, or /) and input the numerator and denominator of each fraction. Furthermore, your program must consist of at least the following functions:

Function menu: This function informs the user about the program’s purpose, explains how to enter data, and allows the user to select the operation.

Function addFractions: This function takes as input four integers representing the numerators and denominators of two fractions, adds the fractions, and returns the numerator and denominator of the result. (Notice that this function has a total of six parameters.)

Function subtractFractions: This function takes as input four integers representing the numerators and denominators of two fractions, subtracts the fractions, and returns the numerator and denominator of the result. (Notice that this function has a total of six parameters.)

Function multiplyFractions: This function takes as input four integers representing the numerators and denominators of two fractions, multiplies the fractions, and returns the numerators and denominators of the result. (Notice that this function has a total of six parameters.)

Function divideFractions: This function takes as input four integers representing the numerators and denominators of two fractions, divides the fractions, and returns the numerator and denominator of the result. (Notice that this function has a total of six parameters.)

Explanation / Answer

#include <stdio.h>
void addFractions(int num1, int den1, int num2, int den2, int *resNum, int *resDen)
{
*resNum = (num1 * den2 + num2 * den1);
*resDen = den1 * den2;
}

void subtractFractions(int num1, int den1, int num2, int den2, int *resNum, int *resDen)
{
*resNum = (num1 * den2 - num2 * den1);
*resDen = den1 * den2;
}

void multiplyFractions(int num1, int den1, int num2, int den2, int *resNum, int *resDen)
{
*resNum = num1 * num2;
*resDen = den1 * den2;
}

void divideFractions(int num1, int den1, int num2, int den2, int *resNum, int *resDen)
{
*resNum = num1 * den2;
*resDen = den1 * num2;
}

void menu()
{
int num1, num2, den1, den2, resNum, resDen;
char ch;
printf("Fraction Calculator: ");
printf("Enter the first fraction: ");
printf("Numerator: ");
scanf("%i", &num1);
printf("Denominator: ");
scanf("%i", &den1);
while(den1 == 0)
{
printf("Denominator cannot be ZERO... Enter a non-zero value: ");
scanf("%i", &den1);
}
printf("Enter the second fraction: ");
printf("Numerator: ");
scanf("%i", &num2);
printf("Denominator: ");
scanf("%i", &den2);
while(den2 == 0)
{
printf("Denominator cannot be ZERO... Enter a non-zero value: ");
scanf("%i", &den2);
}
printf("(A)ddition. (S)ubtraction. (M)ultiplication. (D)ivision. ");
printf("Enter your choice: ");
scanf(" %c", &ch);
switch(ch)
{
case 'A':
case 'a':   addFractions(num1, den1, num2, den2, &resNum, &resDen);
           printf("The summation of two given fractions is: %i/%i ", resNum, resDen);
           break;
case 'S':
case 's':   subtractFractions(num1, den1, num2, den2, &resNum, &resDen);
           printf("The subtraction of two given fractions is: %i/%i ", resNum, resDen);
           break;
case 'M':
case 'm':   multiplyFractions(num1, den1, num2, den2, &resNum, &resDen);
           printf("The product of two given fractions is: %i/%i ", resNum, resDen);
           break;
case 'D':
case 'd':   divideFractions(num1, den1, num2, den2, &resNum, &resDen);
           printf("The division of two given fractions is: %i/%i ", resNum, resDen);
           break;
default :   printf("Invalid Choice..... Quitting..... ");
}
}
int main()
{
menu();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote