Exercise Overview
How to Use These Exercises
These exercises are designed to reinforce your understanding of Java control structures including decision-making statements, loops, and program flow control. Start with the easy exercises and progress to more challenging ones as you build confidence.
Tips for Success:
- Understand the difference between if-else and switch statements
- Practice different types of loops (for, while, do-while)
- Learn to combine control structures effectively
- Follow proper indentation and code structure
- Test your programs with various input scenarios
Easy Exercises - Start Here!
If-Else Statements
EasyPractice using if-else statements for decision making.
Requirements:
- Create a program that determines if a number is positive, negative, or zero
- Use if-else if-else structure
- Handle edge cases appropriately
- Test with multiple input values
Expected Output:
Enter a number: -5
Result: Negative
Enter a number: 10
Result: Positive
Enter a number: 0
Result: Zero
Hints:
- Use comparison operators:
>
,<
,==
- Start with the most specific condition first
- Use
else if
for multiple conditions - Use
else
for the default case
Switch Statements
EasyPractice using switch statements for multiple choice scenarios.
Requirements:
- Create a program that converts numbers to day names (1=Monday, 2=Tuesday, etc.)
- Use switch statement with default case
- Handle invalid inputs gracefully
- Support numbers 1-7 for days of the week
Expected Output:
Enter day number (1-7): 3
Day: Wednesday
Enter day number (1-7): 8
Day: Invalid day number
Hints:
- Use
switch
statement withcase
labels - Don't forget
break
statements after each case - Use
default
case for invalid inputs - Switch works with int, char, String, and enum types
For Loops
EasyPractice using for loops for iteration.
Requirements:
- Create a program that prints the first 10 Fibonacci numbers
- Use for loop with proper initialization and increment
- Format output clearly
- Display the sequence in a readable format
Expected Output:
Fibonacci Series (first 10 numbers):
0 1 1 2 3 5 8 13 21 34
Hints:
- Use
for (int i = 0; i < n; i++)
structure - Initialize first two numbers:
a = 0, b = 1
- Calculate next number:
temp = a + b
- Update variables:
a = b, b = temp
- Use
System.out.print()
for same-line output
Medium Exercises - Build Your Skills!
While Loops
MediumPractice using while loops for conditional iteration.
Requirements:
- Create a program that finds the sum of digits in a number
- Use while loop with proper condition
- Handle edge cases (single digit, zero)
- Display the original number and the sum
Expected Output:
Enter a number: 12345
Number: 12345
Sum of digits: 15
Enter a number: 7
Number: 7
Sum of digits: 7
Hints:
- Use
while (number > 0)
as the condition - Extract last digit:
number % 10
- Remove last digit:
number /= 10
- Add extracted digit to sum variable
- Initialize sum to 0 before the loop
Do-While Loops
MediumPractice using do-while loops for guaranteed execution.
Requirements:
- Create a menu-driven program with options
- Use do-while loop to repeat the menu
- Handle user input validation
- Provide an exit option
Expected Output:
Menu Options:
1. Calculate square
2. Calculate cube
3. Exit
Enter your choice: 1
Enter a number: 5
Square of 5 is: 25
Menu Options:
1. Calculate square
2. Calculate cube
3. Exit
Enter your choice: 3
Goodbye!
Hints:
- Use
do { ... } while (condition);
structure - Do-while executes at least once before checking condition
- Use switch statement inside the loop for menu options
- Set a flag variable to control loop exit
- Validate user input before processing
Break and Continue
MediumPractice using break and continue statements in loops.
Requirements:
- Create a program that finds the first number divisible by both 3 and 7
- Use break to exit loop when condition is met
- Use continue to skip numbers not divisible by 3
- Display the search process and result
Expected Output:
Searching for first number divisible by both 3 and 7...
Checking 1... (skipped)
Checking 2... (skipped)
Checking 3... (divisible by 3, checking 7...)
Checking 6... (divisible by 3, checking 7...)
Checking 9... (divisible by 3, checking 7...)
Checking 12... (divisible by 3, checking 7...)
Checking 15... (divisible by 3, checking 7...)
Checking 18... (divisible by 3, checking 7...)
Checking 21... (divisible by 3, checking 7...)
Found! 21 is divisible by both 3 and 7
Hints:
- Use
continue
to skip to next iteration - Use
break
to exit the loop completely - Check divisibility by 3 first, then by 7
- Use modulo operator
%
for divisibility check - Display progress messages for better understanding
Hard Exercises - Master the Concepts!
Nested Control Structures
HardPractice combining multiple control structures.
Requirements:
- Create a program that finds prime numbers in a range
- Use nested loops and if statements
- Optimize the algorithm for efficiency
- Display prime numbers in a formatted way
- Count and display the total number of primes found
Expected Output:
Prime numbers from 1 to 50:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Total prime numbers found: 15
Hints:
- Use outer loop to check each number in range
- Use inner loop to check divisibility from 2 to sqrt(number)
- Use
Math.sqrt()
for optimization - Use boolean flag to track if number is prime
- Use
break
to exit inner loop when number is not prime
Pattern Printing
HardCreate complex patterns using nested loops and control structures.
Requirements:
- Create a program that prints a pyramid pattern
- Use nested for loops for rows and columns
- Implement different pattern variations
- Make the pattern size configurable
- Add proper spacing and alignment
Expected Output:
Enter pyramid height: 5
*
***
*****
*******
*********
Hints:
- Use outer loop for rows (1 to height)
- Use inner loop for spaces (decreasing)
- Use inner loop for stars (increasing)
- Calculate spaces:
height - row
- Calculate stars:
2 * row - 1
- Use
System.out.print()
for same-line output