Monday, 14 March 2016

Hacker Rank solution for Basic Data Types

Problem:- Hacker Rank Solution Program In C++ For " Basic Data Types " or Hacker Rank Solution Program In C++ ForBasic Data Types or Basic Data Types solution hacker rank or Hacker rank solution for c++ domain or Hacker rank solution for basic Data Types sub domain or Basic Data Types solution in c++ of hacker rank or introduction solutions hacker rank.

Check Here:- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.

Logic:- for this problem there is no logic we have just simplify all data type and print all value, so for this, we can take an example of all data types and print the output of 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.

Int ("%d"): 32 Bit integer or 4 Byte integer
Long ("%ld"): 64 bit integer 8 Byte integer
Char ("%c"): Character type 
Float ("%f"): 32 bit real value 4 Byte integer
Double ("%lf"): 64 bit real value 8 Byte integer

Hint:- For converting any Bits into Byte divided the Bits by 8, the reason is very simple cause 1 Byte = 8 Bits.

Explanation:- So as we know we have to use all data types and taking a 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 a 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 hold by Variable and add return statement in the end.

    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 out put should be print in the next line so do not forget to add endl in the last. A bonus point is for all reader you can also find This problem solution in C language.

Also Check- Hacker rank solution for Strings, Classes, STL, Inheritance in C++.

Copy the full solution or colored code and paste into the hacker rank editor and click to Run Code if the code runs successfully then click to Submit Code. Before pasting the code into the editor make sure you have chosen C++.

Submit your solution here:- Click here

Tip:- Before copy the program I recommended please read this full article, this will help you to build your own logic. 


If you have any query feel free to share with me, and if you like my work please share, comments and subscribe for our new and latest post related to programming this will help you update all about new post this Article.

Solution:-

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

}


2. 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;

}

Output:- 

1. C++ Program Output

Hacker Rank solution for Basic Data Types

2. C Program Output

Hacker Rank solution for Basic Data Types

2 comments:
Write comments
  1. if we use cout and cin intead of scanf anf printf it is giving error. cam anyone tell me why?

    ReplyDelete
    Replies
    1. Hii AARUSHI
      C is closer to the machine circuit, hardware, etc than C++ is. But at the same time C++ "arguably" allows more direct manipulation of your data.you can use iomanip to solve the problem i if you are good in webtechnology Click Here

      Delete

Recommended Posts × +