Can someone explain working of this code ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can someone explain working of this code ?

public class Program { public static void main(String[] args) { int a[][]= new int[4][]; a[0]= new int [1]; a[1]= new int [2]; a[2]= new int [3]; a[3]= new int [4]; int i,j,k=0; for(i=0;i<4;i++) for(j=0;j<i+1;j++) { a[i][j]=k; k++; } for(i=0;i<4;i++) { for(j=0;j<i+1;j++) System .out .print (a[i][j]+" "); System .out .println (); } } }

5th Sep 2020, 9:58 AM
shubham singh
shubham singh - avatar
1 Answer
+ 1
public class Program { public static void main(String[] args) { int a[][]= new int[4][]; //declaring array of 4 rows but not columns a[0]= new int [1]; //declaring 0th row of 1 comumn a[1]= new int [2]; //declaring 1st row of 2 comumn a[2]= new int [3]; //declaring 2nd row of 3 comumn a[3]= new int [4]; //declaring 3rd row of 4 comumn int i,j,k=0; for(i=0;i<4;i++) // you have 4 rows declared so you can use upto i<=3 for(j=0;j<i+1;j++) // colomns are 0,1,2,3 as above decakred { a[i][j]=k; k++; } //printing only those values, which are you assigned for(i=0;i<4;i++) { for(j=0;j<i+1;j++) System .out .print (a[i][j]+" "); System .out .println (); } } }
5th Sep 2020, 1:23 PM
Jayakrishna 🇮🇳