C++ program to represent the Fraction of Two Numbers in the String Format


We are given two integer numerators and a denominator. We need to represent the fraction of these two integers in string format. If a certain decimal is repeating, we need a bracket to show its repeating sequence.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Determine the integral quotient (absolute part before to the decimal point) before determining the fractional portion.

  • Insert the remainder (numerator % denominator) in a map with the key being the remainder and the value being the index position at which this remainder occurs to see if the fractional component repeats.

  • There is no repetitive fraction if, at any moment in time, the remainder is zero.

  • There exists a repeating fraction if the remainder is already seen in the map.

Example

Suppose one integer is '8' as a numerator and another is '6' as a denominator.

int numerator = 8, denominator = 6;
res = solve(numerator, denominator);

So the fraction is 8/6, which is 1.333... So we need to output "1.(3)" The bracket represents the repeating fraction part if there is no repeating part, so we output the string value directly.

We can first calculate an integral part and save it in the string. After that, we can find the fraction part by using the remainder till it becomes 0 or till we find a repeating sequence. To find the repeating sequence, we can store the rest in the map, and if we happen to see the same remainder in the map, we can be sure that the sequence is repeating. The value part in the map will be the index where this fraction occurred first so that we can place the blanket on that index.

#include <iostream> #include <map> using namespace std; string solve(int numerator, int denominator) { if (numerator == 0) { return "0"; } if(denominator == 0) { return "INFINITY"; } string ans = ""; if ( ((numerator < 0) ^ (denominator < 0)) ) { ans+="-"; } numerator = abs(numerator); denominator = abs(denominator); ans+=to_string(numerator/denominator); if(numerator%denominator == 0) return ans; ans+="."; int remainder = numerator % denominator; map<int, int> mp; while (remainder > 0) { if (mp.find(remainder) != mp.end()) { ans += ")"; ans.insert(mp[remainder], "("); break; } else { mp[remainder] = ans.size(); } remainder*=10; ans+= to_string(remainder/denominator); remainder = remainder % denominator; } return ans; } int main() { int numerator = 8, denominator = 6; cout << solve(numerator, denominator) << endl; return 0; }

Output

1.(3)

Conclusion

We can see that using the map as a hash to find the repeating remainder and storing the index from where we can again start the sequence as the value in the key-value pair makes tracking and finding answers a lot easier. Using the correct data structure and some observation was needed in this problem.

Updated on: 10-Aug-2022

946 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements