25/03/2023

Print Pretty Hackerrank Solution in C++ | STL Solution

Print Pretty Hackerrank Solution in C++. Given 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.

Print Pretty Hackerrank Solution in C++

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: A, B, 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 A, B, and C, respectively. Each A, B, 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 lowercase letters.
  2. B: Print it to a scale of 2 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

1  
100.345 2006.008 2331.41592653498

Sample Output

0x64             
_______+2006.01  
2.331415927E+03

Submit your solution here: Click here

Print Pretty Hackerrank Solution in C++


#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int T;
	cin >> T;

	cout << setiosflags(ios::uppercase);
	cout << setw(0xf) << internal;

	while (T--)
	{
		double A;
		cin >> A;
		double B;
		cin >> B;
		double C;
		cin >> C;

		/*Enter your code here */
		cout << resetiosflags(ios::uppercase | ios::scientific);
		cout << std::setiosflags(ios::left | ios::fixed);
		cout << setw(0) << std::hex;
		cout << std::setiosflags(ios::showbase);
		cout << (unsigned long) A << std::endl;

		cout << resetiosflags(ios::showbase);
		cout << setw(0xf) << setfill('_') << std::dec;
		cout << setiosflags(ios::showpos) << setprecision(2);
		cout << B << endl;

		cout << resetiosflags(ios::fixed | ios::showpos);
		cout << setiosflags(ios::scientific | ios::uppercase) << setprecision(9);
		cout << C << std::endl;

		/*code ends here*/
	}

	return 0;
}

The Output of Print Pretty Hackerrank Solution


The Output of Print Pretty Hackerrank Solution

Similar to Print Pretty


Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: