w3resource

Java: Adds up columns and rows of given table as shown in the specified figure


Add Rows and Columns of Spreadsheet Table

Your task is to develop a small piece of spreadsheet software. Write a Java program that adds up the columns and rows of a given table as shown in the specified figure.

Visual Presentation:

Java Basic Exercises: Adds up columns and rows of given table as shown in the specified figure.


Input:
n (the size of row and column of the given table)
1st row of the table
2nd row of the table
:
:
n th row of the table
The input ends with a line consisting of a single 0.
Output:
For each dataset, print the table with sum of rows and columns.

Sample Solution:

Java Code:

// Importing the necessary Java utility package
import java.util.*;

// Main class named "Main"
public class Main {

    // Main method, the entry point of the program
    public static void main(String[] args) {

        // Creating a Scanner object to read input from the console
        Scanner sc = new Scanner(System.in);

        // Prompting the user to input the number of rows/columns (0 to exit)
        System.out.println("Input number of rows/columns (0 to exit)");

        // Continuous loop to handle multiple inputs until 0 is entered
        while (true) {

            // Reading an integer from the user
            int n = sc.nextInt();

            // Checking if the entered value is 0, and breaking the loop if true
            if (n == 0) break;

            // Creating a 2D array 'map' with dimensions (n+1) x (n+1)
            int[][] map = new int[n + 1][n + 1];

            // Nested loops to populate the 'map' array with user inputs and calculate row sums
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    map[i][j] = sc.nextInt();
                    map[i][n] += map[i][j];
                }
                map[n][n] += map[i][n];
            }

            // Nested loops to calculate column sums
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    map[n][i] += map[j][i];
                }
            }

            // Printing the result header
            System.out.println("Result:");

            // Nested loops to print the final 'map' array
            for (int i = 0; i < n + 1; i++) {
                for (int j = 0; j < n + 1; j++) {
                    // Formatting and printing each element of the array
                    System.out.printf("%5d", map[i][j]);
                }
                // Moving to the next line after each row is printed
                System.out.println();
            }
        }
    }
} 

Sample Output:

Input number of rows/columns (0 to exit)
4
25 69 51 26
68 35 29 54
54 57 45 63
61 68 47 59
Result:
   25   69   51   26  171
   68   35   29   54  186
   54   57   45   63  219
   61   68   47   59  235
  208  229  172  202  811

Flowchart:

Flowchart: Find the number of combinations.


For more Practice: Solve these Related Problems:

  • Write a Java program to compute the sum of each row and column in a spreadsheet and identify the row with the maximum total.
  • Write a Java program to add rows and columns of a table and then compute the average for each row.
  • Write a Java program to sum the rows and columns of a table and display the result in a formatted matrix.
  • Write a Java program to perform row and column summation on a table and then transpose the resulting sum matrix.

Go to:


PREV : Count Combinations Satisfying Sum of Variables.
NEXT : Pair Words with Page Numbers.

Java Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.