Hackerrank Basic Data Types Solution
2 min read

Hackerrank Basic Data Types Solution

Hackerrank Basic Data Types Solution

Some C++ data types, their format specifiers, and their most common bit widths are as follows:

  • 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
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);

Note: 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.

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.30493

Sample Output.

3
12345678912345
a
334.230
14049.304930000

Explanation.

Print int ,
followed by long ,
followed by char ,
followed by float ,
followed by double .

Solution in cpp

Approach 1.

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

int main() {
    // Complete the code.
    int a;
    long l; char c;
    float  f; double d;
    cin>>a>>l>>c>>f>>d;
    cout<<a<<endl<<l<<endl<<c<<endl;
    cout.precision(3);
    cout<<fixed<<f<<endl;
    cout.precision(9);
    cout<<fixed<<d;
    return 0;
}

Approach 2.

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

int main() {
 int a;
    long b;
    char x;
    float y;
    double z;
    scanf("%d %ld %c %f %lf",&a,&b,&x,&y,&z);
    printf("%d\n%ld\n%c\n%f\n%lf",a,b,x,y,z);
    // Complete the code.
    return 0;
}

Approach 3.

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

int main() {
    int i;
    long l;
    char c;
    float f;
    double d;
    scanf("%d %ld %c %f %lf", &i, &l, &c, &f, &d);

    printf("%d\n%ld\n%c\n%f\n%lf", i, l, c, f, d);
    return 0;
}

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×