Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

How to Add two Array

  Asked By: Craig    Date: Jun 04    Category: Java    Views: 1220
  

i have problem add two array;
I am new to java program so i found this one hard.

int a[]= {1,2,3,4,5};
int b[]= {9,2,4,7,1};

I want to add this whole array to my final output will be

10, 4, 7, 11, 6

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Dang Tran     Answered On: Jun 04

heres the code which does exactly what u want. u may modify it so that
it looks better eg aray sizes should be hardcoded etc

the code is displays the output  on the standard outout which is the
console.

public class AddArray
{
public static void main(String[] args)
{
int a[]= {1,2,3,4,5};
int b[]= {9,2,4,7,1};

/* I want to add  this whole array  to my final  output will be

10, 4, 7, 11, 6
*/

int c[] = new int[5];
for (int i = 0; i < 5; i++)
{
c[i] = a[i] + b[i];

}

for (int i = 0; i < 5; i++) System.out.println(c[i]);

}
}

 
Didn't find what you were looking for? Find more on How to Add two Array Or get search suggestion and latest updates.




Tagged: