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
3 Easy Exercises
3 Medium Exercises
1 Hard Exercises

Easy Exercises - Start Here!

3.1

If-Else Statements

Easy

Practice 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

Submit Your Work

  • Add your Java Codes and a screenshot of the output in the zip file.
  • Add your Full Name, Program, Year, and Section in your code.
Name your file: lastname_first_name_course_year_section_exercise_1.zip. Max 5MB.
3.2

Switch Statements

Easy

Practice 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 with case labels
  • Don't forget break statements after each case
  • Use default case for invalid inputs
  • Switch works with int, char, String, and enum types

Submit Your Work

  • Add your Java Codes and a screenshot of the output in the zip file.
  • Add your Full Name, Program, Year, and Section in your code.
Name your file: lastname_first_name_course_year_section_exercise_2.zip. Max 5MB.
3.3

For Loops

Easy

Practice 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

Submit Your Work

  • Add your Java Codes and a screenshot of the output in the zip file.
  • Add your Full Name, Program, Year, and Section in your code.
Name your file: lastname_first_name_course_year_section_exercise_3.zip. Max 5MB.

Medium Exercises - Build Your Skills!

3.4

While Loops

Medium

Practice 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

Submit Your Work

  • Add your Java Codes and a screenshot of the output in the zip file.
  • Add your Full Name, Program, Year, and Section in your code.
Name your file: lastname_first_name_course_year_section_exercise_4.zip. Max 5MB.
3.5

Do-While Loops

Medium

Practice 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

Submit Your Work

  • Add your Java Codes and a screenshot of the output in the zip file.
  • Add your Full Name, Program, Year, and Section in your code.
Name your file: lastname_first_name_course_year_section_exercise_5.zip. Max 5MB.
3.6

Break and Continue

Medium

Practice 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

Submit Your Work

  • Add your Java Codes and a screenshot of the output in the zip file.
  • Add your Full Name, Program, Year, and Section in your code.
Name your file: lastname_first_name_course_year_section_exercise_6.zip. Max 5MB.

Hard Exercises - Master the Concepts!

3.7

Random Number Guessing Game

Hard

Build an interactive guessing game that uses decision-making and loops to let the user guess a randomly generated number.

Requirements:

  • Generate a random secret number between 1 and 100.
  • Prompt the user to enter a guess repeatedly.
  • Print hints: "Too low" or "Too high" until the guess is correct.
  • Count and display the number of attempts when the user guesses correctly.
  • (Optional) Limit to a maximum number of attempts (e.g., 10) and end the game if exceeded.
  • Validate input and handle non-numeric or out-of-range values gracefully.

Sample Output:

I'm thinking of a number between 1 and 100.
Enter your guess: 50
Too high! Try again.
Enter your guess: 25
Too low! Try again.
Enter your guess: 32
🎉 Correct! You guessed it in 3 attempts.

Hints:

  • Use java.util.Random or (int)(Math.random()*100) + 1 to generate numbers.
  • Read input with Scanner and loop using while or do-while.
  • Compare guesses with if-else; use break when correct.
  • Track attempts with a counter variable.
  • Validate input: check ranges (1–100) and handle non-integers.

🎉 Module 3 Exercises Complete!

You've practiced Java control structures including decision-making, loops, and program flow control. Ready to move on to methods and functions!