{"id":22893,"date":"2023-04-27T05:46:40","date_gmt":"2023-04-27T00:16:40","guid":{"rendered":"https:\/\/btechgeeks.com\/?p=22893"},"modified":"2023-04-28T09:57:07","modified_gmt":"2023-04-28T04:27:07","slug":"java-program-to-count-total-number-of-positive-elements-in-a-matrix","status":"publish","type":"post","link":"https:\/\/btechgeeks.com\/java-program-to-count-total-number-of-positive-elements-in-a-matrix\/","title":{"rendered":"Java Program to Count Total Number of Positive Elements in a Matrix"},"content":{"rendered":"
In the previous article, we have discussed Java Program to Count the Numbers of 0\u2019s in a Binary Matrix<\/a><\/p>\n In this article we are going to see how we can write a program to count the Total Number of Positive Elements in a matrix in JAVA language.<\/p>\n A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.<\/p>\n Let\u2019s understand it in more simpler way.<\/p>\n Positive elements<\/strong> in a matrix are the elements which are greater than 0.<\/p>\n Let\u2019s see different ways to count total number of positive elements in a Matrix.<\/p>\n Approach:<\/strong><\/p>\n Program:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Program:<\/strong><\/p>\n Explore complete java concepts from the Java programming examples<\/a> and get ready to become a good programmer and crack the java software developer interview with ease.<\/p>\n Related Java Programs:<\/strong><\/p>\n In the previous article, we have discussed Java Program to Count the Numbers of 0\u2019s in a Binary Matrix In this article we are going to see how we can write a program to count the Total Number of Positive Elements in a matrix in JAVA language. Java Program to Count Total Number of Positive … Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":22889,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[15],"tags":[],"yoast_head":"\nJava Program to Count Total Number of Positive Elements in a Matrix<\/h2>\n
| A00 A01 A02 |\r\nMatrix A = | A10 A11 A12 |\r\n | A20 A21 A22 | 3*3<\/pre>\n
\n
Matrix A<\/code>\u00a0represents a 3*3 matrix.<\/li>\n
A<\/code>\u2018 represents the matrix element<\/li>\n
Aij<\/code>\u2018 represents the matrix element at it\u2019s matrix position\/index.<\/li>\n
i<\/code>\u2018 represents the row index<\/li>\n
j<\/code>\u2018 represents the column index<\/li>\n
A00=Aij<\/code>\u00a0 where\u00a0
i=0<\/code>\u00a0and\u00a0
j=0<\/code>,\u00a0\u00a0
A01=aij<\/code>\u00a0where\u00a0
i=0<\/code>\u00a0and\u00a0
j=1<\/code>\u00a0and like this.<\/li>\n
row<\/code>\u00a0value from 0 and\u00a0
column<\/code>\u00a0value from 0.<\/li>\n<\/ul>\n
\n
<\/a>Method-1: Java Program to Count Total Number of Negative Elements in a Matrix By Static Initialization of Array Elements<\/h3>\n
\n
public class matrix\r\n{\r\n public static void main(String args[])\r\n {\r\n \/\/ Initializing the 3X3 matrix i.e. 2D array\r\n int arr[][] = {{19,25,-32},{40,-54,-62},{-70,-20,60}};\r\n int row, col ,count = 0;\r\n\r\n System.out.print(\"The matrix elements are : \");\r\n printMatrix(arr);\r\n\r\n \/\/ Loops to count total number of positive elements in a matrix\r\n for(row=0;row<3;row++)\r\n for(col=0;col<3;col++)\r\n {\r\n if(arr[row][col]>0)\r\n count++;\r\n } \r\n \r\n System.out.println(\"\\nNumber of positive elements in the matrix are : \"+count);\r\n }\r\n\r\n \/\/ Method to print the matrix\r\n static void printMatrix(int arr[][])\r\n {\r\n int row, col;\r\n \/\/ Loop to print the elements\r\n for(row=0;row<3;row++)\r\n {\r\n \/\/ Used for formatting\r\n System.out.print(\"\\n\");\r\n for(col=0;col<3;col++)\r\n {\r\n System.out.print(arr[row][col]+\" \");\r\n }\r\n }\r\n System.out.print(\"\\n\");\r\n }\r\n}<\/pre>\n
Output:\r\n\r\nThe matrix elements are : \r\n19 25 -32 \r\n40 -54 -62 \r\n-70 -20 60\r\n\r\nNumber of positive elements in the matrix are : 4<\/pre>\n
<\/a>Method-2: Java Program to Count Total Number of Negative Elements in a Matrix By Dynamic Initialization of Array Elements<\/h3>\n
\n
import java.util.Scanner;\r\npublic class matrix{\r\n public static void main(String args[])\r\n {\r\n \/\/Scanner class to take input\r\n Scanner scan = new Scanner(System.in);\r\n\r\n \/\/ Initializing the 3X3 matrix i.e. 2D array\r\n int arr[][] = new int[3][3];\r\n int row, col ,count = 0;\r\n\r\n \/\/ Taking matrix1 input\r\n System.out.println(\"Enter matrix elements : \");\r\n for(row=0;row<3;row++)\r\n for(col=0;col<3;col++)\r\n arr[row][col] = scan.nextInt();\r\n\r\n\r\n System.out.print(\"The matrix elements are : \");\r\n printMatrix(arr);\r\n\r\n \/\/ Loops to count total number of positive elements in a matrix\r\n for(row=0;row<3;row++)\r\n for(col=0;col<3;col++)\r\n {\r\n if(arr[row][col]>0)\r\n count++;\r\n } \r\n \r\n System.out.println(\"\\nNumber of positive elements in the matrix are : \"+count);\r\n }\r\n\r\n \/\/ Method to print the matrix\r\n static void printMatrix(int arr[][])\r\n {\r\n int row, col;\r\n \/\/ Loop to print the elements\r\n for(row=0;row<3;row++)\r\n {\r\n \/\/ Used for formatting\r\n System.out.print(\"\\n\");\r\n for(col=0;col<3;col++)\r\n {\r\n System.out.print(arr[row][col]+\" \");\r\n }\r\n }\r\n System.out.print(\"\\n\");\r\n }\r\n}<\/pre>\n
Output:\r\n\r\nEnter matrix elements : \r\nThe matrix elements are : \r\n0 6 -1 \r\n-2 3 8 \r\n7 0 5\r\n\r\nNumber of positive elements in the matrix are : 5<\/pre>\n
\n