Question

Write a java program that declares 10 element array (of type integers), creates and initializes the...

Write a java program that declares 10 element array (of type integers), creates and initializes the array, and perform the sum of elements of the array using for loop.  

public class SumArray {

public static void main (String[], args) {

} // end of main

} // end of SumArray class

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class SumArray2 {
    public static void main (String[] args) {
        // declares 10 element array (of type integers)
        // creates and initializes the array
        int arr[] = new int[10];

        // reading 10 values form user
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter 10 values for array");
        for(int i = 0;i<10;i++){
            arr[i] = scan.nextInt();
        }

        // calculating sum
        int sum = 0;
        for(int i = 0;i<10;i++){
            sum += arr[i];
        }

        // printing sum
        System.out.println("Sum: "+sum);
    } // end of main
} // end of SumArray class

Add a comment
Know the answer?
Add Answer to:
Write a java program that declares 10 element array (of type integers), creates and initializes the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that initializes an array with ten random integers and then prints out the...

    Write a program that initializes an array with ten random integers and then prints out the following: Every element at an even index; Every even element All elements in reverse order; Only the first and last elements; The minimum and maximum element The sum of all elements The alternating sum of all elements, where the alternating sum contains all elements at even index added, and the elements at odd index subtracted. Please write comments above the piece of code that...

  • (JAVA) Given an array of unique positive integers, write a function findSums that takes the array...

    (JAVA) Given an array of unique positive integers, write a function findSums that takes the array input and: 1. Creates a hashtable called “hashT” and inserts all the elements of the input array in the hashtable. 2. Finds pairs of elements in the hashtable whose sum is another element (sum) in the hashtable and print the pairs in the console. 3. Returns another hashtable names “sums” of those sum elements. For example: Input: [1,5,4,6,7,9] Output: [6,5,7,9] Explanation: 6 = 1...

  • Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a...

    Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a method named FillArray to interactively fill the array with any number of values up to 10 or until a sentinel value (999) is entered. If an entry is not an integer, reprompt the user. Call a second method named Statistics that accepts out parameters for the highest value in the array, lowest value in the array, sum of the values in the array, and...

  • Write a Java program to find the duplicate values of an array of integer values. import...

    Write a Java program to find the duplicate values of an array of integer values. import java.util.Arrays; public class ex7DuplicateValue { public static void main(String[] args) { int[] my_array = {1, 2, 3, 3, 4, 5, 6, 2}; } } //Duplicate Element : 2 //Duplicate Element : 3

  • IN C, write a program that declares an array of 100 integers and initializes it to...

    IN C, write a program that declares an array of 100 integers and initializes it to the values (100, 99, …, 2, 1)

  • Write a JAVA program that declares and initializes an array with the following numbers: 10, 89,...

    Write a JAVA program that declares and initializes an array with the following numbers: 10, 89, 50, 24, 60, 1, 15. Then ask the user for a number and check if the array contains that number. If it does, return a message to the user (print to screen) that says

  • Write code in static void main method class, which creates a boolean array called flags, size...

    Write code in static void main method class, which creates a boolean array called flags, size 10, and using a for loop sets each element to alternating values (true at index zero, false at index one Write code in a static void main method class, which creates a float array called sales, size 5, uses an initializer list to instantiate its elements with positive values having two decimal places (example 7.25), and using a for loop reads those values of...

  • Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as...

    Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as a String and returns each line in the file in an array. Have each element of the array be a String. Do not include the newline at the end of each line. Use a BufferedReader object to read the file contents. Note, the contents of input.txt will be different (including the number of lines) for each test case. Example: getLines( "input.txt" ) returns (an...

  • For Java - Write a program that creates an array of 10 integers. Ask the user...

    For Java - Write a program that creates an array of 10 integers. Ask the user to enter the subscript (index) of an element, then displays the element located at that subscript. If the subscript entered is out of bounds (less than 0 or greater than length - 1), display the message "Out of Bounds".

  • java question: Write a program that reads in data from a text file named in.txt. Compute...

    java question: Write a program that reads in data from a text file named in.txt. Compute the sum of all the valid integers in the input file. Likewise, compute the sum of all the valid doubles in the input file. Write the former to an output file called int_total.txt, and write the latter to an output file called double_total.txt. B) Write a program that converts the Java source code from the next-line brace style to the end-of-line brace style. For...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT