
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
- if remainder is not in m, then −
- return ans as string
Example
Let us see the following implementation to get better understanding −
#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)
- Related Articles
- Decimal representation of a rational number can not be......
- Complete the following sentences:The decimal representation of a rational number is either… or…
- Show that $3.142678$ is a rational number.
- Swift Program to get the denominator from a rational number
- Swift Program to get the numerator from a rational number
- Golang Program to get the denominator from a rational number
- Golang Program to get the numerator from a rational number
- Haskell Program to get the numerator from a rational number
- Haskell Program to get the denominator from a rational number
- Java program to convert decimal number to hexadecimal number
- Java Program to convert binary number to decimal number
- Java Program to convert hexadecimal number to decimal number
- Java Program to convert octal number to decimal number
- Golang Program to convert a number into a rational number
- How to convert a decimal number to a rational number with denominator to the power of 10?

Advertisements