How to reduce fractions in C++
A fraction is the ratio of two values. Fractions are written as a/b, where an is the numerator, b is the denominator, and b cannot equal zero. The denominator indicates how many equal portions there are. The numerator indicates how many of these are taken.
Given two integers, b and a, b is divisible by a. It can be represented as a fraction b/a. The aim is to reduce the fraction to its simplest form.
Examples:
Input : b = 32, a = 28
Output : b = 8, a = 7
Input : b = 12, a = 8
Output : b = 3, a = 2
Proper and Improper Fractions
A fraction with a smaller numerator than a denominator is called a proper fraction. A fraction is considered inappropriate if its denominator exceeds its numerator by an equivalent amount. Proper fractions are 2/3, 8/19, and 1/8; improper fractions are 11/4, 9/2, and 15/7.
What is meant when fractions are reduced?
Writing fractions most straightforwardly is known as reducing them. A fraction can be decreased by reducing its numerator and denominator by its largest common factor. For instance, a fraction is fully reduced when its numerator and denominator share exactly one element: 1.
Methods for Lowering Fractions
The steps to reduce fractions are as follows:
1. List the numerator's factors
2. Enumerate the denominator's factors.
3. Determine the denominator's and numerator's greatest common factor (GCF).
4. Apply the GCF to both the numerator and the denominator.
Here's an illustration:
Diminish the fraction 36/48
1. Enumerate the numerator's factors: 1, 2, 3, 4, 6, 9, 13, 16, 36 are the factors of 36.
2. Enumerate the denominator's factors: 1, 2, 3, 4, 6, 8, 12, 16, 24, and 48 are the factors of 48.
3. Determine the numerator's and the denominator's greatest common factor (GCF): For 36 and 48, the GCF is 16.
4. Apply the GCF to both the numerator and the denominator:
Thus, lowering the fraction 36 / 48 produces the fraction 3/4.
Method 1:
Algorithm:
- Create the "subFunction" function, which takes two integer inputs, b and a.
- Define the variable k as an integer.
- Call the __gcd() method using the inputs b and a, then save the result in k.
- Divide b by k, and then return the result to b.
- Divide a by k and then add the result back to a
- Print the lowered fraction together with the amended b and a values.
// C++ program to reduce a fraction x/y
// to its lowest form
#include <bits/stdc++.h>
using namespace std;
void subFunction(int b, int a)
{
int k;
k = __gcd(b, a);
b = b / k;
a = a / k;
cout << "b = " << b << ", a = " << a << endl;
}
int main()
{
int b = 81;
int a = 45;
subFunction(b, a);
return 0;
}
Output:
![How to reduce fractions in C++/>
<!-- /wp:html -->
<!-- wp:paragraph -->
<p><strong>Time Complexity:</strong> O(log(max(x,y)))</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p><strong>Auxiliary Space:</strong> O(log(max(x,y)))</p>
<!-- /wp:paragraph -->
<!-- wp:heading -->
<h2 class=](https://static.tutorialandexample.com/cpp/how-to-reduce-fractions-in-cpp1.png)
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
char abc;
cout << "Enter any fraction: ";
cin >> a >> abc >> b;
if (abc != '/')
exit(0);
for (int i = b; i>=2; i--)
{
if (a % i == 0 && b % i == 0)
{
a = a/i;
b = b/i;
}
}
cout << "Reduced Fraction: ";
if (b == 1)
cout << a;
else
cout << a << "/" << b;
cout << endl;
return 0;
}
Output:
![How to reduce fractions in C++/>
<!-- /wp:html -->
<!-- wp:heading -->
<h2 class=](https://static.tutorialandexample.com/cpp/how-to-reduce-fractions-in-cpp2.png)
Fractions should be reduced for several reasons.
- Reducing fractions makes them easier to handle. Utilizing a smaller fraction, such as 4/7, is far simpler to work with than a fraction that hasn't been decreased, like 12/21.
- Fraction reduction is a common mathematics operation. Mathematicians everywhere reduce fractions to keep things simple when working with fractions.
- Fraction reduction makes sense!
Tricks and Advice for Lowering Fractions
You now understand how to simplify a fraction using the three different ways. Here are some tips that will enable you to easily reduce fractions. Take a look at these pointers and strategies to simplify fractions.
- A fraction cannot be further simplified if either the numerator or the denominator is a prime number.
- One cannot lower a fraction with 1 in the numerator any further.
- To reduce an improper fraction, use the same process as for simplifying a proper fraction: first, write it as a mixed fraction.