The document contains 8 code examples that demonstrate various operations on arrays in Java, including:
1) Finding the length of an array
2) Calculating the average of array elements
3) Adding all elements of an array
4) Reversing an array
5) Sorting an array in ascending order
6) Adding two matrices using multi-dimensional arrays
7) Printing elements of an array at odd positions
8) Printing elements of an array at even positions
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
30 views
Array Solution
The document contains 8 code examples that demonstrate various operations on arrays in Java, including:
1) Finding the length of an array
2) Calculating the average of array elements
3) Adding all elements of an array
4) Reversing an array
5) Sorting an array in ascending order
6) Adding two matrices using multi-dimensional arrays
7) Printing elements of an array at odd positions
8) Printing elements of an array at even positions
System.out.println("Number of elements in the given String array: " + names.length);
2. Java Program to Calculate average of numbers using Array
import java.util.Scanner; public class arrayaverage {
public static void main(String[] args) {
System.out.println("How many numbers you want to enter?"); Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); /* Declaring array of n elements, the value * of n is provided by the user */ double[] arr = new double[n]; double total = 0;
for(int i=0; i<arr.length; i++){
System.out.print("Enter Element No."+(i+1)+": "); arr[i] = scanner.nextDouble(); } scanner.close(); for(int i=0; i<arr.length; i++){ total = total + arr[i]; }
3. Java Program to Add the elements of an Array
import java.util.Scanner; class SumDemo{ public static void main(String args[]){ Scanner scanner = new Scanner(System.in); int[] array = new int[10]; int sum = 0; System.out.println("Enter the elements:"); for (int i=0; i<10; i++) { array[i] = scanner.nextInt(); } for( int num : array) { sum = sum+num; } System.out.println("Sum of array elements is:"+sum); } } 4. Java Program to reverse an array import java.util.Scanner; public class reverse { public static void main(String args[]) { int counter, i=0, j=0, temp; int number[] = new int[100]; Scanner scanner = new Scanner(System.in); System.out.print("How many elements you want to enter: "); counter = scanner.nextInt();
/* This loop stores all the elements that we enter in an
* the array number. First element is at number[0], second at * number[1] and so on */ for(i=0; i<counter; i++) { System.out.print("Enter Array Element"+(i+1)+": "); number[i] = scanner.nextInt(); }
/* Here we are writing the logic to swap first element with
* last element, second last element with second element and * so on. On the first iteration of while loop i is the index * of first element and j is the index of last. On the second * iteration i is the index of second and j is the index of * second last. */ j = i - 1; i = 0; scanner.close(); while(i<j) { temp = number[i]; number[i] = number[j]; number[j] = temp; i++; j--; }
System.out.print("Reversed array: ");
for(i=0; i<counter; i++) { System.out.print(number[i]+ " "); } } } 5. Java Program to sort an array in ascending order import java.util.Scanner; public class sortarray { public static void main(String[] args) { int count, temp;
//User inputs the array size
Scanner scan = new Scanner(System.in); System.out.print("Enter number of elements you want in the array: "); count = scan.nextInt();
int num[] = new int[count];
System.out.println("Enter array elements:"); for (int i = 0; i < count; i++) { num[i] = scan.nextInt(); } scan.close(); for (int i = 0; i < count; i++) { for (int j = i + 1; j < count; j++) { if (num[i] > num[j]) { temp = num[i]; num[i] = num[j]; num[j] = temp; } } } System.out.print("Array Elements in Ascending Order: "); for (int i = 0; i < count - 1; i++) { System.out.print(num[i] + ", "); } System.out.print(num[count - 1]); } }
6. Java Program to Add Two Matrix Using Multi-dimensional Arrays
7. Java Program to print the elements of an array present on odd Position
public class oddprint { public static void main(String[] args) {
//Initializing the array
int [] numbers = new int [] {1, 3, 5, 7, 9, 11, 13};
System.out.println("Array Elements on odd Positions: ");
/* Note we are using i = i+2 as we are only traversing odd positions * Important point here is that the array indices start with 0, which * means the odd positions such as 1st, 3rd and 5th positions are having * indices 0, 2, 4 and so on. That's why numbers[0] prints 1st position * element of the array. */ for (int i = 0; i < numbers.length; i = i+2) { System.out.println(numbers[i]); } } } 8. Java Program to print the elements of an array present on even Position public class evenprint {
public static void main(String[] args) {
//Initializing the array
int [] numbers = new int [] {5, 12, 16, 3, 9, 7, 1, 100};
System.out.println("Array Elements on even Positions: ");
/* Note we are using i = i+2 as we are only traversing even positions
* Important point here is that the array indices start with 0, which
* means the even positions such as 2nd, 4th and 6th positions are having
* indices 1, 3, 5 and so on. That's why numbers[1] prints 2nd position