Print Pretty in C++ – Hacker Rank Solution | HackerRank Programming Solutions | HackerRank C++ Solutions

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language C++ . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight you profile to the recruiters.

In this post, you will find the solution for Print Pretty in C++-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Introduction To C++

C++ is a general-purpose programming language that was developed as an enhancement of the C language to include object-oriented paradigm. It is an imperative and a compiled language. 

C++ is a middle-level language rendering it the advantage of programming low-level (drivers, kernels) and even higher-level applications (games, GUI, desktop apps etc.). The basic syntax and code structure of both C and C++ are the same. 

C++ programming language was developed in 1980 by Bjarne Stroustrup at bell laboratories of AT&T (American Telephone & Telegraph), located in U.S.A. Bjarne Stroustrup is known as the founder of C++ language.

Print Pretty in C++  - Hackerrank Solution

Problem

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 :

1  
100.345 2006.008 2331.41592653498

Sample Output :

0x64             
_______+2006.01  
2.331415927E+03

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++ – Hacker Rank Solution
#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 */
	    /* 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;

}

Leave a Comment

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Refresh