45 Program to add two jagged array in Java

Code:

import java.util.*;
import java.io.*;

class Add
{
public static void main( String args[] )
throws IOException {
BufferedReader scan = new BufferedReader( new InputStreamReader( System.in ) );

int a[][] = new int[3][];
int b[][] = new int[3][];
int c[][] = new int[3][];

int i,j;

a[0] = new int[2];
a[1] = new int[3];
a[2] = new int[4];


b[0] = new int[2];
b[1] = new int[3];
b[2] = new int[4];


c[0] = new int[2];
c[1] = new int[3];
c[2] = new int[4];




System.out.println("\n\n Enter 9 elements= ");

for(i=0 ; i< a.length ; i++)
{
for( j=0 ; j<a[i].length ; j++ )
{
a[i][j]= Integer.parseInt(scan.readLine());
}
}

System.out.println("\n\n Enter 9 elements= ");

for(i=0 ; i< b.length ; i++)
{
for( j=0 ; j<b[i].length ; j++ )
{
b[i][j]= Integer.parseInt(scan.readLine());
}
}

System.out.println("\n\n A= ");

for(i=0 ; i< c.length ; i++)
{
for( j=0 ; j<c[i].length ; j++ )
{

System.out.print(" "+a[i][j]);
}
System.out.println("\n");
}

System.out.println("\n\n B= ");

for(i=0 ; i< c.length ; i++)
{
for( j=0 ; j<c[i].length ; j++ )
{

System.out.print(" "+b[i][j]);
}
System.out.println("\n");
}
System.out.println("\n\n Addition= ");

for(i=0 ; i< c.length ; i++)
{
for( j=0 ; j<c[i].length ; j++ )
{
c[i][j] = a[i][j] + b[i][j];

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

}
}


Output:


Previous
Next Post »