Before going for a solution first clear the doubts on data type and their size, below is the data type and their size. Some C++ data types, their format specifiers, and their most common bit widths are as follows: or we can use an io-manip Header file to make an easy solution. Let's dive into and find out the hackerrank c++ basic data types solution within a few minutes.
Basic Data Types Hackerrank Solution Logic
Some C++ data types, their format specifiers, and their most common bit widths are as follows. But we will solve or find the solution of hackerrank basic data types solution c++ on a priority basis.
Int ("%d"): 32 Bit integer
Long ("%ld"): 64 bit integer
Char ("%c"): Character type
Float ("%f"): 32 bit real value
Double ("%lf"): 64 bit real value
Reading
To read a data type, use the following syntax:
To read a data type, use the following syntax:
scanf("format_specifier", &val)
For example, to read a character followed by a double:
char ch;
double d;
scanf("%c %lf", &ch, &d);
For the moment, we can ignore the spacing between format specifiers.
Printing(hackerrank basic data types solution c++)
To print a data type, use the following syntax:
Printing(hackerrank basic data types solution c++)
To print a data type, use the following syntax:
printf("format_specifier", val)
For example, to print a character followed by a double:
char ch = 'd';
double d = 234.432;
printf("%c %lf", ch, d);
Pro Hint: You can also use cin and cout instead of scanf and printf; however, if you are taking a million numbers as input and printing a million lines, it is faster to use scanf and printf. So that is the only reason we provide basic data types hackerrank solutions in c++ and C Language.
Basic Data Types Input/Output Format
Input Format
Input consists of the following space-separated values: int, long, char, float, and double, respectively.
Output Format
Print each element on a new line in the same order it was received as input. Note that the floating-point value should be correct up to 3 decimal places and the double to 9 decimal places.
Sample Input
3 12345678912345 a 334.23 14049.304933 12345678912345 a 334.23 14049.30493
Sample Output
3
12345678912345
a
334.230
14049.304930000
Explanation
Print int 3,
followed by long 12345678912345,
followed by char a,
followed by float 334.23,
followed by double 14049.30493.
Hint: To convert any Bits into Byte divide the Bits by 8, Because 1 Byte = 8 Bits. This point needs to remember to solve hackerrank basic data types solution c++.
Check: 30 Days of Code Hackerrank Solutions
Explanation of Basic Data Types in C++ Hackerrank Solution
As we know we have to use all data types and take user input and store the user input in all variables after that print the values of all data types.
Declare of Data Types
int integer;
long long1;//by default int long
long long long2; //by default int long long
char character;
float floatnumber;
double doublenumber;
Now take user input and store the value in variables.
cin>>integer;
cin>>long1;
cin>>long2;
cin>>character;
cin>>floatnumber;
cin>>doublenumber;
Now the last step is to print the value held by the Variable and add a return statement at the end. Our programming challenge program to basic data types hackerrank solution in c++ is done now.
cout << setprecision(20) << integer<< endl;
cout << setprecision(20) << long1<< endl;
cout << setprecision(20) << long2<< endl;
cout << setprecision(20) << character<< endl;
cout << setprecision(20) << floatnumber<< endl;
cout << setprecision(20) << doublenumber<< endl;
return 0;
But remember all output should be printed in the next line so do not forget to add an endl in the end.
Submit your solution here: Click here
Submit your solution here: Click here
Basic Data Types Hackerrank Solution in C++
#include <iostream>
#include <cstdio>
#include <iomanip>
using namespace std;
int main()
{
int integer;
long long1;//by default int long
long long long2; //by default int long long
char character;
float floatnumber;
double doublenumber;
cin>>integer;
cin>>long1;
cin>>long2;
cin>>character;
cin>>floatnumber;
cin>>doublenumber;
cout << setprecision(20) << integer<< endl;
cout << setprecision(20) << long1<< endl;
cout << setprecision(20) << long2<< endl;
cout << setprecision(20) << character<< endl;
cout << setprecision(20) << floatnumber<< endl;
cout << setprecision(20) << doublenumber<< endl;
return 0;
}
Hackerrank Basic Data Types Solution In C
#include <iostream>
#include <cstdio>
int main()
{
int a;
long int b;
long long int c;
char d;
float e;
double f;
scanf("%d %ld %lld %c %f %lf",&a,&b,&c,&d,&e,&f);
printf("%d\n%ld\n%lld\n%c\n%f\n%lf",a,b,c,d,e,f);
return 0;
}
0 Comments: