Write a C program: You are to write a program that reads fractions and prints ea
ID: 3684880 • Letter: W
Question
Write a C program:
You are to write a program that reads fractions and prints each fraction reduced to its lowest terms. The output of your program is to look like this where the user input is underlined bold: A fraction is to be stored as two integers: the numerator and the denominator. The input is terminated by entering any nonnumeric (quit is one example). Your program is to have a recursive function called GCF for finding the greatest common factor (aka greatest common divisor) of two integers. Your program is to have a function called for reduce for reducing a fraction to its lowest terms. The GCF of two integers x and y is given by GCF(x, y) = x if y is 0, GCF(x, y) = GCF(y,x%y) otherwise. All input and output is to be done in your main function.Explanation / Answer
/** C code that reads and prints each fraction in lowest form **/
#include <stdio.h>
int GCF(int a, int b) // function to calculate GCF
{
if(b == 0) return a;
else return GCF(b,a%b);
}
int main()
{
int a,b;
while(1)
{
printf("Fraction: ");
if(scanf("%d/%d", &a, &b) != 2) return 0; // check if non numeric value is entered
int gcd = GCF(a,b); // calculate GCF
if(gcd == 1) printf("Cannot be Reduced ");
else printf("Reduced = %d/%d ", a/gcd, b/gcd);
}
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.