Question

Code a repetition control structure that: Processes an array of integers in index order to sum...

Code a repetition control structure that:

Processes an array of integers in index order to sum its elements.

Only odd numbers should be included in the sum and processing should cease when either a negative integer or (be careful) the end of the array is encountered.

The array can be any length but you do not have to deal with an empty array.

Declare all required variables and give them initial values if required.

Display the output using myWindow.writeOutLine(…).

You do not need to code a method.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

public class Array {

    public static void main(String[] args) {

        int arr[] = { 2, 1, 8, 5, 6, 9, 1, 23, 6, 47, 8, 9, 1, -8, 6, 7, 5, 5, 7, 9, 9, 2, 1 };

        int sum = 0;

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

            if (arr[i] % 2 != 0) {

                sum += arr[i];

            }

            if (arr[i] < 0)

                break;

        }

        // you need to make the Window class to run this

        myWindow.writeOutLine(sum);

        // normally System.out.println(); line is used to write data to console.

        // System.out.println(sum);

    }

}

1 2 3 4 public class Array ( Run Debug public static void main(String[] args) { int arr[] = { 2, 1, 8, 5, 6, 9, 1, 23, 6, 47,

OUTPUT:

96

Please upvote if you like my answer and comment below if you have any queries or need any further explanation.

Add a comment
Know the answer?
Add Answer to:
Code a repetition control structure that: Processes an array of integers in index order to sum...
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
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
ADVERTISEMENT