Search⌘ K
AI Features

Fraction to Recurring Decimal

Explore how to convert a fraction given by numerator and denominator into its decimal string representation. Learn to identify repeating decimal sequences by using hash maps to efficiently track remainders and detect cycles. This lesson helps you implement a function to return non-repeating and recurring decimals with parentheses around repeating parts.

Statement

Given the two integer values of a fraction, numerator and denominator, implement a function that returns the fraction in string format. If the fractional part repeats, enclose the repeating part in parentheses.

Constraints:

  • denominator !=0!= 0
  • 105-10^{5} \leq numerator, denominator 1051\leq 10^{5} - 1

Examples

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Fraction to Recurring Decimal

1.

Select the correct answer for the fraction given below:

20/4

A.

“2”

B.

“3”

C.

“4”

D.

“5”


1 / 3

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5
6
7

Try it yourself

Implement your solution in the following coding playground.

C++
usercode > main.cpp
#include <iostream>
#include <string>
string FractionToDecimal(int numerator, int denominator)
{
// Replace this placeholder return statement with your code
return "";
}
Fraction to Recurring Decimal