Java End-of-file - hacker Rank Solution 2020

Java End-of-file - hacker Rank Solution 2020

 Java End-of-file

The challenge here is to read n lines of input until you reach EOF, then number and print all n lines of content.

 

Hint: Java's Scanner.hasNext() method is helpful for this problem.

 

Input Format

 

Read some unknown  lines of input from stdin(System.in) until you reach EOF; each line of the input contains a non-empty String.

 

Output Format

 

For each line, print the line number, followed by a single space, and then the line content received as input.

 

Sample Input

 

Hello world

I am a file

Read me until end-of-file.

Sample Output

 

1 Hello world

2 I am a file

3 Read me until end-of-file.


Solution:

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) {
        /* Enter your code here. Read input from STDIN.
 Print output to STDOUT. Your class should be named Solution. */
        Scanner sc = new Scanner(System.in);
        for(int i = 1sc.hasNext()== true; i++){
       System.out.println(i + " " + sc.nextLine());
   }
    }
}

Java Datatypes - Hacker Rank Solution

Java Datatypes - Hacker Rank Solution

 

Java Datatypes

Java has 8 primitive data types; char, boolean, byte, short, int, long, float, and double. For this exercise, we'll work with the primitives used to hold integer values (byte, short, int, and long):

 

A byte is an 8-bit signed integer.

A short is a 16-bit signed integer.

An int is a 32-bit signed integer.

A long is a 64-bit signed integer.

Given an input integer, you must determine which primitive data types are capable of properly storing that input.

 

To get you started, a portion of the solution is provided for you in the editor.

 

Input Format

 

The first line contains an integer, T, denoting the number of test cases.

Each test case, T , is comprised of a single line with an integer, Datatype , which can be arbitrarily large or small.

 

Output Format

 

For each input variable and appropriate primitive, you must determine if the given primitives are capable of storing it. If yes, then print:

n can be fitted in:

* dataType


If there is more than one appropriate data type, print each one on its own line and order them by size (i.e.: ).

 byte<short<int<long

If the number cannot be stored in one of the four aforementioned primitives print the line:

 

n can't be fitted anywhere.

Solution:

import java.util.*;
import java.io.*;



class Solution{
    public static void main(String []argh)
    {



        Scanner sc = new Scanner(System.in);
        int t=sc.nextInt();

        for(int i=0;i<t;i++)
        {

            try
            {
                long x=sc.nextLong();
                System.out.println(x+" can be fitted in:");
                if(x>=-128 && x<=127)System.out.println("* byte");
                 if(x >= -Math.pow(215) && x <= Math.pow(215) - 1)
                System.out.println("* short");
            if(x >= -Math.pow(231) && x <= Math.pow(231) - 1)
                System.out.println("* int");
            if(x >= -Math.pow(263) && x <= Math.pow(263) - 1)
                System.out.println("* long");
                //Complete the code
            }
            catch(Exception e)
            {
                System.out.println(sc.next()+" can't be fitted anywhere.");
            }

        }
    }
}




Java loops - II - HackerRank Soluitons

Java loops - II - HackerRank Soluitons

 

We use the integers a,b, and n  to create the following series:

 

You are given q queries in the form of a, b, and n. For each query, print the series corresponding to the given  a, b, and n  values as a single line of space n separated integers.

 

Input Format

 

The first line contains an integer, , denoting the number of queries.

Each line  of the  subsequent lines contains three space-separated integers describing the respective, , and values for that query.


Output Format

 

For each query, print the corresponding series on a new line. Each series must be printed in order as a single line of  space-separated integers


import java.util.*;
import java.io.*;

class Solution{
    public static void main(String []argh){
        Scanner in = new Scanner(System.in);
        int t=in.nextInt();
        for(int i=0;i<t;i++){
            int a = in.nextInt();
            int b = in.nextInt();
            int n = in.nextInt();
            int c = a;
            for(int j=0;j<n;j++){
                c += Math.pow(2, j)*b;
                System.out.printf("%s ",c);
            }
            System.out.println();
        }
        in.close();
    }
}
Java Loops I - HackerRank Solutions 2020

Java Loops I - HackerRank Solutions 2020

In this challenge, we're going to use loops to help us do some simple math.

 

Task

Given an integer, , print its first multiples. Each multiple  (where ) should be printed on a new line in the form: N x i = result.

 

Input Format

 

A single integer,.

 

Constraints

 

Output Format

 

Print  lines of output; each line  (where ) contains the  of  in the form:

N x i = result. 



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

public class Solution {



    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        for (int i = 1; i <= 10; i++) {
            System.out.printf("%d x %d = %d\n", n, i, n*i);
        }
        
        scanner.close();
    }
}

Java Output Formatting- hacker rank solution 2020

Java Output Formatting- hacker rank solution 2020

 Java Output Formatting

To get you started, a portion of the solution is provided for you in the editor; you must format and print the input to complete the solution.

 

Input Format

 

Every line of input will contain a String followed by an integer.

Each String will have a maximum of  alphabetic characters, and each integer will be in the inclusive range from to.

 

Output Format

 

In each line of output there should be two columns:

The first column contains the String and is left-justified using exactly characters.

The second column contains the integer, expressed in exactly  digits; if the original input has less than three digits, you must pad your output's leading digits with zeroes.


import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            System.out.println("================================");
            for(int i=0;i<3;i++){
                String s1=sc.next();
                int x=sc.nextInt();
                System.out.printf("%-15s%03d%n", s1, x);
            }
            System.out.println("================================");

    }
}

 

In java we used ("%-15s%03d%n", s1, x) % as  a  formatter. where '-' : minus sign used for left indentation of the string. 15s : is denotes the string's minimum field width 15. '0' : denotes the pads the extra 0s in the integer. 3d : here 3 denotes integer's minimum field width 3. %n : prints the new line.

 

I Hope, this helps!


Java Stdin and Stdout II - hackerrank solution 2020

Java Stdin and Stdout II - hackerrank solution 2020

 

Java Stdin and Stdout II

In this challenge, you must read an integer, a double, and a String from stdin, then print the values according to the instructions in the Output Format section below. To make the problem a little easier, a portion of the code is provided for you in the editor.

 

Note: We recommend completing Java Stdin and Stdout I before attempting this challenge.

 

Input Format

 

There are three lines of input:

 

The first line contains an integer.

The second line contains a double.

The third line contains a String.


Solution:

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a sc.nextInt();
        double b sc.nextDouble();
        sc.nextLine();
        String c=sc.nextLine();


        System.out.println("String: " + c);
        System.out.println("Double: " + b);
        System.out.println("Int: " + a);


       
    }
}


 

 if-else -HackerRank solution 2020

if-else -HackerRank solution 2020

 if-else -HackerRank solution

In this challenge, we test your knowledge of using if-else conditional statements to automate decision-making processes. An if-else statement has the following logical flow:



import java.io.*;

import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {



    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int N = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
         String ans="";
            if(N%2==1 || (N>=6 && N <= 20)){
  ans = "Weird";
}else{
  ans = "Not Weird";
}
            System.out.println(ans);
            

        scanner.close();
    }
}