#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
int main() {
int i;
long l;
char c;
float f;
double d;
// Read input values
cin >> i >> l >> c >> f >> d;
// Output values
cout << i << endl;
cout << l << endl;
cout << c << endl;
cout << fixed << setprecision(3) << f << endl;
cout << fixed << setprecision(9) << d << endl;
return 0;
}
Let's break down the code and explain each part:
#include
#include
#include
using namespace std;: This statement allows us to use the standard library functions without explicitly specifying the namespace (e.g., cout, cin).
int main(): This is the main function of the program. It is the entry point for execution.
int i; long l; char c; float f; double d;: We declare variables of different data types to store the input values. int for integer, long for long integer, char for character, float for floating-point, and double for double-precision floating-point.
cin >> i >> l >> c >> f >> d;: This statement reads input values from the user and assigns them to the respective variables using the >> operator.
cout << i << endl;: This line outputs the value of i followed by a newline character (endl) to the console.
cout << l << endl;: This line outputs the value of l followed by a newline character.
cout << c << endl;: This line outputs the value of c followed by a newline character.
cout << fixed << setprecision(3) << f << endl;: This line outputs the value of f with a fixed number of decimal places using setprecision(). The fixed manipulator ensures that the floating-point number is displayed in fixed-point notation.
cout << fixed << setprecision(9) << d << endl;: This line outputs the value of d with a fixed number of decimal places using setprecision(). It sets the precision to 9 for double-precision floating-point.
return 0;: This line indicates that the program execution is successful and returns the value 0 to the operating system.
When you run this program, it will prompt the user to enter values of different data types. After the user enters the values, the program will display them as output, following the specified format.
To use this code in HackerRank's "Basic Data Types" problem, you can copy and paste it into the editor and submit it. The program will prompt the user for input, and the output will be displayed in the output window or terminal, depending on the HackerRank environment.
Remember to include the necessary header files (#include