Question

In: Computer Science

Write a program in java which store 10 numbers and find the sum of odd and...

  1. Write a program in java which store 10 numbers and find the sum of odd and even numbers.
  2. Create a program that uses a two dimensional array that can store integer values inside. (Just create an array with your own defined rows and columns). Make a method called Square, which gets each of the value inside the array and squares it. Make another method called ShowNumbers which shows the squared numbers.

Solutions

Expert Solution

Below is your code:

public class NumberArray {

        public static void main(String[] args) {
                // problem 1 to calculate sum of 10 numbers in array
                int array1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                // printing array
                System.out.println("Array 1 is: ");
                for (int i = 0; i < array1.length; i++) {
                        System.out.print(array1[i] + " ");
                }
                System.out.println();
                // printing sum of array
                calculateSum(array1);
                // problem 2 to square the numbers in two dimensional array
                int array2[][] = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 },
                                { 11, 12, 13, 14, 15 }, { 16, 17, 18, 19, 20 } };
                System.out.println("\nArray before Squaring: ");
                ShowNumbers(array2);
                array2 = Square(array2);
                System.out.println("Array After Squaring: ");
                ShowNumbers(array2);
        }

        public static void calculateSum(int array[]) {
                int sumOdd = 0;
                int sumEven = 0;
                for (int i = 0; i < array.length; i++) {
                        int num = array[i];
                        if (num % 2 == 0) {
                                sumEven = sumEven + num;
                        } else {
                                sumOdd = sumOdd + num;
                        }
                }
                System.out.println("Sum of Odd numbers: " + sumOdd);
                System.out.println("Sum of Even numbers: " + sumEven);
        }

        public static int[][] Square(int array[][]) {
                for (int i = 0; i < array.length; i++) {
                        for (int j = 0; j < array[i].length; j++) {
                                array[i][j] = array[i][j] * array[i][j];
                        }
                }
                return array;
        }

        public static void ShowNumbers(int array[][]) {
                for (int i = 0; i < array.length; i++) {
                        for (int j = 0; j < array[i].length; j++) {
                                System.out.print(array[i][j] + " ");
                        }
                        System.out.println();
                }
        }
}

Output

Array 1 is:
1 2 3 4 5 6 7 8 9 10
Sum of Odd numbers: 25
Sum of Even numbers: 30

Array before Squaring:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
Array After Squaring:
1 4 9 16 25
36 49 64 81 100
121 144 169 196 225
256 289 324 361 400


Related Solutions

Write a program in java which store 10 numbers and find the sum of odd and...
Write a program in java which store 10 numbers and find the sum of odd and even numbers. Create a program that uses a two dimensional array that can store integer values inside. (Just create an array with your own defined rows and columns). Make a method called Square, which gets each of the value inside the array and squares it. Make another method called ShowNumbers which shows the squared numbers. Write a program in java which has an array...
write in c plus plus Write a program that computes the sum of the odd numbers...
write in c plus plus Write a program that computes the sum of the odd numbers and the sum of the even numbers between two values a and b. For example a=1 and b=10
Write a program in C++ that computes the sum of odd numbers between 1 and 117....
Write a program in C++ that computes the sum of odd numbers between 1 and 117. Execute the program and submit a screen capture of the program and its results.
Write an assembly program In MASM assembler to calculate the sum of odd numbers only stored...
Write an assembly program In MASM assembler to calculate the sum of odd numbers only stored in a 16-bit integers array. The size of the array is provided in a separate variable. Use the following design: Sum = 0 FOR i = 0 TO SizeOfArray - 1     IF Arr[i] % 2 = 1           Sum = Sum + Arr[i]     END IF END FOR
Write a MIPS Assembly program that computes the sum of all the odd numbers from 1...
Write a MIPS Assembly program that computes the sum of all the odd numbers from 1 ..99 and print out the answer.
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by...
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by 7, and compute the average of those numbers, print both the sum and the average with appropriate messages to the screen. Run the program. Capture the console output. Put the program code and console output at the end of your text file,
Write a Java program to find the sum of all integers between 200 to 250 which...
Write a Java program to find the sum of all integers between 200 to 250 which are divisible by 7. Sample Output: Numbers between 200 and 250, divisible by 7: 203 210 217 224 231 238 245 The sum is: 1568
write a program to find the maximum possible sum such that no two chosen numbers are...
write a program to find the maximum possible sum such that no two chosen numbers are adjacent either vertically, horizontally, or diagonally. code in java
The Sum and The Average In C++, Write a program that reads in 10 integer numbers....
The Sum and The Average In C++, Write a program that reads in 10 integer numbers. Your program should do the following things: Use a Do statement Determine the number positive or negative Count the numbers of positive numbers, and negative Outputs the sum of: all the numbers greater than zero all the numbers less than zero (which will be a negative number or zero) all the numbers Calculate the average of all the numbers. The user enters the ten...
Write a program that will calculate the sum of the first n odd integers, and the...
Write a program that will calculate the sum of the first n odd integers, and the sum of the first n even integers. Use a Do while loop Use a for loop Here is a sample of how the program should run: How many odd integers would you like to add? 5 Count Odd Integer Sum 1 1 1 2 3 4 3 5 9 4 7 16 5 9 25 The sum of the first 5 odd integers is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT