Basic Data Types in C++ - Hacker Rank Solution


Basic Data Types in C++ - Hacker Rank Solution



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.230.
followed by double 14049.304930000.



Solution :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cstdio>
using namespace std;

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

 scanf("%d %lld %c %f %lf",&a,&b,&c,&d,&e);

 printf("%d\n%lld\n%c\n%f\n%lf",a,b,c,d,e);

return 0;
}



Disclaimer :-
the hole problem statement are given by HackerRank.com but the solution are generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.

Next Post Previous Post
12 Comments
  • Abuzar
    Abuzar Tuesday, November 16, 2021

    not correct i think so

  • Ankit Kumar Pandey
    Ankit Kumar Pandey Saturday, January 15, 2022

    But, its c++ ,is it correct to write input and output statement of C language???

    • Anonymous
      Anonymous Monday, December 12, 2022

      #include
      #include
      using namespace std;

      int main() {
      // Complete the code.
      int a;
      long b;
      char c;
      float d;
      double e;
      cin>>a>>b>>c>>d>>e;
      cout<<a<<endl<<b<<endl<<c<<endl;
      cout.precision(3);
      cout<<fixed<<d<<endl;
      cout.precision(9);
      cout<<fixed<<e<<endl;
      return 0;
      }

  • Unknown
    Unknown Wednesday, February 09, 2022

    #include
    #include
    #include
    using namespace std;

    int main() {

    int a;
    long b;
    char c;
    float d;
    double e;
    cin >> a >> b >> c >> d >> e;
    cout<< a << '\n' << b << '\n' << c << '\n';
    cout << std::fixed << std::setprecision(3) << d << '\n';
    cout << std::fixed << std::setprecision(9) << e << '\n';

    return 0;
    }

  • Nagendra Prajapati
    Nagendra Prajapati Thursday, February 17, 2022

    #include
    using namespace std;

    int main() {

    int a;
    long b;
    char c;
    float d;
    double e;
    cin >> a >> b >> c >> d >> e;
    cout<< a << '\n' << b << '\n' << c << '\n';
    cout << std::fixed << std::setprecision(3) << d << '\n';
    cout << std::fixed << std::setprecision(9) << e << '\n';

    return 0;
    }




    //techdark.in

  • Unknown
    Unknown Monday, April 04, 2022

    #include
    #include
    using namespace std;

    int main() {
    int a;
    long long int b;
    char c;
    float s;
    double d ;
    scanf("%d %lld %c %f %lf",&a,&b,&c,&s,&d);

    printf("%d\n%lld\n%c\n%f\n%lf",a,b,c,s,d);

    return 0;
    }

    • Anonymous
      Anonymous Wednesday, June 08, 2022

      Plz can anyone tell me why float and double value showes 5 decimal points respectively when we don't use long long int but instead use only long

  • Anonymous
    Anonymous Saturday, June 11, 2022

    My Test case 0 was pass but another cases are fail, what is the problem?

  • Anonymous
    Anonymous Monday, June 13, 2022

    #include
    #include
    using namespace std;

    int main() {
    int a;
    long b;
    char c;
    float d;
    double e;
    cin>>a>>b>>c>>d>>e;

    cout<<a<<endl<<b<<endl<<c<<endl<<d<<endl<<e<<endl;


    return 0;
    }

  • Anonymous
    Anonymous Saturday, July 02, 2022

    this is not write ans for all test case

    • Anonymous
      Anonymous Monday, July 25, 2022

      exactly

  • Anonymous
    Anonymous Monday, November 07, 2022

    same my test case 0 is pass but 1,2,3 are not pass

Add Comment
comment url