Sample Programs in java

Java Program for Upper Triangular Matrix

[java]

public class MainUTM {
public static void main(String[] args) {
int[][] a = new int[3][3]; // declaration of an array. here array a
// contain 3-rows and 3-columns.
a[0][0] = 5; // here initializing 1st element of an array
a[0][1] = 3;
a[0][2] = 8;
a[1][0] = 0;
a[1][1] = 2;
a[1][2] = 9;
a[2][0] = 0;
a[2][1] = 0;
a[2][2] = 1;
for (int i = 0; i < a.length; i++) // Here a.length is 3 ie i<3. { for (int j = 0; j < a[i].length; j++) // here a[i].length=3 ie. j<3. { System.out.print(a[i][j] + " "); // here printing an array // elements. } System.out.print("\n"); } } } [/java]

Output

5 3 8

0 2 9

0 0 1