Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- cout << "--------------------------------------------\n";
- cout << "Enter a PROPER fraction\n";
- cout << "[Numerator MUST be smaller than Denominator]\n";
- cout << "--------------------------------------------\n";
- cout << "Enter numerator: ";
- int x;
- cin >> x;
- cout << "Enter denominator: ";
- int y;
- cin >> y;
- cout << "--------------------------------------------\n";
- cout << "Fraction = " << x << " / " << y << '\n';
- cout << "--------------------------------------------\n";
- cout << "Enter number of decimal places to TRUNCATE: ";
- int n;
- cin >> n;
- x *= 10;
- int cnt = 0;
- vector<int> decimals;
- while (cnt < n) {
- int quotient = x / y;
- decimals.emplace_back(quotient);
- x -= (y * quotient);
- x *= 10;
- cnt++;
- }
- cout << "--------------------------------------------\n";
- cout << "After TRUNCATING " << n << " decimal places:\n";
- cout << "0.";
- for (int i = 0; i < (int) decimals.size(); i++) {
- cout << decimals[i];
- }
- cout << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement