Advertisements
Advertisements
प्रश्न
Write a program which accepts an N × N array and prints the upper triangular matrix.
कोड लेखन
Advertisements
उत्तर
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
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
