Skip to main content

Featured

say hello world with C++ - Solution in Hacker Rank - hackerranksolutions8

  Objective This is a simple challenge to help you practice printing to  stdout . You may also want to complete  Solve Me First  in C++ before attempting this challenge. We’re starting out by printing the most famous computing phrase of all time! In the editor below, use either  printf  or  cout  to print the string  Hello ,World!  to  stdout . The more popular command form is  cout . It has the following basic form: cout<<value_to_print<<value_to_print; Any number of values can be printed using one command as shown. The  printf  command comes from C language. It accepts an optional format specification and a list of variables. Two examples for printing a string are: printf("%s", string);   printf(string); Note that neither method adds a newline. It only prints what you tell it to. Output Format Print   Hello ,World!   to stdout. Sample Output Hello, World! Solution:- //Say Hello, World! With C++ - Hacker Rank Solution #include <iostream> #include <cstdio

Basic Data Types in C++ – Solution in Hacker Rank - hackerranksolutions8

 

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 :

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

Comments

Popular Posts