Print Pretty in C++ HackerRank Solution

Hello Programmers, In this post, you will know how to solve the Print Pretty in C++ HackerRank Solution. This problem is a part of the HackerRank C++ Programming Series.

Print Pretty in C++ HackerRank Solution
Print Pretty in C++ HackerRank Solutions

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

Print Pretty in C++ HackerRank Solution


Your manager gave you a text file with many lines of numbers to format and print. For each row of 3 space-separated doubles, format and print the numbers using the specifications in the Output Format section below.

Input Format :

The first line contains an integer, T, the number of test cases.
Each of the T subsequent lines describes a test case as 3 space-separated floating-point numbers: AB, and C, respectively.

Constraints :

  • 1 <= T <= 1000
  • Each number will fit into a double.

Output Format :

For each test case, print 3 lines containing the formatted AB, and C, respectively. Each AB, and C must be formatted as follows:

  1. A : Strip its decimal (i.e., truncate it) and print its hexadecimal representation (including the 0x prefix) in lower case letters.
  2. B : Print it to a scale 2 of decimal places, preceded by a + or  sign (indicating if it’s positive or negative), right justified, and left-padded with underscores so that the printed result is exactly 15 characters wide.
  3. C : Print it to a scale of exactly nine decimal places, expressed in scientific notation using upper case.

Sample Input :

100.345 2006.008 2331.41592653498

Sample Output :


Explanation :

For the first line of output, (100)10 -> (64)16 (in reverse, 6*16^1+4*16^0 = (100)10).
The second and third lines of output are formatted as described in the Output Format section.

Print Pretty in C++ HackerRank Solution

#include <iostream>
#include <iomanip>
using namespace std;
int main()
	int T; cin >> T;
	cout << setiosflags(ios::uppercase);
	cout << setw(0xf) << internal;
	    double A; cin >> A;
	    double B; cin >> B;
	    double C; cin >> C;
	/* Enter your code here */
	    /* Print Pretty in C++ - Hacker Rank Solution START */
            cout << std::hex << std::left << std::showbase << std::nouppercase;
            cout << (long long) A << endl;
            cout << std::dec << std::right << std::setw(15) << std::setfill('_') << std::showpos << std::fixed << std::setprecision(2);
            cout << B << endl;
            cout << std::scientific << std::uppercase << std::noshowpos << std::setprecision(9);
            cout << C << endl;
         /* Print Pretty in C++ - Hacker Rank Solution END */
	return 0;

Disclaimer: The above Problem (Print Pretty in C++) is generated by Hackerrank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: Maps-STL in C++ HackerRank Solution

Leave a Reply

Your email address will not be published. Required fields are marked *