Practice is one of the best ways to learn Java programming. In this lesson, you will solve coding exercises covering variables, operators, conditions, loops, arrays, strings, and methods.
Question: Write a Java program to print Hello World.
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Output:
Hello World
Question: Add two numbers and display the result.
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 20;
int sum = a + b;
System.out.println("Sum = " + sum);
}
}
Output:
Sum = 30
Question: Find the square of a number.
public class Main {
public static void main(String[] args) {
int number = 5;
int square = number * number;
System.out.println("Square = " + square);
}
}
Output:
Square = 25
Question: Check whether a number is even or odd.
public class Main {
public static void main(String[] args) {
int number = 7;
if (number % 2 == 0) {
System.out.println("Even Number");
} else {
System.out.println("Odd Number");
}
}
}
Output:
Odd Number
The modulus operator % returns the remainder.
If the remainder after division by 2 is zero, the number is even.
Question: Find the larger of two numbers.
public class Main {
public static void main(String[] args) {
int a = 25;
int b = 40;
if (a > b) {
System.out.println(a + " is largest");
} else if (b > a) {
System.out.println(b + " is largest");
} else {
System.out.println("Both numbers are equal");
}
}
}
Output:
40 is largest
Question: Identify whether a number is positive, negative, or zero.
public class Main {
public static void main(String[] args) {
int number = -5;
if (number > 0) {
System.out.println("Positive Number");
} else if (number < 0) {
System.out.println("Negative Number");
} else {
System.out.println("Zero");
}
}
}
Output:
Negative Number
Question: Use a for loop to print numbers from 1 to 10.
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
}
}
Output:
1
2
3
4
5
6
7
8
9
10
Question: Print the multiplication table of 5.
public class Main {
public static void main(String[] args) {
int number = 5;
for (int i = 1; i <= 10; i++) {
System.out.println(number + " x " + i
+ " = " + (number * i));
}
}
}
Output:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Question: Calculate the factorial of 5.
public class Main {
public static void main(String[] args) {
int number = 5;
long factorial = 1;
for (int i = 1; i <= number; i++) {
factorial = factorial * i;
}
System.out.println("Factorial = " + factorial);
}
}
Output:
Factorial = 120
Factorial of 5 is \(5 \times 4 \times 3 \times 2 \times 1 = 120\). The factorial of zero is also 1.
Question: Calculate the sum of numbers from 1 to 10.
public class Main {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
System.out.println("Sum = " + sum);
}
}
Output:
Sum = 55
Question: Reverse the digits of a positive integer.
public class Main {
public static void main(String[] args) {
int number = 1234;
int reverse = 0;
while (number > 0) {
int digit = number % 10;
reverse = reverse * 10 + digit;
number = number / 10;
}
System.out.println("Reverse = " + reverse);
}
}
Output:
Reverse = 4321
The modulus operator extracts the last digit, and integer division removes that digit from the original number.
Question: Check whether a positive integer reads the same forwards and backwards.
public class Main {
public static void main(String[] args) {
int number = 121;
int original = number;
int reverse = 0;
while (number > 0) {
int digit = number % 10;
reverse = reverse * 10 + digit;
number = number / 10;
}
if (original == reverse) {
System.out.println("Palindrome Number");
} else {
System.out.println("Not a Palindrome");
}
}
}
Output:
Palindrome Number
Question: Find the largest value in an integer array.
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 25, 7, 40, 15};
int largest = numbers[0];
for (int number : numbers) {
if (number > largest) {
largest = number;
}
}
System.out.println("Largest = " + largest);
}
}
Output:
Largest = 40
Question: Count the vowels in a string.
public class Main {
public static void main(String[] args) {
String text = "Hello Java";
int count = 0;
for (int i = 0; i < text.length(); i++) {
char ch = Character.toLowerCase(text.charAt(i));
if (ch == 'a' || ch == 'e' || ch == 'i'
|| ch == 'o' || ch == 'u') {
count++;
}
}
System.out.println("Vowels = " + count);
}
}
Output:
Vowels = 4
Question: Create a method that accepts two integers and returns their sum.
public class Main {
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = add(15, 25);
System.out.println("Sum = " + result);
}
}
Output:
Sum = 40
The add() method accepts two parameters and
returns the calculated sum.
Question: Check whether a number is prime.
public class Main {
public static void main(String[] args) {
int number = 17;
boolean isPrime = number >= 2;
for (int i = 2; i <= number / i; i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.println("Prime Number");
} else {
System.out.println("Not a Prime Number");
}
}
}
Output:
Prime Number
A prime number is an integer greater than 1 that has exactly two positive divisors: 1 and itself.
Question: Swap the values of two variables using a temporary variable.
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("Before: a = " + a + ", b = " + b);
int temp = a;
a = b;
b = temp;
System.out.println("After: a = " + a + ", b = " + b);
}
}
Output:
Before: a = 10, b = 20
After: a = 20, b = 10
Question: Count the digits in a non-negative integer.
public class Main {
public static void main(String[] args) {
int number = 12345;
int count = 0;
do {
count++;
number = number / 10;
} while (number != 0);
System.out.println("Digits = " + count);
}
}
Output:
Digits = 5
The do-while loop executes at least once, so
it also counts zero as a one-digit number.
Try to solve these questions without looking at the solutions above.
Choose the correct answer for each question.