Java Arrays Programs

 1. Write a program to store 10 students' marks.

Program:

public class Array {
public static void main(String[] args) {
//There are three ways to do
// First way declare and initialize
//int[] marks ={40,80,48,42,39,72,55,90,99,98};
// Second way
//int[] marks; // Declaration
//marks = new int[10];//Memory allocation
//first way-> Declaration and memory allocation
int [] marks = new int[10]; // Takes value from 0 to 9
marks[0]=40;
marks[1]=80;
marks[2]=48;
marks[3]=42;
marks[4]=39;
marks[5]=72;
marks[6]=55;
marks[7]=90;
marks[8]=99;
marks[9]=98;
System.out.println(marks[5]);
System.out.println("Printing using for loop: ");
//for displaying all marks
for(int i=0;i<marks.length;i++){
System.out.println(marks[i]);
}
System.out.println("Printing using for each loop: ");
for(int element: marks){
System.out.println(element);
}

}
}

Output:

72

All marks are: 

40

80

48

42

39

72

55

90

99

98

Printing using for each loop: 

40

80

48

42

39

72

55

90

99

98

public class Multidimensional {
public static void main(String[] args) {
// 2D Array
int[][] price = new int[2][3];//[2]rows X [3]Columns
price[0][0] = 100;
price[0][1] = 200;
price[0][2] = 70;
price[1][0] = 50;
price[1][1] = 20;
price[1][2] = 250;
//System.out.println(price.length);// To check length
for(int i = 0;i<price.length;i++){
for(int j = 0;j<price[i].length;j++){
System.out.print(price[i][j]);
System.out.print(" ");
}
System.out.println("");

}


}
}

Output:

100 200 70 

50 20 250 


2. Create an array of 5 floats and calculate their sum.

Program:

public class Multidimensional {
public static void main(String[] args) {
float[] arr = {11.2f, 45.8f, 72.50f, 62.1f, 77.2f};
float sum = 0;
for (float element : arr) {
sum = sum + element;
}
System.out.println("The addition of 5 floating numbers: " +sum);


}
}

Output:
The addition of 5 floating numbers: 268.8

3. Calculate the average marks from an array containing marks of all students in physics using for-each loop

Program:


public class Multidimensional {
public static void main(String[] args) {
float[] arr = {11.2f, 45.8f, 72.50f, 62.1f, 77.2f};
float sum = 0;
float average = 0;
for (float element : arr) {
sum = sum + element;
average = sum/arr.length;
}
System.out.println("The average marks is: " +average);
}
}

Output:

The average marks is: 53.76

4. Write a program to add two matrices of size 3X3.


Program:

public class Multidimensional {
public static void main(String[] args) {
//creating two matrices
int[][] a = {{7,4,1},{8,5,2},{9,6,3}};
int[][] b = {{3,6,9},{2,5,8,},{1,4,7}};
//creating another matrix to store sum of int a and int b
int[][] c= new int[3][3];//3 rows and 3 column
//for loop for addition of two matrix
for(int i = 0;i<3;i++){//i is for
for(int j = 0;j<3;j++){//j is for column
c[i][j]=a[i][j]+b[i][j];
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}
Output:

10 10 10 
10 10 10 
10 10 10 

5Write a java Program to print the elements of an array in reverse order

Program:

public class Multidimensional {
public static void main(String[] args) {
int [] array = {10,9,8,7,6};
System.out.println("array: ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
System.out.println("Array in reverse order: ");
for (int i = array.length-1; i >= 0; i--) {
System.out.print(array[i] + " ");
}
}
}

Output;

array: 

10 9 8 7 6 

Array in reverse order: 

6 7 8 9 10 


6. write a java program to multiply two matrices

Program:

public class MatrixMultiplication {
public static void main(String[] args) {
int[][] a= {{1,2,3},{4,5,6},{8,9,1}};
int[][] b= {{9,8,7},{6,5,4},{3,2,1}};
int[][] c= new int[3][3];
for(int i =0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++){
c[i][j]+=a[i][j]*b[i][j];

}
System.out.print(c[i][j]+ " ");
}
System.out.println();
}
}
}

Output:

27 48 63 

72 75 72 

72 54 3 


Comments

Popular posts from this blog

Login Page in Java | IntelliJ Idea | Create Login Page

Java Loops Programs