Lab Exercises for Unit 2
Decision Making, Loops, Arrays, and Strings
Exercise 1: Decision Making with If-Else and Switch
• Write a Java program to input a student's marks and print the grade according to a
defined scale (e.g., A for >90, B for 80-90, etc.).
• Implement a program to determine the day of the week based on user input (1 for
Monday, 2 for Tuesday, etc.) using both if-else and switch-case constructs.
Exercise 2: Loop Control Statements
• Write a program to print the first N Fibonacci numbers using a for loop.
• Write a Java program to display all prime numbers between two given numbers using
nested loops.
• Implement a program to calculate the factorial of a number using while and do-while
loops.
• Write a program that prints a pattern of stars, such as a pyramid or diamond, using
nested loops.
• Create programs to demonstrate the use of break, continue, and labeled
break/continue statements within loops.
Exercise 3: Array Handling
• Write a program to input N integers into an array and find the largest and smallest
elements.
• Write a Java program to reverse an integer array and print the reversed array.
• Implement a program to find the sum and average of elements in a double-type array.
• Create a program to merge two arrays and display the resultant array.
• Write a program to count the frequency of each element in an array.
Exercise 4: String Manipulation
• Write a program to input a string and check whether it is a palindrome.
• Implement programs to demonstrate usage of various String methods such as length(),
charAt(), substring(), indexOf(), replace(), and toUpperCase()/toLowerCase().
• Create a program to compare two strings ignoring case differences.
• Write a program to count the number of vowels and consonants in a given string.
• Demonstrate the difference between String, StringBuffer, and StringBuilder in
concatenation and reversal operations.
Exercise 5: Two-Dimensional Arrays
• Write a program to input elements into a 2D array (matrix) and display it.
• Implement matrix addition and multiplication programs.
• Write a program to find the transpose of a matrix.
• Display the diagonal elements and sum them in a square matrix.