- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- 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 find rounded numbers to save kingdom

Suppose we have a string S where a decimal real number is given. There is a king called Amal, and his son Bimal is in a kingdom. There are other citizens also. They lived happily until great trouble came into the Kingdom. The monsters settled there. Most damage those strange monsters inflicted upon the kingdom was that they loved high precision numbers. As a result, the Kingdom healers had already had three appointments with the merchants who sell, exactly 0.273549107 potion barrels. To deal with the problem somehow, the King Amal issued an order obliging rounding up all numbers to the closest integer to simplify calculations. Specifically, the order went like this −

If a number's integer part does not end with the digit 9 and its fractional part is strictly less than 0.5, then the rounded-up number coincides with the number’s integer part.

If a number's integer part does not end with the digit 9 and its fractional part is not less than 0.5, the rounded-up number is obtained if 1 is added to the last digit of the number’s integer part.

If the number’s integer part ends with the digit 9, to round up the numbers, report that as "Not possible".

We have to implement the logic to get the rounded number.

## Problem Category

To solve this problem, we need to manipulate strings. Strings in a programming language are a stream of characters that are stored in a particular array-like data type. Several languages specify strings as a specific data type (eg. Java, C++, Python); and several other languages specify strings as a character array (eg. C). Strings are instrumental in programming as they often are the preferred data type in various applications and are used as the datatype for input and output. There are various string operations, such as string searching, substring generation, string stripping operations, string translation operations, string replacement operations, string reverse operations, and much more. Check out the links below to understand how strings can be used in C/C++.

https://www.tutorialspoint.com/cplusplus/cpp_strings.htm

https://www.tutorialspoint.com/cprogramming/c_strings.htm

So, if the input of our problem is like S = "3.14159", then the output will be 3

## Steps

To solve this, we will follow these steps −

ret := a blank string p := position of '.' in S if S[p - 1] is same as '9', then: return "Not Possible" Otherwise ret := ret concatenate (substring of S from index 0 to p-1) if S[p+1] < '5', otherwise (substring of S from index 0 to p-2) concatenate (S[p - 1] + 1) return ret

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; string solve(string S){ string ret = ""; int p = S.find('.'); if (S[p - 1] == '9') return "Not Possible"; else ret += (S[p + 1] < '5' ? S.substr(0, p) : S.substr(0, p - 1) + string(1, (S[p - 1] + 1))); return ret; } int main(){ string S = "3.14159"; cout << solve(S) << endl; }

## Input

"3.14159"

## Output

3

- Related Articles
- C++ Program to Find Fibonacci Numbers using Recursion
- C++ Program to Find Factorial of Large Numbers
- C++ Program to Find Fibonacci Numbers using Iteration
- Java Program to save decimal
- Java Program to create rounded borders in Swing
- C++ Program to Find Largest Number Among Three Numbers
- Program to find first N Iccanobif Numbers in C++
- C# program to find the maximum of three numbers
- C++ Program to Find Fibonacci Numbers using Matrix Exponentiation
- C++ Program to Find Fibonacci Numbers using Dynamic Programming
- C program to convert roman numbers to decimal numbers
- C++ program to Find Sum of Natural Numbers using Recursion
- Program to find GCD of floating point numbers in C++
- C program to Find the Largest Number Among Three Numbers
- Program to find LCM of two Fibonnaci Numbers in C++