HackerRank Introduction Challenges - Java
HackerRank Introduction Challenges - Java
The code stub in the editor already creates the main function and solution class. All you have to do is copy and paste the following lines inside the main function.
Sample Output
Download Code Here
Java Output Formatting
Java Loops
a+20b,a+20b+21b,......,a+20b+21b+...+2n−1b
0≤a,b≤50
1≤n≤15
Welcome to Java!
Problem Statement
Welcome to the world of Java! Just print "Hello World." and "Hello Java." in two separate lines to complete this challenge.
The code stub in the editor already creates the main function and solution class. All you have to do is copy and paste the following lines inside the main function.
System.out.println("Hello World.");
System.out.println("Hello Java.");
Sample Output
Hello World.
Hello Java.
Download Code Here
Java Stdin and Stdout 1
Problem Statement
In most of the Hackerrank challenges, you need to read input from stdin (standard input) and write your output in stdout (standard output).
One way to take input from stdin is to use Scanner class and reading from System.in.
You can write your output to stdout simply using System.out.printf function.
In this problem you need to read 3 integers from stdin and print them in stdout.
Sample input:
42
100
125
Sample output:
42
100
125
To make the problem easier for you, some part of the code is already provided in the editor.
Java Stdin and Stdout 2
Problem Statement
In most of the problems in Hackerrank, you need to read input from stdin or standard input and write your output in stdout or standard output.
One way to take input from stdin is to use Scanner class and reading from System.in. Alternatively, you can use BufferedReader class.
You can write your output to stdout simply using System.out.printf function.
In this problem you just need to read the inputs from stdin and print them in stdout.
Input Format:
There will be three lines of input. The first line will have an integer. The second line will have adouble number. The last line will consist of a string.
There will be three lines of input. The first line will have an integer. The second line will have adouble number. The last line will consist of a string.
Output Format:
Print the string in the first line, the double in the second line and the integer in the third line. See the sample output for exact formatting.
Print the string in the first line, the double in the second line and the integer in the third line. See the sample output for exact formatting.
To make the problem easier, some part of the code is already provided in the editor. Note that Scanner's nextInt() method doesn't read the last newline character of the input, you need to keep this in mind if you use nextLine() after using nextInt().
Sample Input
42
3.1415
Welcome to Hackerrank Java tutorials!
Sample Output
String: Welcome to Hackerrank Java tutorials!
Double: 3.1415
Int: 42
Note: Don't worry about the precision of the double, you can simply print it using System.out.println. There might be leading or trailing spaces in the string, keep it exactly as it is.
Java Output Formatting
Problem Statement
Java System.out.printf function allowes you to print formatted output. This problem will test your knowledge on this topic.
Take exactly 3 lines of input. Each line consists of a string and an integer. Suppose this is the sample input:
java 100
cpp 65
python 50
The strings will have at most 10 alphabetic characters and the integers will range between 0 to 999.
In each line of output there should be two columns. The string should be in the first column and the integer in the second column. This is the output for the input above:
================================
java 100
cpp 065
python 050
================================
The first column should be left justified using exactly 15 characters. The integer of the second column should have exactly 3 digits. If the original input has less than 3 digits, you should pad with zeros to the left.
To make the problem easier, some part of the solution is already given in the editor, just complete the remaining parts.
Java Loops
Problem Statement
In this problem you will test your knowledge of Java loops. Given three integers a , b , and n , output the following series:
Input Format
The first line will contain the number of testcases t. Each of the next t lines will have three integers, a , b , and n .
Constraints:
Output Format
Print the answer to each test case in separate lines.
Sample Input
2
0 2 10
5 3 5
Sample Output
2 6 14 30 62 126 254 510 1022 2046
8 14 26 50 98
Explanation
In the first case:
1st term=0+1*2=2
2nd term=0+1*2+2*2=6
3rd term=0+1*2+2*2+4*2=14
and so on.
Java Datatypes
Problem Statement
Java has 8 Primitive Data Types; they are char, boolean, byte, short, int, long, float, and double. In this problem we are only concerned about integer datatypes used to hold integer values (byte, short, int, long). Let's take a closer look at them:
- byte data type is an 8-bit signed integer.
- short data type is an 16-bit signed integer.
- int data type is an 32-bit signed integer.
- long data type is an 64-bit signed integer.
Given an integer number, you have to determine which of these datatypes you can use to store that number. If there are multiple suitable datatypes, list them all in the order above.
To make the problem easier, some part of the code is already provided in the editor.
Input Format
The first line will contain an integer T , which denotes the number of inputs that will follow. Each of the next T lines will contain an integer n . The number can be arbitrarily large or small!
Output Format
For each n , list all the datatypes it can be fitted into ordered by the size of the datatype. If it can't be fitted into any of these datatypes, print "n can't be fitted anywhere." See the sample output for the exact formatting.
Sample Input
5
-150
150000
1500000000
213333333333333333333333333333333333
-100000000000000
Sample Output
-150 can be fitted in:
* short
* int
* long
150000 can be fitted in:
* int
* long
1500000000 can be fitted in:
* int
* long
213333333333333333333333333333333333 can't be fitted anywhere.
-100000000000000 can be fitted in:
* long
Explanation
-150 can be fitted in a short or in an int or in a long. 213333333333333333333333333333333333 is way too large to fit in any datatypes mentioned in the problem statement.
Comments
Post a Comment