Answer to Question #317913 in C for Harsha Vardhan

Question #317913

Write a program that works with fractions. Your program should be able to add, 

subtract, multiply, and divide two fractions. Write a separate function for addition, 

subtraction, multiplication and division. Specifically, your program must request 

two fractions from the user, getting the numerator and denominator separately for 

each fraction, and the operation to perform (add, subtract, multiply, or divide). 

Your program will then compute the resulting fraction, keeping the numerator and 

denominator separate, and output the result. 

Note:

Make the structure for fractions. Then declare variable of fractions and use 

them for addition and multiplication etc....


1
Expert's answer
2022-03-25T12:10:08-0400
#include <stdio.h>
#include <stdlib.h>


typedef struct {
	int numerator;
	int denominator; 
} Fraction;


int gcd(int numerator, int denominator) {
	int result;
	while (denominator != 0) {
		result = numerator % denominator;
		numerator = denominator;
		denominator = result;
	}
	return numerator;
}


int lcm(int a, int b) {
	return (a / gcd(a,b)) * b;
}


Fraction simplify (Fraction a) {
	int cd;
	cd = gcd(a.numerator, a.denominator);
	a.numerator /= cd;
	a.denominator /= cd;
	return a;
}


Fraction add (Fraction a, Fraction b) {
	Fraction sum;
	int lcmd;
	a = simplify(a);
	b = simplify(b);
	lcmd = lcm(a.denominator, b.denominator);
	sum.numerator = (lcmd / a.denominator * a.numerator) + (lcmd / b.denominator * b.numerator);
	sum.denominator = lcmd;
	return simplify(sum);
}


Fraction subtract (Fraction a, Fraction b) {
	Fraction sum;
	int lcmd;
	a = simplify(a);
	b = simplify(b);
	lcmd = lcm(a.denominator, b.denominator);
	sum.numerator = (lcmd / a.denominator * a.numerator) - (lcmd / b.denominator * b.numerator);
	sum.denominator = lcmd;
	return simplify(sum);
}


void swap(int *a, int *b) {
	int tmp = *a;
	*a = *b;
	*b = tmp;
}


Fraction multiply (Fraction a, Fraction b) {
	Fraction sum;
	a = simplify(a);
	b = simplify(b);
	swap(&a.numerator, &b.numerator); 
	a = simplify(a);
	b = simplify(b);
	sum.numerator = (a.numerator * b.numerator);
	sum.denominator = (a.denominator * b.denominator);
	return sum;
}


Fraction divide (Fraction a, Fraction b) {
	swap(&b.numerator, &b.denominator);
	return multiply(a, b);
}


int main() {
	int a, b;
	Fraction f1 ,f2, res;


	
	printf("Enter numerator of fraction 1: ");
	scanf("%d",&f1.numerator);
	printf("Enter denominator of fraction 1: ");
	scanf("%d",&f1.denominator);
	
	printf("Enter numerator of fraction 2: ");
	scanf("%d",&f2.numerator);
	printf("Enter denominator of fraction 2: ");
	scanf("%d",&f2.denominator);


	
	res = add(f1, f2);
	printf(" %d/%d + %d/%d = %d/%d \n", f1.numerator, f1.denominator, f2.numerator, f2.denominator, res.numerator, res.denominator);
	res = subtract(f1, f2);
	printf(" %d/%d - %d/%d = %d/%d \n", f1.numerator, f1.denominator, f2.numerator, f2.denominator, res.numerator, res.denominator);
	res = multiply(f1, f2);
	printf(" %d/%d x %d/%d = %d/%d \n", f1.numerator, f1.denominator, f2.numerator, f2.denominator, res.numerator, res.denominator);
	res = divide(f1, f2);
	printf(" %d/%d / %d/%d = %d/%d \n", f1.numerator, f1.denominator, f2.numerator, f2.denominator, res.numerator, res.denominator);


	getchar();
	getchar();


	return 0;
	
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS