Basic Data Types in C++ - Hacker Rank Solutions

Hey Coders, Today we will learn about some basic Data Types of C++ and how to use them in our program to get the desire output.

Basic Data Types in C++ - Hacker Rank Solutions

Hey Coders, Today we will learn about some basic Data Types of C++ and how to use them in our program to get the desire output.

We will be solving on of the Hackerrank problem (Basic Data Types in C++) which will describe us what basically data types are and how to implement them in our code.

So, Without further ado let's jump into the problem.

Basic Data Types in C++ - Hacker Rank Solutions

Problem

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

followed by long 12345678912345,

followed by char a

followed by float 334.23,

followed by double 14049.30493.

Solution - Basic Data Types in C++

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

int main() {
    // Complete the code.
    int a;
    long b;
    char c;
    float d;
    double e;

    scanf("%d %ld %c %f %lf", &a, &b, &c, &d, &e);
    printf("%d\n%ld\n%c\n%f\n%lf", a, b,c, d, e);
    return 0;
}
Basic Data Types in C++ - Hacker Rank Solutions
Basic Data Types in C++ - Hacker Rank Solution

Basic Data Types in C++ - Hacker Rank Solutions
Basic Data Types in C++ - Result

Conclusion

Today we had learned about different types of basic data types and how to implement them in input and output function to get the desire output.

Practice few more problems related to data types and you will master this topic. The above problem is generated by HackerRank but the solution is provided by Sloth Coders.

If you have any doubt regarding the solution of above problem ( Basic Data Types in C++ ), feel free to contact in the Comment section.

Share this post with your coder friends and relatives and Don't forget to Subscribe Our Blog to get latest updates related to coding.

Happy Coding!!

A Sloth Who loves to Code.

Sloth Coders is a Learning Platform for Coders, Programmers and Developers to learn from the basics to advance of Coding of different langauges(python, Java, Javascript and many more).

1 comment

  1. I don't understand why this code uses C lang Input output syntax and it works but when i use cin and cout it doesn't work.
    it does not execute the negative value of double.