English

Write a program which accepts an N × N array and prints the upper triangular matrix.

Advertisements
Advertisements

Question

Write a program which accepts an N × N array and prints the upper triangular matrix.

Code Writing
Advertisements

Solution

import java.util.*;
public class DDA6
{
    void main()
    {
        Scanner sc = new Scanner(System.in);
        int N = 5;
        int AR[ ][ ] = new int[N][N];
        int r, c;        
        System.out.println("Filling array: ");
        for(r = 0; r < N; r++)
        {
            for(c = 0; c < N; c++)
            {
                //System.out.print("Cell[" + r+"]["+ c +"] : " );
                AR[r][c] = (int) (Math.random() * 100);
            }
        }        
        System.out.println("The array is : ");
        for(r = 0; r < N; r++)
        {
            for(c = 0; c < N; c++)
            {
                System.out.print(AR[r][c] + "\t");
            }
            System.out.println();
        }        
        System.out.println("The upper triangular array is : ");
        for(r = 0; r < N; r++)
        {
            for(c = 0; c < N; c++)
            {
                if(c < r)
                {
                    System.out.print(0 + "\t");
                }
                else
                {
                    System.out.print(AR[r][c] + "\t");
                }
            }
            System.out.println(); 
        }
    }
}

Output:

Filling array:
The array is:
38 14 38 4 26
73 99 91 36 24
84 75 62 82 84
20 57 37 32 33
49 58 18 48 21
The upper triangular array is:
38 14 38 4 26
0 99 91 36 24
0 0 62 82 84
 0 0 0 32 33
0 0 0 0 21

shaalaa.com
  Is there an error in this question or solution?
Chapter 11: Programs Overall - Double Dimensional Arrays [Page 252]

APPEARS IN

Rupa Pandit Computer Applications [English] Class 10 ICSE
Chapter 11 Programs Overall
Double Dimensional Arrays | Q 6. | Page 252
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×