In the previous article, we have seen Java Program to Swap Diagonals of a Matrix
In this article we are going to see how we can write a program to increment each element of the matrix by a specified number in JAVA language.
Java Program to Increment the Matrix Elements by One
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.
Let’s understand it in more simpler way.
| A00 A01 A02 | Matrix A = | A10 A11 A12 | | A20 A21 A22 | 3*3
Matrix A
represents a 3*3 matrix.- ‘
A
‘ represents the matrix element - ‘
Aij
‘ represents the matrix element at it’s matrix position/index. - ‘
i
‘ represents the row index - ‘
j
‘ represents the column index - Means
A00=Aij
wherei=0
andj=0
,A01=aij
wherei=0
andj=1
and like this. - Here we have started
row
value from 0 andcolumn
value from 0.
Let’s see different ways to Increment the Matrix Elements by a specified number.
Method-1: Java Program to Increment the Matrix Elements by a Specified Number By Static Initialization of Array Elements
Approach:
- Declare and Initialize an array of 3*3 means main matrix is ready.
- Declare another resultant array of 3*3 means resultant matrix.
- Now take two for loops and iterate each element of the main matrix and increment each element by the specified number.
- Go on storing each incremented element to the resultant matrix.
- Print the resultant matrix.
Program:
public class Matrix { public static void main(String args[]) { // Initializing the 3X3 matrix i.e. 2D array int arr[][] = {{19,25,32},{40,54,62},{70,20,60}}; //Resultant matrix int resArr[][] = new int[3][3]; //A number specified int n=10; int row, col ; System.out.print("The matrix is : "); printMatrix(arr); // Loops to increment each element of matrix by a specified number for(row=0;row<3;row++) { for(col=0;col<3;col++) { resArr[row][col] = arr[row][col]+n; } } System.out.print("\nThe matrix after incrementing by "+n+" is : "); printMatrix(resArr); } // Method to print the matrix static void printMatrix(int arr[][]) { int row, col; // Loop to print the elements for(row=0;row<3;row++) { // Used for formatting System.out.print("\n"); for(col=0;col<3;col++) { System.out.print(arr[row][col]+" "); } } System.out.print("\n"); } }
Output: The matrix is : 19 25 32 40 54 62 70 20 60 The matrix after incrementing by 10 is : 29 35 42 50 64 72 80 30 70
Method-2: Java Program to Increment the Matrix Elements by a Specified Number By Dynamic Initialization of Array Elements
Approach:
- Declare an array of 3*3.
- Take the input of array elements from the user, so now main matrix is ready.
- Declare another resultant array of 3*3 means resultant matrix.
- Now take two for loops and iterate each element of the main matrix and increment each element by the specified number.
- Go on storing each incremented element to the resultant matrix.
- Print the resultant matrix.
Program:
import java.util.*; public class Matrix { public static void main(String args[]) { //Scanner class to take input Scanner scan = new Scanner(System.in); // Initializing the 3X3 matrix i.e. 2D array int arr[][] = new int[3][3]; //Taking a number input from user //which will be used to increment matrix System.out.println("Enter a number : "); int n=scan.nextInt(); //Resultant matrix int resArr[][] = new int[3][3]; int row, col ; // Taking main matrix input System.out.println("Enter matrix elements : "); for(row=0;row<3;row++) for(col=0;col<3;col++) arr[row][col] = scan.nextInt(); System.out.print("The matrix is : "); printMatrix(arr); // Loops to increment each element of matrix by a specified number for(row=0;row<3;row++) { for(col=0;col<3;col++) { resArr[row][col] = arr[row][col]+n; } } System.out.print("\nThe matrix after incrementing by "+n+" is : "); printMatrix(resArr); } // Method to print the matrix static void printMatrix(int arr[][]) { int row, col; // Loop to print the elements for(row=0;row<3;row++) { // Used for formatting System.out.print("\n"); for(col=0;col<3;col++) { System.out.print(arr[row][col]+" "); } } System.out.print("\n"); } }
Output: Enter a number : 10 Enter matrix elements : 1 1 1 1 1 1 1 1 1 1 The matrix is : 1 1 1 1 1 1 1 1 1 The matrix after incrementing by 10 is : 11 11 11 11 11 11 11 11 11
Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.
Related Java Programs: