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

Computer Project 45 Pages

The document provides two Java programs: one to find the largest element in an array and another to reverse an array. The first program iterates through the array to identify the maximum value, while the second program swaps elements to reverse the array. Each program includes variable descriptions for clarity.

Uploaded by

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

Computer Project 45 Pages

The document provides two Java programs: one to find the largest element in an array and another to reverse an array. The first program iterates through the array to identify the maximum value, while the second program swaps elements to reverse the array. Each program includes variable descriptions for clarity.

Uploaded by

Pradyumn Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

QUESTION

Write a program to find the largest element in an array.

SOLUTION
public class LargestElement {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
System.out.println("Largest element: " + max);
}
}

VDT
Variable Type Description

arr int[] Array to store elements

max int Variable to store largest


element

i int Loop counter


QUESTION
Write a program to reverse an array.

SOLUTION
public class ReverseArray {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length / 2; i++) {
int temp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = temp;
}
for (int num : arr) {
System.out.print(num + " ");
}
}
}

VDT
Variable Type Description

arr int[] Array to store elements

temp int Temporary variable for


swapping

i int Loop counter

You might also like