Computer Science, asked by h3lpp1zz, 3 months ago

Anyone knows java? array?

Create two array size of 10. In your program, input 10 lists of each array and compute the sum of each array. Your program should compare the two arrays which has the greater sum.

Sample Run:
Enter list1: 1 2 3 4 5 6 7 8 9 1
Enter list2: 0 0 0 0 2 3 6 5 4 9
Sum of list1 is: 46
Sum of list2 is: 29
list1 is greater that list2

Answers

Answered by anindyaadhikari13
2

Required Answer:-

Question:

  • Create two array of size 10. Input 10 elements for both arrays and compute their sum. Compare their sum and print which array has greater sum. Write a program for this question in Java.

Solution:

Here comes the program.

import java.util.*;

public class Array {

 public static void main(String[] args) {

    Scanner sc=new Scanner(System.in);

    System.out.println("Enter 10 elements for first array.");

    int a[]=new int[10];

    int b[]=new int[10];

    int s1=0, s2=0;

    for(int i=0;i<10;i++)  {

       System.out.print("["+i+"] >> ");

       a[i]=sc.nextInt();

       s1+=a[i];

    }

    System.out.println("------------------\n");

    System.out.println("Enter 10 elements for second array.");

    for(int i=0;i<10;i++)  {

       System.out.print("["+i+"] >> ");

       b[i]=sc.nextInt();

       s2+=b[i];

    }

    System.out.println("First list: ");

    System.out.println(Arrays.toString(a));

    System.out.println("Sum of Elements:  "+s1);

    System.out.println("Second list: ");

    System.out.println(Arrays.toString(b));

    System.out.println("Sum of Elements:  "+s2);

    if(s1>s2)

      System.out.println("First list has greater sum.");

    else if(s1<s2)

      System.out.println("Second list has greater sum.");

    else

      System.out.println("Both lists has same sum.");

    sc.close();

 }

}

Algorithm:

  • Accept elements for both arrays.
  • Compute sum of all elements in both arrays and store them in two different variables.
  • Compare their sum and display the result accordingly.

Refer to the attachment ☑

Attachments:
Similar questions