168k views
5 votes
Fractions in Lowest Terms Problem Statement: You are tasked to write a program which takes a numerator and a denominator as a user input and reduce the fraction to its lowest terms. For example, if user inputs 48 as numerator, and 9 as denominator, then your program should output 16/3. This will require finding the greatest common divisor (gcd) for the numerator and denominator, then dividing both by that number. If the denominator is 0, an error message should be printed. At the end of each calculation, the user should be asked if they want to do another conversion or exit the program. The program must handle all types of bad inputs from user and recover from the errors. Example run: Enter your numerator: 4.6 Invalid input, enter whole numbers only! Enter your numerator: -9 Enter your denominator: 45 The lowest terms of your fraction: -1/5 Do you want to do another conversion? (0-no, 1-yes): 1 Enter your numerator: 8 Enter your denominator: abc8 Invalid input, enter whole numbers only! Enter your denominator: 0 Invalid input, denominator cannot be 0! Enter your denominator: 2 The lowest terms of your fraction: 4 Do you want to do another conversion? (0-no, 1-yes): 0 Required function(s): Your program must involve the following functions. You may not change the parameters or the return type!!! //return false if denominator is 0, return true otherwise //Reduce both numerator and denominator to lowest terms inside the function bool to lowest_terms (int &numerator, int &denominator); //return the greatest common divisor of num1 and num2 int g_c_d(int numl, int num2);

User Jmccure
by
8.2k points

2 Answers

5 votes

Final answer:

To reduce a fraction to its lowest terms, find the greatest common divisor (gcd) of the numerator and denominator, then divide both numbers by the gcd.

Step-by-step explanation:

To reduce a fraction to its lowest terms, you need to find the greatest common divisor (gcd) of the numerator and denominator and divide both numbers by that gcd. Here is a step-by-step process:

  1. Check if the denominator is 0. If it is, print an error message.
  2. Calculate the gcd of the numerator and denominator using the g_c_d function.
  3. Divide both the numerator and denominator by the gcd.
  4. Print the reduced fraction.
  5. Ask the user if they want to do another conversion or exit.

For example, if the user inputs a numerator of 48 and a denominator of 9, the gcd is 3. Dividing both numbers by 3 gives us the reduced fraction 16/3.

User Nothing Here
by
7.0k points
1 vote

Below is a program which takes a numerator and a denominator as a user input and reduce the fraction to its lowest terms.

Python

def g_c_d(num1, num2):

if num2 == 0:

return num1

else:

return g_c_d(num2, num1 % num2)

def to_lowest_terms(numerator, denominator):

if denominator == 0:

return False

gcd = g_c_d(numerator, denominator)

numerator //= gcd

denominator //= gcd

return True

# Test cases

numerator = 48

denominator = 9

if to_lowest_terms(numerator, denominator):

print(f"The lowest terms of your fraction: {numerator}/{denominator}")

else:

print("Invalid input, denominator cannot be 0!")

numerator = -9

denominator = 45

if to_lowest_terms(numerator, denominator):

print(f"The lowest terms of your fraction: {numerator}/{denominator}")

else:

print("Invalid input, denominator cannot be 0!")

numerator = 8

denominator = 2

if to_lowest_terms(numerator, denominator):

print(f"The lowest terms of your fraction: {numerator}/{denominator}")

else:

print("Invalid input, denominator cannot be 0!")

So, the above program is one that works for all types of bad inputs from the user and recovers from the errors. For example, if the user enters a non-whole number for the numerator or denominator, the program will print an error message and ask the user to enter a new value.

So, If the user enters 0 for the denominator, the program will print an error message and terminate.

User Con Posidielov
by
8.0k points

No related questions found