- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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.

- Related Questions & Answers
- C program to represent numbers in numerator and denominator in string format
- C program to represent the numbers in spiral pattern
- Java program to calculate the product of two numbers
- Java program to find the LCM of two numbers
- Java Program to Add the two Numbers
- C program to convert decimal fraction to binary fraction
- Java program to print the Armstrong numbers between two numbers
- PHP program to calculate the repeated subtraction of two numbers
- C++ Program for the Common Divisors of Two Numbers?
- Java program to find the GCD or HCF of two numbers
- Python Program to Find the Product of two Numbers Using Recursion
- Java Program to Find the Product of Two Numbers Using Recursion
- C program to represent the alphabets in spiral pattern
- Java Program to format a string
- C++ program to count minimum number of binary digit numbers needed to represent n
- C program to display the prime numbers in between two intervals