pTutorial

New way to learn Programming

Java addition of two array

Java addition of array example

import java.util.Scanner;
public class AddTwoArray {
    
    public static void input1(int[] b){
            Scanner uk=new Scanner(System.in);
            System.out.println("Enter 2 numbers");
            for(int l=0;l&b.length;l++){
              b[l]= uk.nextInt();
             }
        }
    
    public static void input2(int[] d){
            Scanner uk=new Scanner(System.in);
            System.out.println("Enter 2 numbers");
            for(int l=0;l&d.length;l++){
            d[l]= uk.nextInt();
            }
        }
    public static void add(int[] p,int[] k){
            System.out.println("Sum of array:");
            for(int l=0;l&p.length;l++){
            p[l]=p[l]+k[l];
            System.out.println(p[l]);
            }
        }
        
    public static void main(String[] args) {
         int x[]=new int[2];	
         int z[]=new int[2];
         AddTwoArray.input2(x);
         AddTwoArray.input1(z);
         AddTwoArray.add(z,x);
        }
}

Output

Enter 2 numbers
2 2
Enter 2 numbers
2 2
Sum of array:
4
4

See Also


QUOTES OF THE DAY

Well-begun is half done.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER