Question

Write a Java program that gets a 4x4 array of numbers and that calculates the sum...

Write a Java program that gets a 4x4 array of numbers and that calculates the sum and average of numbers on the main diagonal, ie the diagonal that goes from the left corner down to the right corner.

Examples of resulting programs are:

Enter a 4x4 matrix row by row:

1 2 3 4
5 6 7 8
9 1 2 3
4 5 6 7

The sum of numbers in the diagonal is: 16

The average of numbers in the diagonal is: 4
Write a program that:

- Prints appropriate prompt to user.
- Loading a 4x4 matrix.
- Calculates sum and average for the main diagonal
- Prints the result with an appropriate user prompt.
Hint:
Store the matrix in a 4x4 double array.

Homework Answers

Answer #1

Ans

code:-

import java.util.*;

class MyClass {

public static void main(String[ ] args) {

//create object for taking input

Scanner sc = new Scanner(System.in);

//creating double array of 4 by 4

double a[][]=new double[4][4];

double sum=0.0;//initialise sum

//prompt

System.out.println("Enter matrix elements of size 4x4");

//loading matrix a

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

for(int j=0;j<4;j++){

a[i][j]=sc.nextDouble();

if(i==j){//sum the elements on diagonal

sum=sum+a[i][j];

}

}

}//display the sum

System.out.println("The sum of numbers in the diagonal is: "+sum);

//display the average

System.out.println("The average of numbers in the diagonal is: "+(sum/4.0));

  

}

}

code with output:-

.

...

..

If any doubt ask in the comments.

Know the answer?
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
Similar Questions
Write a Java program to randomly create an array of 50 double values. Prompt the user...
Write a Java program to randomly create an array of 50 double values. Prompt the user to enter an index and prints the corresponding array value. Include exception handling that prevents the program from terminating if an out of range index is entered by the user. (HINT: The exception thrown will be ArrayIndexOutOfBounds)
Step 1: Write a Java program that finds the sum of 10 numbers entered by the...
Step 1: Write a Java program that finds the sum of 10 numbers entered by the user. Step 2: Modify the previous program so that it asks the user how many numbers they have, inputs that many numbers, and finally outputs the average of their numbers. The messages to the user should be clear throughout.
Please use Python 3 4). Write a program that asks the user to enter 10 numbers....
Please use Python 3 4). Write a program that asks the user to enter 10 numbers. The program should store the numbers in a list and then display the following data: • The lowest number in the list • The highest number in the list •The total of the numbers in the list • The average of the numbers in the list   Sample input & output: (Prompt) Enter Number 1: (User enter) 4 (Prompt) Enter Number 2: (User enter) 7...
In Java, Write a small program that gets some numbers as command line arguments and finds...
In Java, Write a small program that gets some numbers as command line arguments and finds the minimum of those numbers. You can assume that the user will provide some command line arguments when running the program (so you don’t have to worry about what to do if the user doesn’t provide any arguments -- that won’t happen). Also, you can assume that all the command line arguments will be floating-point numbers, i.e., numbers with a decimal point, like 8.25.
Define a recursive rangeSum(from, to) method that calculates and prints the sum of numbers between the...
Define a recursive rangeSum(from, to) method that calculates and prints the sum of numbers between the two values inclusive. IN JAVA. OUTPUT rangeSum(1,5) -> 15 rangeSum(1,5) -> 1+2+3+4+5=15 rangeSum(-3,4) -> 4 rangeSum(-3,4) -> -3+-2+-1+0+1+2+3+4=4 rangeSum(-5, 7) -> 13 rangeSum(-5, 7) -> -5+-4+-3+-2+-1+0+1+2+3=-9 please write main method as well as output for three above methods.
Python code Write a complete program with for loop which calculates the sum of the numbers...
Python code Write a complete program with for loop which calculates the sum of the numbers from 1 to 100
Write a program in MASM (32 bit) that calculates the sum of all odd numbers in...
Write a program in MASM (32 bit) that calculates the sum of all odd numbers in the Fibonacci sequence between 0 and 1,000,000. ***Please Complete in MASM 32 bit!!***
Write a python program to find the sum of the first n natural numbers, where the...
Write a python program to find the sum of the first n natural numbers, where the value of n is provided by the user. What is the sum of the first 200 numbers? Write a program that finds the average of a series of numbers entered by the user. As in the previous problem, the program will first ask the user how many numbers there are. Note: the average should always be a float. What is the average of 10.2,...
write a java program to find the sum of natural numbers from 1 to 1000 inclusive...
write a java program to find the sum of natural numbers from 1 to 1000 inclusive (using for loop) sample output: sum=500500
Write a Python program which calculates the average of the numbers entered by the user through...
Write a Python program which calculates the average of the numbers entered by the user through the keyboard. Use an interactive loop and ask the user at each iteration if he/she wants to enter more numbers. At the end dispay the average on the screen. Using built-in library functions for finding average is not allowed. Sample output of the program: Enter a number > 23 More numbers (yes or no)? y Enter a number > 4 Do you have more...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT