# Fraction to Recurring Decimal in C++

C++Server Side ProgrammingProgramming

Suppose we have two integers representing the numerator and denominator of a fraction, we have to find fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. So if the numerator is 2 and denominator is 3, then the output will be “0.(6)”

To solve this, we will follow these steps −

• if numerator is 0, then return 0

• define one array ans

• if the numerator < 0 and denominator > 0 or numerator 0 and denominator < 0, then insert negative symbol ‘-’ into the ans array

• divisor := |numerator| and dividend := |denominator|, remainder := divisor mod dividend

• x := string of divisor / dividend

• insert each character from x into ans array

• if remainder = 0, then return ans array as a string.

• insert dot ‘.’ into ans

• define one map m

• while remainder is not 0

• if remainder is present in m, then

• insert opening parentheses at index m[remainder] of ans

• insert closing parentheses into the ans at the end

• break the loop

• otherwise

• m[remainder] := size of ans

• remainder := remainder * 10

• insert (remainder / dividend) as character into ans

• remainder := remainder mod dividend

• return ans array as string.

Let us see the following implementation to get better understanding −

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
string fractionToDecimal(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());
}
};
main(){
Solution ob;
cout << ((ob.fractionToDecimal(100,6)));
}

## Input

100
6

## Output

16.(6)
{"ft_lang_label":"__label__en","ft_lang_prob":0.5211012,"math_prob":0.97686493,"size":2151,"snap":"2022-05-2022-21","text_gpt3_token_len":541,"char_repetition_ratio":0.18351188,"word_repetition_ratio":0.0,"special_character_ratio":0.29428172,"punctuation_ratio":0.18292683,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99970806,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-19T06:45:04Z\",\"WARC-Record-ID\":\"<urn:uuid:cd7efc2c-8643-4f1c-8b30-8dc6c7580c4d>\",\"Content-Length\":\"33187\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5d7f75fe-a1f1-4941-98c3-5f22b75e0186>\",\"WARC-Concurrent-To\":\"<urn:uuid:0e4692c9-18de-46bf-a7ef-7d8db5bec60f>\",\"WARC-IP-Address\":\"192.229.210.176\",\"WARC-Target-URI\":\"https://www.tutorialspoint.com/fraction-to-recurring-decimal-in-cplusplus\",\"WARC-Payload-Digest\":\"sha1:JOGROY5KC5NJZKWWZAXRLJEOB4SZFTYC\",\"WARC-Block-Digest\":\"sha1:MHEOBM43KPC7HN4SBYZGFGI4PDUTYGUQ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662525507.54_warc_CC-MAIN-20220519042059-20220519072059-00252.warc.gz\"}"}