# Find recurring sequence of a fraction in C++

So, guys today we will learn How to find the recurring sequence of a fraction in C++. Let’s do it together.

A recurring decimal is a decimal number that has a pattern then repeats over and over after the decimal place. The topic is basically to find the decimal value that is repeated.

Here is the solution to your question. Let’s understand it better with an example. For eg:

2/3 = 0.66666…..

The recurring sequence of a fraction is 6.

2/11 = 0.181818…..

The recurring sequence of a fraction is 18.

In the second example, we first got 1 as a remainder further, we multiplied it by 10 then we calculated it ahead until we got 1 again. After this, we started the second iteration and calculated until we got 1 again, then on checking further, we got 18.

#include <bits/stdc++.h> using namespace std; string fractionToDecimal(int num, int den) { string r; map <int, int> mp; mp.clear(); int rem = num%den; while ( (rem!=0) && (mp.find(rem) == mp.end()) ) { mp[rem] = r.length(); rem = rem*10; int res = rem / den; r += to_string(res); rem = rem % den; } return (rem == 0)? "" : r.substr(mp[rem]); } int main() { int num = 2, den = 11; string r = fractionToDecimal(num, den); if (r == "") cout << "No recurring sequence"; else cout << "Recurring sequence is " << r; return 0; }

Output :

Recurring sequence is 18

That is how we calculated the Recurring sequence of a Fraction in C++.

But I guess you are still hungry so feed yourself with some amazing lines of code. Have a look!

## Leave a Reply