0% found this document useful (0 votes)
2 views4 pages

Java - Ex 5

The document contains three Java programs: the first finds the maximum value in an array, the second adds two 3x3 matrices, and the third reverses a word using both a loop and StringBuilder. Each program includes the necessary imports, class definitions, and main methods to execute the tasks. The code snippets demonstrate basic programming concepts in Java such as arrays, loops, and string manipulation.

Uploaded by

aagnikali
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Java - Ex 5

The document contains three Java programs: the first finds the maximum value in an array, the second adds two 3x3 matrices, and the third reverses a word using both a loop and StringBuilder. Each program includes the necessary imports, class definitions, and main methods to execute the tasks. The code snippets demonstrate basic programming concepts in Java such as arrays, loops, and string manipulation.

Uploaded by

aagnikali
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EXPERIMENT – 5

Program 1- Write a Java Program to to find maximum in arr[].

import [Link].*;

public class Main {

public static void main(String[] args) {

// Initialize an array

int[] arr = {12, 45, 7, 91, 23, 64};

// Assume the first element is the maximum

int max = arr[0];

// Loop through the array starting from the second element

for (int i = 1; i < [Link]; i++) {

if (arr[i] > max) {

max = arr[i]; // Update max if a larger value is found

[Link]("The maximum element in the array is: " + max);

}
Program 2 -Java program for addition of two matrices.

import [Link].*;

public class Main {

public static void main(String[] args) {

// Defining two 3x3 matrices

int[][] a = {

{1, 3, 4},

{2, 4, 3},

{3, 4, 5}

};

int[][] b = {

{1, 3, 4},

{2, 4, 3},

{1, 2, 4}

};

// Creating another matrix to store the sum

int[][] c = new int[3][3];

// Adding and printing the addition of 2 matrices

[Link]("Resultant Matrix after Addition:");

for (int i = 0; i < 3; i++) { // Loop for rows

for (int j = 0; j < 3; j++) { // Loop for columns

c[i][j] = a[i][j] + b[i][j];

[Link](c[i][j] + " ");


}

[Link](); // New line after each row

}
Program 3- Java program to reverse a word.

import [Link].*;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter a word to reverse: ");

String original = [Link](); // Takes a single word

// --- METHOD 1: Using a For Loop (Logic Based) ---

String reversed = "";

// Loop starts from the last character and goes to the first

for (int i = [Link]() - 1; i >= 0; i--) {

reversed = reversed + [Link](i);

[Link]("Reversed (Loop): " + reversed);

// --- METHOD 2: Using StringBuilder (Professional way) ---

StringBuilder sb = new StringBuilder(original);

[Link]("Reversed (Built-in): " + [Link]().toString());

[Link]();

You might also like