declare two integer arrays of size 5 create a third array by filling it with the sum of elements from the corresponding index position of the first two arrays
Answers
Hi,
Please find below 2 programs :
addition of 1D arrays elements:
public class Demo7 {
public static void main(String[] args) {
int[]a={6,2,3};
int b[]={4,5,6};
int c[]=new int[a.length];
for(int i=0;i<=a.length-1;i++)
{
c[i]=a[i]+b[i]; //ADDING
}
System.out.println("SUM OF TWO ARRY");
for( int j=0;j<=c.length-1;j++)
{
System.out.println(c[j]); //DISPLAYING
}
}
}
2) Addition of 2 matrix :
public class Demo3 {
public static void main(String[] args) {
int a[][]={{10,20},{10,20}};
int b[][]={{25,20},{45,20}};
Addition add=new Addition(a,b);
}
}
class Addition
{
int add[][]=new int[5][5];
Addition(int a[][],int b[][])
{
for(int i=0;i<=a.length-1;i++)
{
for(int j=0 ;j<=b[i].length-1;j++)
{
add[i][j]=a[i][j]+b[i][j];
System.out.print("\t\t"+add[i][j]);
}
System.out.println();
}
}
}
I hope seeing above program you get some idea..!
Regards,
Ias officer ujjian