INDEX
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.
Exercise 1: Decision Making with If-Else and Switch
1. 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.).
Program
import [Link];
public class L01 {//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the student's marks (0-100): ");
int marks = [Link]();
if (marks > 100 || marks < 0) {
[Link]("Invalid marks! Please enter between 0 and 100.");
} else if (marks > 90) {
[Link]("Grade: A");
} else if (marks >= 80) {
[Link]("Grade: B");
} else if (marks >= 70) {
[Link]("Grade: C");
} else if (marks >= 60) {
[Link]("Grade: D");
} else if (marks >= 50) {
[Link]("Grade: E");
} else {
[Link]("Grade: F (Fail)");
}
}
}
Output
2. 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.
Program
import [Link];
public class L02 {//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number (1-7) for the day of the week: ");
int day = [Link]();
[Link]("\n--- Using if-else ---");
if (day == 1) {
[Link]("Monday");
} else if (day == 2) {
[Link]("Tuesday");
} else if (day == 3) {
[Link]("Wednesday");
} else if (day == 4) {
[Link]("Thursday");
} else if (day == 5) {
[Link]("Friday");
} else if (day == 6) {
[Link]("Saturday");
} else if (day == 7) {
[Link]("Sunday");
} else {
[Link]("Invalid input! Please enter 1 to 7.");
}
[Link]("\n--- Using switch-case ---");
switch (day) {
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
case 3:
[Link]("Wednesday");
break;
case 4:
[Link]("Thursday");
break;
case 5:
[Link]("Friday");
break;
case 6:
[Link]("Saturday");
break;
case 7:
[Link]("Sunday");
break;
default:
[Link]("Invalid input! Please enter 1 to 7.");
}
}
}
Output
Exercise 2: Loop Control Statements
1. Write a program to print the first N Fibonacci numbers using a for loop.
Program
import [Link];
public class L03 { //BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter value of N: ");
int n = [Link]();
int first = 0, second = 1;
[Link]("Fibonacci Series: ");
for (int i = 1; i <= n; i++) {
[Link](first + " ");
int next = first + second;
first = second;
second = next;
}
[Link]();
}
}
Output
2. Write a Java program to display all prime numbers between two given numbers
using nested loops.
Program
import [Link];
public class L04 { //BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter start number: ");
int start = [Link]();
[Link]("Enter end number: ");
int end = [Link]();
[Link]("Prime numbers between " + start + " and " + end + ":");
for (int i = start; i <= end; i++) {
boolean isPrime = true;
if (i <= 1) continue;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
[Link](i + " ");
}
}
}
}
Output
3. Implement a program to calculate the factorial of a number using while and do-
while loops.
Program
import [Link];
public class L05 { //BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int fact1 = 1, i = num;
while (i > 0) {
fact1 *= i;
i--;
}
[Link]("Factorial using while loop: " + fact1);
int fact2 = 1, j = num;
do {
fact2 *= j;
j--;
} while (j > 0);
[Link]("Factorial using do-while loop: " + fact2);
}
}
Output
4. Write a program that prints a pattern of stars, such as a pyramid or diamond,
using nested loops.
Program
import [Link];
public class L06 { //BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows: ");
int rows = [Link]();
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
[Link](" "); // spaces
}
for (int k = 1; k <= (2 * i - 1); k++) {
[Link]("*"); // stars
}
[Link]();
}
}
}
Output
5. Create programs to demonstrate the use of break, continue, and labeled
break/continue statements within loops.
Program
public class L07 { //BETN1CS24109
public static void main(String[] args) {
[Link]("Demonstrating break:");
for (int i = 1; i <= 10; i++) {
if (i == 5) break; // exit loop
[Link](i + " ");
}
[Link]("\n\nDemonstrating continue:");
for (int i = 1; i <= 10; i++) {
if (i == 5) continue; // skip iteration
[Link](i + " ");
}
[Link]("\n\nDemonstrating labeled break:");
outer: for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (i == 2 && j == 2) break outer;
[Link](i + " " + j);
}
}
[Link]("\nDemonstrating labeled continue:");
outer: for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) continue outer;
[Link](i + " " + j);
}
}
}
}
Output
Exercise 3: Array Handling
1. Write a program to input N integers into an array and find the largest and smallest
elements
Program
import [Link];
public class L08 {//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of elements: ");
int n = [Link]();
int[] arr = new int[n];
[Link]("Enter " + n + " integers:");
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}
int largest = arr[0], smallest = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > largest) largest = arr[i];
if (arr[i] < smallest) smallest = arr[i];
}
[Link]("Largest element: " + largest);
[Link]("Smallest element: " + smallest);
}
}
Output
2. Write a Java program to reverse an integer array and print the reversed array.
Program
import [Link];
public class L09 {//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of elements: ");
int n = [Link]();
int[] arr = new int[n];
[Link]("Enter " + n + " integers:");
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}
[Link]("Reversed array:");
for (int i = n - 1; i >= 0; i--) {
[Link](arr[i] + " ");
}
}
}
Output
3. Implement a program to find the sum and average of elements in a double-type
array.
Program
import [Link];
public class L10 {//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of elements: ");
int n = [Link]();
double[] arr = new double[n];
[Link]("Enter " + n + " double values:");
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}
double sum = 0;
for (double val : arr) {
sum += val;
}
double avg = sum / n;
[Link]("Sum of elements: " + sum);
[Link]("Average of elements: " + avg);
}
}
Output
4. Create a program to merge two arrays and display the resultant array.
Program
import [Link];
public class L11{//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter size of first array: ");
int n1 = [Link]();
int[] arr1 = new int[n1];
[Link]("Enter elements of first array:");
for (int i = 0; i < n1; i++) {
arr1[i] = [Link]();
}
[Link]("Enter size of second array: ");
int n2 = [Link]();
int[] arr2 = new int[n2];
[Link]("Enter elements of second array:");
for (int i = 0; i < n2; i++) {
arr2[i] = [Link]();
}
int[] merged = new int[n1 + n2];
for (int i = 0; i < n1; i++) merged[i] = arr1[i];
for (int i = 0; i < n2; i++) merged[n1 + i] = arr2[i];
[Link]("Merged array:");
for (int val : merged) {
[Link](val + " ");
}
}
}
Output
5. Write a program to count the frequency of each element in an array.
Program
import [Link];
public class L12{//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of elements: ");
int n = [Link]();
int[] arr = new int[n];
[Link]("Enter " + n + " integers:");
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}
boolean[] visited = new boolean[n];
[Link]("Element frequencies:");
for (int i = 0; i < n; i++) {
if (visited[i]) continue;
int count = 1;
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
count++;
visited[j] = true;
}
}
[Link](arr[i] + " occurs " + count + " times");
}
}
}
Output
Exercise 4: String Manipulation
1. Write a program to input a string and check whether it is a palindrome.
Program
import [Link];
public class L13 {//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String rev = "";
for (int i = [Link]() - 1; i >= 0; i--) {
rev += [Link](i);
}
if ([Link](rev)) {
[Link]("The string is a palindrome.");
} else {
[Link]("The string is NOT a palindrome.");
}
}
}
Output
2. Implement programs to demonstrate usage of various String methods such as
length(), charAt(), substring(), indexOf(), replace(), and
toUpperCase()/toLowerCase().
Program
public class L14 {//BETN1CS24109
public static void main(String[] args) {
String text = "Hello World";
[Link]("Original String: " + text);
[Link]("Length: " + [Link]());
[Link]("Character at index 4: " + [Link](4));
[Link]("Substring (0-5): " + [Link](0, 5));
[Link]("Index of 'World': " + [Link]("World"));
[Link]("Replace 'World' with 'Java': " + [Link]("World",
"Java"));
[Link]("Uppercase: " + [Link]());
[Link]("Lowercase: " + [Link]());
}
}
Output
3. Write a program to count the number of vowels and consonants in a given string.
Program
import [Link];
public class L15 {//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]().toLowerCase();
int vowels = 0, consonants = 0;
for (char ch : [Link]()) {
if ([Link](ch)) {
if ("aeiou".indexOf(ch) != -1) {
vowels++;
} else {
consonants++;
}
}
}
[Link]("Vowels: " + vowels);
[Link]("Consonants: " + consonants);
}
}
Output
4. Create a program to compare two strings ignoring case differences.
Program
import [Link];
public class L16{//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first string: ");
String str1 = [Link]();
[Link]("Enter second string: ");
String str2 = [Link]();
if ([Link](str2)) {
[Link]("Both strings are equal (ignoring case).");
} else {
[Link]("Strings are NOT equal.");
}
}
}
Output
5. Demonstrate the difference between String, StringBuffer, and StringBuilder in
concatenation and reversal operations.
Program
public class L17{//BETN1CS24109
public static void main(String[] args) {
String str = "Java";
String concatStr = str + " Programming";
[Link]("String concatenation: " + concatStr);
StringBuffer sb = new StringBuffer("Java");
[Link](" Programming");
[Link]("StringBuffer concatenation: " + [Link]());
[Link]();
[Link]("StringBuffer reversed: " + sb);
StringBuilder sb2 = new StringBuilder("Java");
[Link](" Programming");
[Link]("StringBuilder concatenation: " + [Link]());
[Link]();
[Link]("StringBuilder reversed: " + sb2);
}
}
Output
Exercise 5: Two-Dimensional Arrays
1. Write a program to input elements into a 2D array (matrix) and display it.
Program
import [Link];
public class L18{//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter rows: ");
int rows = [Link]();
[Link]("Enter columns: ");
int cols = [Link]();
int[][] matrix = new int[rows][cols];
[Link]("Enter matrix elements:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = [Link]();
}
}
[Link]("Matrix is:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
[Link](matrix[i][j] + " ");
}
[Link]();
}
}
}
Output
2. Implement matrix addition and multiplication programs.
Program
import [Link];
public class L19 {//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter rows: ");
int rows = [Link]();
[Link]("Enter columns: ");
int cols = [Link]();
int[][] a = new int[rows][cols];
int[][] b = new int[rows][cols];
int[][] sum = new int[rows][cols];
[Link]("Enter elements of first matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
a[i][j] = [Link]();
}
}
[Link]("Enter elements of second matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
b[i][j] = [Link]();
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}
[Link]("Resultant Matrix (Addition):");
for (int[] row : sum) {
for (int val : row) {
[Link](val + " ");
}
[Link]();
}
}
}
Output
Program
import [Link];
public class L20 {//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter rows of first matrix: ");
int r1 = [Link]();
[Link]("Enter cols of first matrix: ");
int c1 = [Link]();
[Link]("Enter rows of second matrix: ");
int r2 = [Link]();
[Link]("Enter cols of second matrix: ");
int c2 = [Link]();
if (c1 != r2) {
[Link]("Matrix multiplication not possible!");
return;
}
int[][] a = new int[r1][c1];
int[][] b = new int[r2][c2];
int[][] product = new int[r1][c2];
[Link]("Enter elements of first matrix:");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
a[i][j] = [Link]();
}
}
[Link]("Enter elements of second matrix:");
for (int i = 0; i < r2; i++) {
for (int j = 0; j < c2; j++) {
b[i][j] = [Link]();
}
}
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
product[i][j] = 0;
for (int k = 0; k < c1; k++) {
product[i][j] += a[i][k] * b[k][j];
}
}
}
[Link]("Resultant Matrix (Multiplication):");
for (int[] row : product) {
for (int val : row) {
[Link](val + " ");
}
[Link]();
}
}
}
Output
3. Write a program to find the transpose of a matrix.
Program
import [Link];
public class L21 {//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter rows: ");
int rows = [Link]();
[Link]("Enter columns: ");
int cols = [Link]();
int[][] matrix = new int[rows][cols];
int[][] transpose = new int[cols][rows];
[Link]("Enter matrix elements:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = [Link]();
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}
[Link]("Transpose of matrix:");
for (int[] row : transpose) {
for (int val : row) {
[Link](val + " ");
}
[Link]();
}
}
}
Output
4. Display the diagonal elements and sum them in a square matrix.
Program
import [Link];
public class L22 {//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter size of square matrix (n): ");
int n = [Link]();
int[][] matrix = new int[n][n];
int sum = 0;
[Link]("Enter matrix elements:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = [Link]();
}
}
[Link]("Diagonal elements:");
for (int i = 0; i < n; i++) {
[Link](matrix[i][i] + " ");
sum += matrix[i][i];
}
[Link]("\nSum of diagonal elements: " + sum);
}
}
Output