Sum of Two Arrays | Write a program in Java to find the sum of two arrays. | ISC Computer Science | ICSE Computer Applications |

Sum of Array

Algorithm

  1.   Start
  2. a[]=null
  3. b[]=null
  4. sum[]=null
  5. i:=0
  6. n:=0
  7. Print "Enter the length of both the array"
  8. Read n
  9. a=new int[n]
  10. b=new int[n]
  11. sum=new int[n]
  12. i=0
  13. If i<n, go to step 14 or else go to step 19
  14. Print "Enter a number in the first array"
  15. Read a[i]
  16. Print "Enter a number in the second array"
  17. Read b[i]
  18. i++. Go to step 13
  19. i=0
  20. If i<n, go to step 21 or else go to step 23
  21. sum[i]=a[i]+b[i]
  22. i++. Go to step 20
  23. i=0
  24. If i<n, go to step 25 or else go to step 27
  25. Print sum[i]
  26. i++. Go to step 24
  27. Stop

Program

Buffered Reader

import java.io.*;
class SumArray
{
    int a[], b[], sum[], i, n;
    void accept() throws IOException
    {
        BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
        System.out.println("Enter the length of both the array");
        n=Integer.parseInt(br.readLine());
        a=new int[n];
        b=new int[n];
        sum=new int[n];
        for(i=0;i<n;i++)
        {
            System.out.println("Enter a number in the first array");
            a[i]=Integer.parseInt(br.readLine());
            System.out.println("Enter a number in the second array");
            b[i]=Integer.parseInt(br.readLine());
        }
    }
    
    void add()
    {
        for(i=0;i<n;i++)
        {
            sum[i]=a[i]+b[i];
        }
    }
    
    void display()
    {
        for(i=0;i<n;i++)
        {
            System.out.println(sum[i]);
        }
    }
    
    void main() throws IOException
    {
        accept();
        add();
        display();
    }
}

Scanner

import java.util.*;
class SumArray
{
    int a[], b[], sum[], i, n;
    void accept() 
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the length of both the array");
        n=sc.nextInt();
        a=new int[n];
        b=new int[n];
        sum=new int[n];
        for(i=0;i<n;i++)
        {
            System.out.println("Enter a number in the first array");
            a[i]=sc.nextInt();
            System.out.println("Enter a number in the second array");
            b[i]=sc.nextInt();
        }
    }
    
    void add()
    {
        for(i=0;i<n;i++)
        {
            sum[i]=a[i]+b[i];
        }
    }
    
    void display()
    {
        for(i=0;i<n;i++)
        {
            System.out.println(sum[i]);
        }
    }
    
    void main() 
    {
        accept();
        add();
        display();
    }
}

Documentation

Variable Description

 Data TypeVariable NameDescription
 int[]To store the first array 
 int[]To store the second array 
 int[]sum To store the sum of the two arrays 
 intnTo store the length of the arrays 
 intTo act as a counter variable 

Method Description

 Return TypeMethod NameDescription
 voidaccept() To accept data from the user  
 voidadd() To find the sum of the two arrays 
 voiddisplay() To display the sum of the array 
 voidmain() To act as an entry gateway for the compiler. 

Output


Sum of Array Output




Share with all ISC Computer Science Students you know.

Comment below with all your program requests and blog suggestions.

Follow us on social media by clicking on these links. 
Instagram : @ISCJavaCode
Twitter: @ISCJavaCode
Reddit: @ISCJavaCode
Pinterest: @ISCJavaCode
Facebook: @ISCJavaCode
LinkedIn : ISC Java Code 


Stay Healthy
Stay Happy
Stay Helpful



Comments

You might also like:

Conversion of Complex Sentences to Simple Sentences

Emirp Number | Write a program in Java to print an Emirp Number. | ISC Computer Science | ICSE Computer Applications

Conversion of Complex Sentences to Compound Sentences