Sum of two 1-D Array using BufferedReader

In this blog, we will work on how to sum two 1-D arrays using Command line And BufferedReader class.

Sum of two array using Command line arguments.

class arr3

{

public static void main(String args[])

{

int[] x = new int[5];

int[] y = new int[5];

int k=0;

for(int i=0;i<=4;i++)

{

x[i] = Integer.parseInt(args[k]); // Command Line Arg

k++;

}

for(int i=0;i<=4;i++)

{

y[i] = Integer.parseInt(args[k]); // Command Line Arg

k++;

}

for(int i=0;i<x.length;i++)

{

System.out.println(x[i]+y[i]); // Sum of two array

}

}

}

Output:

Insert Value in Command Line: 10 20 30 40 50 1 2 3 4 5

11

22

33

44

55

 


Sum of Array using BufferedReader Class

import java.io.*;

class arr4

{

public static void main(String args[]) throws IOException

{

InputStreamReader in1 = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(in1);

int[] x = new int[5];

int[] y = new int[5];

int k=0;

System.out.println(“Enter 1st Array : “);

for(int i=0;i<=4;i++)

{

x[i] = Integer.parseInt(br.readLine());

}

System.out.println(“Enter 2nd Array : “);

for(int i=0;i<=4;i++)

{

y[i] = Integer.parseInt(br.readLine());

}

System.out.println(“Answer : “);

for(int i=0;i<x.length;i++)

{

if(i!=4)

System.out.println(x[i]+y[i+1]);

else

System.out.println(x[i]+y[0]);

}

}

}

Output:

Enter 1st Array:

10

20

30

40

50

Enter 2nd Array:

1

2

3

4

5

Answer:

12

23

34

45

51

 

Hint : In this last program, we have some x[0]+y[1] , x[1]+y[2], x[2]+y[4], x[3]+y[4], x[5]+y[0].

 

 

Basic to Advance Level Tutorial.

 

NoTitle
Enter X[] = {10,20,30,40,50};

Enter Y[] = {1   ,2  ,3   ,4  ,5};

Find Listed Output.

1Out 1: {13,24,35,41,52}
2Out 2: {14,25,31,42,53}
3Out 3: {15,24,33,42,51}

 

[sbscrbr_form]

Now, you can subscribe in our website.

If you found any query on this blog, you can directly comment on this blog post.

406total visits,4visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

1 Comment

  1. September 27, 2016 - 9:03 pm

    Thanks, this website is really useful

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image