Basic Data Types HackerRank solution in C++

Basic Data Types HackerRank solution in C++

Today we will give you the Basic Data Types HackerRank solution in C++. There are primitive data types and abstract data types in any programming language to learn more about them visit HERE.

Before going towards the problem let’s see the format specifiers and common bit widths of different data types.

Data TypesFormat SpecifiersCommon bit widths
int%d32
long int%ld64
char%c8
float%f32
double%lf64

Question

The link to the HackerRank Question is HERE.

Input Format

The input format consists of int, long int, character, float and double.

Output Format

Print each element on a new line in the same order it was received as input.

Note: I have used scanf and printf in my solution, you can use cin and cout too, but if you have to take numbers in million and want to print it out than scanf and printf are faster.

Example

  • Input: 2 345345345 a 45.3 34323.23423435
  • Output must be all in separate lines
  • 2
  • 345345345
  • a
  • 45.3
  • 34323.23423435

Basic Data Types HackerRank solution in C++

  • Starting Code in C++ will be


#include <iostream>
using namespace std;
int main()
{
 // Complete the code.
return 0;
}

  • Let’s create all the variables first.
#include <iostream>
using namespace std;
int main()
{
 // Complete the code.
    int a;
    long int b;
    char c;
    float d;
    double e;
return 0;
}
  • Take the input variables from the User.
#include <iostream>
using namespace std;
int main()
{
 // Complete the code.
    int a;
    long int b;
    char c;
    float d;
    double e;

    scanf("%d %ld %c %f %lf",&a,&b,&c,&d,&e);
return 0;
}
  • Print out all the Variables on Separate Lines.
#include <iostream>
using namespace std;
int main()
{
 // Complete the code.
    int a;
    long int 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;
}

That’s all. Test the Code and Check.

input: 3 12345678912345 a 334.23 14049.30493

Output: 
3
12345678912345
a
334.230
14049.304930000

Expected Output: 
3
12345678912345
a
334.230
14049.304930000

READ MORE

C++ related posts Visit HERE

Python-related posts Visit HERE

Databases related posts Visit HERE

Data Structures related posts visit HERE

Algorithms related posts visit HERE

Data Science related posts visit HERE

Share the Knowledge