Day 1: Data Types - Hackerrank Solution

Day 1: Data Types - Hackerrank Solution 

Welcome Back guys!!
Today in this post we will discuss Day 1: Data Types Hackerrank Problem Solution.
The objective of this post is to make code acquaint with basic data type like string ,int and doubles....


Problem Statement of Day 1: Data Types - Hackerrank Problem :

Objective

Today, we're discussing data types. Check out the Tutorial tab for learning materials and an instructional video!


Task

Complete the code in the editor below. The variables i,d and s are already declared and initialized for you. You must:

1. Declare 3 variables: one of type int, one of type double, and one of type String.
2. Read 3 lines of input from stdin (according to the sequence given in the Input Format section below) and initialize your  variables.
3. Use the + operator to perform the following operations:
   1.Print the sum of  plus your int variable on a new line.
   2.Print the sum of  plus your double variable to a scale of one decimal place on a new line.
   3.Concatenate  with the string you read as input and print the result on a new line.
Note: If you are using a language that doesn't support using  for string concatenation (e.g.: C), you can just print one variable immediately following the other on the same line. The string provided in your editor must be printed first, immediately followed by the string you read as input.


Input Format


The first line contains an integer that you must sum with .
The second line contains a double that you must sum with .
The third line contains a string that you must concatenate with .

Output Format

Print the sum of both integers on the first line, the sum of both doubles (scaled 1 to  decimal place) on the second line, and then the two concatenated strings on the third line.

Sample Input

12
4.0
is the best place to learn and practice coding!

Sample Output

16
8.0
HackerRank is the best place to learn and practice coding!

Explanation

When we sum the integers 4 and 12, we get the integer 16.
When we sum the floating-point numbers 4.0 and 4.0, we get 8.0.
When we concatenate HackerRank with is the best place to learn and practice coding!, we get HackerRank is the best place to learn and practice coding!.


You will not pass this challenge if you attempt to assign the Sample Case values to your variables instead of following the instructions above and reading input from stdin.


Solution Code of Day 1: Data Types - Hackerrank Problem :

For this explanation, the variables provided in your editor are subscripted with a , and the corresponding variables declared and initialized by you are subscripted with .
The following variables are already declared and initialized in your editor:
, and .
In the first test case, the following variables were read from stdin:
, and .
Our first line of output is the result of .
Our second line of output is the result of .
Our third line of output is the result of .

Code in C programing language:


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int i = 4;
    double d = 4.0;

    char s[] = "HackerRank ";
     int i1;
    double d1;
    char s1[100]; // this is not scalable for input of unknown size

// Read inputs from stdin
scanf("%d", &i1);
scanf("%lf", &d1);
scanf("%*[\n] %[^\n]",s1);// or gets(s2); 
//*[\n] this character is used to ignore the new line character entered after float input.

// Print outputs to stdout
printf("%d\n", i + i2);
printf("%.01lf\n", d + d2);

printf("%s%s", s, s2);
return 0;
}



Code in C++ programing language:


#include <iostream>
#include <iomanip>
#include <limits>

using namespace std;

int main() {
    int i = 4;
    double d = 4.0;
    string s = "HackerRank ";
    string str {};
    int a;
    double b{0.0};
    cin>>a;
    cin>>b;
    cin.ignore();
    // to ignore the new line character (enter key) after the double input 
    getline(cin,str);
    cout<<a+i<<endl;
    cout<<fixed<<setprecision(1)<<b+d<<endl;
    cout<<s + str ;   
    return 0;

}


Code in python programing language:


i = 4
d = 4.0
s = 'HackerRank '
# Declare second integer, double, and String variables.
a =0
b =0
# Read and save an integer, double, and String to your variables.
a=int(input())
b= float(input())
string = input()
# Print the sum of both integer variables on a new line.
print(a+i)
# Print the sum of the double variables on a new line.
print(b +d)
# Concatenate and print the String variables on a new line
print(s + string)

# The 's' variable above should be printed first.

Hope this added valuable knowledge to you..
Feel Free to share your thoughts and doubts in the comment section below
Link to my youtube page : https://www.youtube.com/watch?v=aBZRBCx-LMg
See you next time.

Post a Comment

4 Comments

  1. Thanks for the solution for java solution u can check hereSolution in java

    ReplyDelete
  2. NOT print(s + string)
    IT IS print(s,string)

    THERE SPACE BETWEEN 'HackerRank' and 'is'

    ReplyDelete
  3. NOT print(s + string)
    IT IS print(s,string)

    THERE SPACE BETWEEN 'HackerRank' and 'is'

    ReplyDelete
  4. anyone please explain me this line of code meaning
    "cin.ignore();"
    in c++ solution

    ReplyDelete