07/07/2021

Basic Data Types Hackerrank Solution in C++ & C Language

We are providing Basic Data Types Hackerrank Solution in C++ programming language as well as in C language for a better understanding of the programming challenges in both languages. In this problem there is no logic we have just simplified all data types and print all values, so for this, we can take an example of all data types and print the output of the variable.

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 in C++

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:

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:

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

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;
}

Hackerrank Basic Data Types Solution C++ Output


Hackerrank Basic Data Types Solution C++ Output

Basic Data Types Solution Output in C

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: