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

Write a C program that implements fraction numbers. Your structure should have t

ID: 3821506 • Letter: W

Question

Write a C program that implements fraction numbers. Your structure should have two fields: numerator and denominator. Remember that a denominator should never be 0.

You need to implement the following operations:

Add two fractions

Subtract two fractions.

Multiply two fractions

Divide two fractions.

You need to implement a function for each one of these operators.

You main function will need to call these functions.

You main function will have an infinite loop with the following menu:

Add two fractions

Subtract two fractions.

Multiply two fractions

Divide two fractions. (you can do it at Home)

Quit

You would need to ask the user to input both the numerator and the denominator and ask the user for the operation number (1 for add, 2 for sub, 3 for mult, and 4 for div). The program then prints the outcome of the operation.

The infinite loop will terminate if the user input 5.

Explanation / Answer

#include <stdio.h>

struct fraction {
int numerator ;
int denominator;
};

int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}

// Function to convert the obtained fraction
// into it's simplest form
void lowest(struct fraction *f)
{
// Finding gcd of both terms
int common_factor = gcd(f->numerator, f->denominator);

// Converting both terms into simpler
// terms by dividing them by common factor
f->numerator = f->numerator/common_factor;
f->denominator = f->denominator/common_factor;
}

//Function to add two fractions
struct fraction addFraction(struct fraction f1, struct fraction f2)
{
struct fraction result;
// Finding gcd of den1 and den2
int den = gcd(f1.denominator,f2.denominator);

// Denominator of final fraction obtained
// finding LCM of den1 and den2
// LCM * GCD = a * b
den = (f1.denominator*f2.denominator) / den;

// Changing the fractions to have same denominator
// Numerator of the final fraction obtained
int num = (f1.numerator )*(den/f1.denominator) + (f2.numerator)*(den/f2.denominator);

result.numerator = num;
result.denominator = den;
// Calling function to convert final fraction
// into it's simplest form
lowest(&result);
return result;
}

//Function to add two fractions
struct fraction subtractFraction(struct fraction f1, struct fraction f2)
{
struct fraction result;
// Finding gcd of den1 and den2
int den = gcd(f1.denominator,f2.denominator);

// Denominator of final fraction obtained
// finding LCM of den1 and den2
// LCM * GCD = a * b
den = (f1.denominator*f2.denominator) / den;

// Changing the fractions to have same denominator
// Numerator of the final fraction obtained
int num = (f1.numerator )*(den/f1.denominator) - (f2.numerator)*(den/f2.denominator);

result.numerator = num;
result.denominator = den;
// Calling function to convert final fraction
// into it's simplest form
lowest(&result);
return result;
}

//Function to add two fractions
struct fraction multFraction(struct fraction f1, struct fraction f2)
{
struct fraction result;
// Finding gcd of den1 and den2
int den = f1.denominator*f2.denominator;
int num = (f1.numerator *f2.numerator);

result.numerator = num;
result.denominator = den;
// Calling function to convert final fraction
// into it's simplest form
lowest(&result);
return result;
}

//Function to add two fractions
struct fraction divFraction(struct fraction f1, struct fraction f2)
{
struct fraction result;
// Finding gcd of den1 and den2
int den = f1.denominator*f2.numerator;
int num = (f1.numerator *f2.denominator);

result.numerator = num;
result.denominator = den;
// Calling function to convert final fraction
// into it's simplest form
lowest(&result);
return result;
}

int main()
{
while(1)
{
printf("Enter 1. Add, 2. Subtract, 3. Multiply, 4. Divide, 5. Quit: ");
int choice;
scanf("%d", &choice);
  
if (choice == 5)
{
break;
}
  
int num, denom;
printf("Enter numerator of first fraction: ");
scanf("%d", &num);
  
while(1)
{
printf("Enter denomintor of first fraction: ");
scanf("%d", &denom);
if(denom == 0)
{
printf("Denominator cannot be 0. Try again! ");
}
else
{
break;
}
}
struct fraction f1;
f1.numerator = num;
f1.denominator = denom;
  
printf("Enter numerator of second fraction: ");
scanf("%d", &num);
  
while(1)
{
printf("Enter denomintor of second fraction: ");
scanf("%d", &denom);
if(denom == 0)
{
printf("Denominator cannot be 0. Try again! ");
}
else
{
break;
}
}
struct fraction f2;
f2.numerator = num;
f2.denominator = denom;

struct fraction result;
switch(choice)
{
case 1:
result = addFraction(f1, f2);
printf("%d/%d + %d/%d = %d/%d ", f1.numerator, f1.denominator, f2.numerator, f2.denominator, result.numerator, result.denominator);
break;
case 2:
result = subtractFraction(f1, f2);
printf("%d/%d - %d/%d = %d/%d ", f1.numerator, f1.denominator, f2.numerator, f2.denominator, result.numerator, result.denominator);
break;
case 3:
result = multFraction(f1, f2);
printf("%d/%d * %d/%d = %d/%d ", f1.numerator, f1.denominator, f2.numerator, f2.denominator, result.numerator, result.denominator);
break;
case 4:
result = divFraction(f1, f2);
printf("%d/%d / %d/%d = %d/%d ", f1.numerator, f1.denominator, f2.numerator, f2.denominator, result.numerator, result.denominator);
break;
}
}
  

return 0;
}

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