package jun2;
/*
* Find the sum of each row and each column of n x m of 2D Matrix
*
*/
public class SumOf2DMatrixRowsAndColumn {
public static void main(String[] args) {
int [][] twoDMatrix = {{ 20, 18, 23, 20, 16 },
{ 30, 20, 18, 21, 20 },
{ 16, 19, 16, 53, 24 },
{ 25, 24, 22, 24, 25 }
};
outputArray(twoDMatrix);
}
public static void outputArray(int[][] array) {
int sum= 0;
int rowSize = array.length;
int[] colSum =new int[array[0].length];
for (int i = 0; i < array.length; i++){
for (int j = 0; j < array[i].length; j++){
sum += array[i][j];
colSum[j] += array[i][j];
}
System.out.println("sum of rows "+ i +" = " + sum);
}
System.out.println(" ");
for(int k=0;k<colSum.length;k++){
System.out.println("sum of columns "+ k +" = " + colSum[k]);
}
}
}
/*output:
sum of rows 0 = 97
sum of rows 1 = 206
sum of rows 2 = 334
sum of rows 3 = 454
sum of columns 0 = 91
sum of columns 1 = 81
sum of columns 2 = 79
sum of columns 3 = 118
sum of columns 4 = 85
*/
Explore the Latest in Tech & Automation: Android, iOS, Mobile Testing with Appium, Web Automation using Selenium, Python & Java Programming, Robot Framework, Shell & PowerShell Scripting, QA Tools, Spring Boot Development, Agentic AI Innovations, Free Software Resources, and Expert Tips on Designing Scalable Automation Frameworks.
Tuesday, June 2, 2015
Java : Program to find the sum of each row and each column of n x m of 2D Matrix
Subscribe to:
Post Comments (Atom)
Testing a New Product: Essential Information Software Testers Must Gather in the AI Era
Learn what information software testers must gather before starting testing a new product, including AI, automation, compatibility, and ...
-
How to change language of device using commandLine Changing language in Android device on-the fly is needed when we are running or exe...
-
How to find the UDID of Android Device What is UDID : It stands for Unique Device Identifier. UDID is required duri...
-
How to upgrade JAVA version in AWS Linux instance On AWS EC2 instance(Linux) if you ever want to upgrade JAVA version to latest ,you can...
No comments:
Post a Comment