Algorithm


 Problem Name: 30 days of code - Day 1: Data Types

Problem Link:https://www.hackerrank.com/challenges/30-data-types/problem?isFullScreen=true

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 3 variables
  3. Use the + operator to perform the following operations:
  • Print the sum of i plus your int variable on a new line.
  • Print the sum of d plus your double variable to a scale of one decimal place on a new line.
  • Concatenate s 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 i.

The second line contains a double that you must sum with d.

he third line contains a string that you must concatenate with s.

Output Format

Print the sum of both integers on the first line, the sum of both doubles (scaled 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 integersand 12,  we get the integer 16.

When we sum the floating-point numbers 4.0 and 4.0 get 8.0.

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

 

 

 

Code Examples

#1 Code Example with C Programming

Code - C Programming

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

int main() {
    int i = 4;
    double d = 4.0;
    char s[] = "HackerRank ";

    // Declare second integer, double, and String variables.
    int second_integer;
    double second_double;
    char second_string[100];
    
    // Read and save an integer, double, and String to your variables.
    scanf("%d\n", &second_integer);
    scanf("%lf\n", &second_double);
    fgets(second_string, 100, stdin);

    // Print the sum of both integer variables on a new line.
    printf("%d\n", i + second_integer);
    
    // Print the sum of the double variables on a new line.
    printf("%.1lf\n", d + second_double);
    
    // Concatenate and print the String variables on a new line
    // The 's' variable above should be printed first.
    printf("%s%s", s, second_string);

    return 0;
}
Copy The Code & Try With Live Editor

Input

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

Output

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

#2 Code Example with C++ Programming

Code - C++ Programming

#include <iostream>
using namespace std;

int main() {
    int i = 4;
    double d = 4.0;
    string s = "HackerRank ";

    // Declare second integer, double, and String variables.
    int i2;
    double d2;
    string s2;

    // Read and save an integer, double, and String to your variables.
    string tmp;

    getline(cin, tmp);
    i2 = stoi(tmp);

    getline(cin, tmp);
    d2 = stod(tmp);

    getline(cin, s2);

    // Print the sum of both integer variables on a new line.
    i=i+i2;
    cout<<i<<endl;

    // Print the sum of the double variables on a new line.
    // Used setprecision(1) to get at least 1 digit after decimal point
    d=d+d2;
    cout<<fixed<<setprecision(1)<<d<<endl;

    // Concatenate and print the String variables on a new line
    // The 's' variable above should be printed first.
    cout << s << s2 << endl;

    return 0;
}
Copy The Code & Try With Live Editor

Input

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

Output

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

#3 Code Example with C# Programming

Code - C# Programming

using System;
class Solution
{
    static void Main(String[] args)
    {
        int i = 4;
        double d = 4.0;
        string s = "HackerRank ";

        // Declare second integer, double, and String variables.
        int i2;
        double d2;
        string s2;

        // Read and save an integer, double, and String to your variables.
        i2 = int.Parse(Console.ReadLine());
        d2 = double.Parse(Console.ReadLine());
        s2 = Console.ReadLine();

        // Print the sum of both integer variables on a new line.
        Console.WriteLine(String.Format("{0:0}", i + i2));

        // Print the sum of the double variables on a new line.
        Console.WriteLine(String.Format("{0:0.0}", d + d2));

        // Concatenate and print the String variables on a new line
        // The 's' variable above should be printed first.
        Console.WriteLine(s + s2);
    }
}
Copy The Code & Try With Live Editor

Input

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

Output

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

#4 Code Example with Java Programming

Code - Java Programming

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        int i = 4;
        double d = 4.0;
        String s = "HackerRank ";

        Scanner scan = new Scanner(System.in);

        /* Declare second integer, double, and String variables. */
        int i2;
        double d2;
        String s2;

        /* Read and save an integer, double, and String to your variables.*/
        i2 = scan.nextInt();
        d2 = scan.nextDouble();

        scan.nextLine();
        s2 = scan.nextLine();

        /* Print the sum of both integer variables on a new line. */
        System.out.println(i + i2);

        /* Print the sum of the double variables on a new line. */
        System.out.println(d + d2);

        /* Concatenate and print the String variables on a new line;
            the 's' variable above should be printed first. */
        System.out.println(s.concat(s2));

        scan.close();
    }
}
Copy The Code & Try With Live Editor

Input

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

Output

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

#5 Code Example with Javascript Programming

Code - Javascript Programming

var length = 16;  // Number
var lastName = "Johnson";  // String
var x = {firstName:"John", lastName:"Doe"}; // Object
Copy The Code & Try With Live Editor

Input

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

Output

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

#6 Code Example with PHP Programming

Code - PHP Programming

// Declare second integer, double, and String variables.
$f='fgets';

// Read and save an integer, double, and String to your variables.
$i2 = $f(STDIN);
$d2 = $f(STDIN);
$s2 = $f(STDIN);

// Print the sum of both integer variables on a new line.
echo ($i + $i2) . "\n";

// Print the sum of the double variables on a new line.
echo number_format($d + $d2, 1) . "\n";

// Concatenate and print the String variables on a new line
// The 's' variable above should be printed first.
echo $s . $s2 . "\n";
Copy The Code & Try With Live Editor

Input

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

Output

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

#7 Code Example with Python Programming

Code - Python Programming

i = 4
d = 4.0
s = 'HackerRank '

# Declare second integer, double, and String variables.
i2 = None
d2 = None
s2 = None

# Read and save an integer, double, and String to your variables.
i2 = int(input())
d2 = float(input())
s2 = str(input())

# Print the sum of both integer variables on a new line.
print(i + i2)

# Print the sum of the double variables on a new line.
print(d + d2)

# Concatenate and print the String variables on a new line
# The 's' variable above should be printed first.
print(s + s2)
Copy The Code & Try With Live Editor

Input

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

Output

x
+
cmd
16
8.0
HackerRank is the best place to learn and practice coding!
Advertisements

Demonstration


[Solved] Day 1: Data Types solution in Hackerrank - Hacerrank solution C, C++, C#, java, Js, PHP,  Python in 30 days of code

Previous
[Solved] Day 0: Hello, World solution in Hackerrank - Hacerrank solution C, C++, C#, java, Js, Python in 30 days of code
Next
[Solved] Day 2: Operators solution in Hackerrank - Hacerrank solution C, C++, C#, java, Js, PHP, Python in 30 days of code