write in C a program that you read a fraction of the form X/Y from the keyboard,
ID: 3530548 • Letter: W
Question
write in C a program that you read a fraction of the form X/Y from the keyboard, reduce it to its lowest form, and print the result. i.e. if the user enters 6/12 your program will print 1/2. Your program must prompt the user to enter a fraction, and then read the input from the keyboard. This information may not be read from the command line as part of the program invocation. Your program should be enclosed in a loop that asks the user if she/he wants to reduce another fraction. Thus, the user should be able to reduce an unlimited number of fractions without re-running the program. Your program must validate user input, and if an incorrect fraction is entered, print a helpful error message and allow the user to re-enter a fraction. The numerator and denominator are 32 bit signed integers. The output must be a fraction of the form X/Y. [ 4/2 will be printed as 2/1. Your program must compile and run on rohan using the default gcc compiler. Programs that do not compile will receive no credit. Sample Output: John Student, masc1234 Please enter a fraction to reduce: x/2 Sorry, invalid input. Please enter a fraction to reduce: 2/8 The reduced fraction is: 1/4 Do you want to reduce another fraction (y/n)? x Sorry, invalid input. Do you want to reduce another fraction (y/n)? n Program complete. Input Validation You must insure that all input matches these rules:Explanation / Answer
#include <stdbool.h> #include <stdio.h> //gcd function - return gcd of two numbers int gcd(int n, int m) { int gcd, remainder; while (n != 0) { remainder = m % n; m = n; n = remainder; } gcd = m; return gcd; }//end gcd function int main (int argc, const char * argv[]) { // insert code here... //--declarations int number1, number2; int newNumber1, newNumber2; //--get user input printf("Enter a fraction: "); scanf("%d/%d", &number1, &number2); //--calculations //find the gcd of numerator and denominator //then divide both the numerator and denominator by the GCD newNumber1 = number1 / gcd(number1, number2); newNumber2 = number2 / gcd(number1, number2); //--results printf("In lowest terms: %d/%d", newNumber1, newNumber2); }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.