3/14/2016

Hacker Rank Solution for Basic Data Types In C++

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 subdomain or Basic Data Types solution in c++ of hacker rank or introduction solutions hacker rank.

Sponsor Post: "6 Smart investment Tips जिसे प्रत्येक व्यक्ति को अपने से कम उम्र के नवजवानों को जरूर बताना चाहिए, और यदि वह युवा इन Smart investment tips को follow कर लेता है तो उसको सफल होने से कोई नहीं रोक सकता है. Visit For More Financial Tips / Motivational Stories & Inspirational Quotes.

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


Submit your solution here:- Click here


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:

  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