30 Days Of Code HackerRank solution 02 in c/c++/java/python



Day 1: "Data Type" Problem


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

  1. Declare  variables: one of type int, one of type double, and one of type String.
  2. Read  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 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  and , we get the integer .
When we sum the floating-point numbers  and , we get .
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!.


Solution:


1. Code in C

#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 j;
    double f;
    char t[100];
    // Read and save an integer, double, and String to your variables.
    scanf("%d",&j);
    scanf("%lf",&f);
    scanf("%*[\n] %[^\n]",t);
    // Print the sum of both integer variables on a new line.
    printf("%d\n",i+j);
    // Print the sum of the double variables on a new line.
    printf("%.01lf\n",d+f);
    // Concatenate and print the String variables on a new line
    printf("%s%s",s,t);
    
    // The 's' variable above should be printed first.

    return 0;
}

2. Code in C++

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

using namespace std;

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

    // Read and save an integer, double, and String to your variables.
    string temp;
    
    getline(cin,temp);
    j = stoi(temp);
    
    getline(cin, temp);
    f = stod(temp);
    
    getline(cin, t);

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

    // Print the sum of the double variables on a new line.
    // Used setprecision(1) to get at least 1 digit after decimal point
    
    printf("%.1lf\n",d+f);

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

    return 0;
}


3.Code in java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

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 j;
        double f;
        String t;
        /* Read and save an integer, double, and String to your variables.*/
        // Note: If you have trouble reading the entire String, please go back and review the Tutorial closely.
        j = scan.nextInt();
        f = scan.nextDouble();
        
        scan.nextLine();
        t = scan.nextLine();

        /* Print the sum of both integer variables on a new line. */
        System.out.println(i+j);
    
        /* Print the sum of the double variables on a new line. */
        System.out.println(d+f);
        
        /* Concatenate and print the String variables on a new line; 
            the 's' variable above should be printed first. */
        System.out.println(s.concat(t));
        
        scan.close();
    }
}

4.Code in python

i = 4
d = 4.0
s = 'HackerRank '
# Declare second integer, double, and String variables.
j = None
f = None
t = None

# Read and save an integer, double, and String to your variables.
j = int(input())
f = float(input())
t = str(input())

# Print the sum of both integer variables on a new line.
print(i+j)
# Print the sum of the double variables on a new line.
print(d+f)
# Concatenate and print the String variables on a new line
# The 's' variable above should be printed first.
print(s+t)






Post a Comment

0 Comments