Embarking on your Java learning journey? Welcome! Practicing Java with exercises is an excellent way to grasp concepts and build a solid foundation. In this guide, we've curated a list of beginner-friendly Java exercises, complete with solutions to help you learn and grow. Let's dive right in!

Whether you're new to programming or transitioning from another language, consistent practice is key to mastering Java. This article caters to both beginners and those refreshing their memory with fundamental exercises and solutions.

Java Basics: Data Types and Operators
The bedrock of Java programming lies in understanding data types and operators. Let's explore some exercises that delve into these basics.

Start by brushing up on your knowledge of Java's primitive data types: int, float, double, boolean, char, and String. Remember, only int, double, float, and boolean are used for numerical calculations; char and String, for text manipulation.
Exercise 1: Temperature Converter

Create a simple program that converts Fahrenheit to Celsius. The formula is: C = (F - 32) * 5/9.
Here's a simple solution using double for better precision:
import java.util.Scanner;
public class TempConverter {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter temperature in Fahrenheit: ");
double fahrenheit = input.nextDouble();
double celsius = (fahrenheit - 32) * 5/9;
System.out.println(fahrenheit + " degrees Fahrenheit is equivalent to " + celsius + " degrees Celsius.");
}
}
Exercise 2: Arithmetic Operations

Write a program that takes two integers as input and performs basic arithmetic operations (addition, subtraction, multiplication, and division).
Here's a straightforward solution using int for the inputs and double for the results (to account for potential decimal outputs):
import java.util.Scanner;
public class Arithmetic {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = input.nextInt();
System.out.print("Enter second number: ");
int num2 = input.nextInt();
System.out.println("num1 + num2 = " + (num1 + num2));
System.out.println("num1 - num2 = " + (num1 - num2));
System.out.println("num1 * num2 = " + (num1 * num2));
System.out.println("num1 / num2 = " + (double)num1 / num2);
}
}
Control Structures: Looping and Branching

Now that you've conquered the basics, it's time to explore Java's control structures. Let's practice with some looping and branching exercises.
Familiarize yourself with Java's loop types: for, while, and do-while, and understand when to use each. Additionally, grasp the power of branching with if, else if, and switch statements.



















Exercise 3: Guess the Number
Create a simple guessing game. The computer picks a random number, and the user has to guess it. Provide hints like ' Higher' or 'Lower' until the user guesses correctly.
Here's a solution using Scanner, Random, and a while loop:
import java.util.Random;
import java.util.Scanner;
public class GuessTheNumber {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random rand = new Random();
int secretNumber = rand.nextInt(100) + 1;
int guess;
do {
System.out.print("Guess a number between 1 and 100: ");
guess = input.nextInt();
if (guess < secretNumber)
System.out.println("Too low! Try again.");
else if (guess > secretNumber)
System.out.println("Too high! Try again.");
} while (guess != secretNumber);
System.out.println("Congratulations! You guessed the number!");
}
}
Exercise 4: Calculate Factorial
Write a program that calculates the factorial of a given number using a for loop.
Here's a simple solution using a for loop and an int to store the result:
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a non-negative integer: ");
int number = input.nextInt();
int factorial = 1;
for (int i = 1; i <= number; i++) {
factorial *= i;
}
System.out.println("The factorial of " + number + " is " + factorial);
}
}
Keep practicing and exploringJava! As you tackle more exercises, you'll build a strong foundation in Java programming. Happy coding!