Program to show decimal number from rational number representation in C++


Suppose we have two numbers called numerator and denominator representing a rational number in the form (numerator / denominator). We have to find its decimal representation as a string. If there are some recurring numbers, then surround them with brackets.

So, if the input is like numerator = 164 denominator = 3, then the output will be "54.(6)".

To solve this, we will follow these steps −

  • if numerator is same as 0, then −
    • return "0"
  • Define an array ans
  • if numerator < 0 and denominator > 0 or numerator > 0 and denominator < 0, then −
    • insert '-' at the end of ans
  • divisor := |numerator|
  • dividend := |denominator|
  • remainder := divisor mod dividend
  • x := convert (divisor / dividend) to string
  • for initialize i := 0, when i < size of x, update (increase i by 1), do −
    • insert x[i] at the end of ans
  • if remainder is same as 0, then −
    • return ans as string
  • insert '.' at the end of ans
  • Define one map m
  • while remainder is not equal to 0, do −
    • if remainder is not in m, then −
      • insert (first element of ans concatenate '(') into ans
      • insert ')' at the end of ans
      • Come out from the loop
    • Otherwise −
      • m[remainder] := size of ans
      • remainder := remainder * 10
      • insert (remainder / dividend) concatenate '0' at the end of ans
      • remainder := remainder mod dividend
  • return ans as string

Example 

Let us see the following implementation to get better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
   public:
   string solve(int numerator, int denominator) {
      if (numerator == 0)
         return "0";
         vector<char> ans;
      if (numerator < 0 && denominator > 0 || numerator > 0 && denominator < 0)
         ans.push_back('-');
         long divisor = labs(numerator);
         long dividend = labs(denominator);
         long remainder = divisor % dividend;
         string x = to_string(divisor / dividend);
         for (int i = 0; i < x.size(); i++) {
            ans.push_back(x[i]);
         }
         if (remainder == 0) {
         return string(ans.begin(), ans.end());
      }
      ans.push_back('.');
      map<int, int> m;
      while (remainder != 0) {
         if (m.find(remainder) != m.end()) {
            ans.insert(ans.begin() + m[remainder], '(');
            ans.push_back(')');
            break;
         } else {
            m[remainder] = ans.size();
            remainder *= 10;
            ans.push_back((remainder / dividend) + '0');
            remainder %= dividend;
         }
      }
      return string(ans.begin(), ans.end());
   }
};
string solve(int numerator, int denominator) {
   return (new Solution())->solve(numerator, denominator);
}
int main() {
   cout << solve(164, 3);
}

Input

164, 3

Output

54.(6)

Updated on: 12-Dec-2020

281 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements