Coding Challenge Day-6: Problem 1: Find the product of sum of two diagonals of a square Matrix

Hello! So I’m doing a 30 day coding challenge where I solve around 3-5 questions per day and thought of posting them here on my blog so that you guys can join the challenge too!

Welcome to Coding challenge Day 6: Problem 1! Be sure to post your answers, queries etc in the comments!!

Problem: Find the product of sum of two diagonals of a square Matrix

Sample intput: matrix= {{1,2,3},
{4,5,6},
{7,8,9}}
Output: Diagonal 1: 1 5 9 Sum of diagonal 1: 15
Diagonal 2: 3 5 7 Sum of diagonal 2: 15
Sum of product of sum of two diagonals of square Matrix: 225

Solution:

package array;

public class Program005product_of_sum_of_diagonals_Matrix {
	
	public static void main (String [] args){
		int [][] matrix= {{1,2,3}, {4,5,6},{7,8,9}};
		int sum1=0, sum2=0;
		
		System.out.print("Diagonal 1: ");
		for(int i=0; i<3; i++){
			sum1+=matrix[i][i];
			System.out.print(matrix[i][i] + " ");
		}
		System.out.println(" Sum of diagonal 1: " + sum1);
		System.out.print("Diagonal 2: ");
		for(int i=0; i<3; i++){
			sum2+=matrix[i][3-i-1];
			System.out.print(matrix[i][3-i-1] + " ");
		}
		System.out.println(" Sum of diagonal 2: " + sum2);
		
		System.out.println("Sum of product of sum of two diagonals of square Matrix: "+ sum1*sum2);
		
	}

}

Download Code

Happy Learning!!

Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close