Here’s a quick guide and solutions for typical Java Introduction problems found on platforms like HackerRank:
1. Welcome to Java!
This is one of the simplest tasks to print a message in Java.
Problem:
Write a program that prints:
Hello, World.
Hello, Java.
Solution:
public class Solution {
public static void main(String[] args) {
System.out.println("Hello, World.");
System.out.println("Hello, Java.");
}
}
2. Java Stdin and Stdout I
In this task, you need to read 3 integers from the input and print them.
Problem:
Input consists of 3 integers. Print each integer on a new line.
Solution:
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Reading integers from standard input
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
// Output each integer on a new line
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
3. Java If-Else
Based on a given integer, perform conditional checks and print messages accordingly.
Problem:
Write a program that:
- If
n
is odd, print "Weird". - If
n
is even and in the inclusive range of 2 to 5, print "Not Weird". - If
n
is even and in the inclusive range of 6 to 20, print "Weird". - If
n
is even and greater than 20, print "Not Weird".
Solution:
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
if (n % 2 != 0) {
System.out.println("Weird");
} else {
if (n >= 2 && n <= 5) {
System.out.println("Not Weird");
} else if (n >= 6 && n <= 20) {
System.out.println("Weird");
} else {
System.out.println("Not Weird");
}
}
}
}
4. Java Stdin and Stdout II
This task involves reading various types of input from the user and printing them in a specific format.
Problem:
Read a string, an integer, and a double, then print them.
Solution:
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read a double
double d = scanner.nextDouble();
// Read an integer
int i = scanner.nextInt();
// Read the remaining newline and the string
scanner.nextLine(); // Consume the newline
String s = scanner.nextLine();
// Print results in reverse order
System.out.println("String: " + s);
System.out.println("Int: " + i);
System.out.println("Double: " + d);
}
}
5. Java Output Formatting
The task here is to format strings and numbers to fit specific alignment requirements.
Problem:
- Input consists of a string and an integer in each of 3 lines.
- Print the string left-aligned and the integer right-aligned to fit within 15 and 3 spaces, respectively.
Solution:
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("================================");
// Loop to read input and format output
for (int i = 0; i < 3; i++) {
String s = scanner.next();
int x = scanner.nextInt();
// Print the string and the integer with proper formatting
System.out.printf("%-15s%03d%n", s, x);
}
System.out.println("================================");
}
}
6. Java Loops I
This problem focuses on basic loops to print multiplication tables.
Problem:
Given an integer n
, print its first 10 multiples in this format:
n x i = result
Solution:
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
// Print first 10 multiples of n
for (int i = 1; i <= 10; i++) {
System.out.println(n + " x " + i + " = " + (n * i));
}
}
}
7. Java Loops II
In this problem, you need to perform some computations and print results based on a mathematical series.
Problem:
Given a
, b
, and n
, print the series result:
a + 2^0 * b
, a + (2^0 * b) + (2^1 * b)
, ..., for n
terms.
Solution:
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt(); // Number of test cases
for (int i = 0; i < t; i++) {
int a = scanner.nextInt();
int b = scanner.nextInt();
int n = scanner.nextInt();
int sum = a;
for (int j = 0; j < n; j++) {
sum += Math.pow(2, j) * b;
System.out.print(sum + " ");
}
System.out.println();
}
}
}
These are some basic Java Introduction problems commonly found on HackerRank. They cover a wide range of topics including input/output, conditionals, loops, and formatting. Solving them will help you brush up on core Java concepts!
Top comments (0)