Monday, 14 March 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 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 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

Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

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