
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create a Matrix and Fill Primary Diagonal with 1 and Secondary Diagonal with 2
In Java, a matrix can be represented using a two-dimensional array. Matrices are used for storing and manipulating data that have a tabular structure. Matrices have several properties and operations associated with them, such as addition, subtraction, multiplication, transposition, and determinant calculation.
As per the problem statement we have to Create a Matrix and Fill Primary Diagonal with 1 and Secondary Diagonal with 0.
Let's start!
For instance
Suppose we have 4*4 matrix i.e. 4 rows and 4 columns.
After performing the operation on matrix, the result will be:
Enter the size of the matrix: 4
The resultant matrix is:
1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
Algorithm
Step-1: Define the size of the matrix.
Step-2: Create a square matrix of size 'size'.
Step-3: Fill 1 on the primary diagonal and 0 on the secondary diagonal.
Step-4: Print the result.
Multiple Approaches
We have provided the solution in different approaches.
By Using Static Initialization of Matrix Elements
By Using User Defined Method
Let's see the program along with its output one by one.
Approach-1: By Using Static Initialization of Array Elements
In this approach, matrix elements will be initialized in the program. Then as per the algorithm Create a Matrix and Fill Primary Diagonal with 1 and Secondary Diagonal with 0.
Example
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Define the size of the matrix System.out.print("Enter the size of the matrix: "); int size = scanner.nextInt(); // Create a square matrix of size 'size' int[][] matrix = new int[size][size]; // Filling the matrix for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (i == j) { // Filling 1 on the primary diagonal matrix[i][j] = 1; } else if (i + j == size - 1) { // Filling 0 on the secondary diagonal matrix[i][j] = 0; } } } // Print the matrix System.out.println("The resultant matrix is:"); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); } } }
Output
Enter the size of the matrix: 4 The resultant matrix is: 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
Approach-2: By Using User Defined Method
In this approach, matrix elements will be initialized in the program. Then call a user defined method by passing the matrix as parameter and inside method as per the algorithm create a matrix and fill primary diagonal with 1 and secondary diagonal with 0.
Example
import java.util.Scanner; public class Main { public static void main(String[] args) { // taking the dimension of the matrix Scanner scanner = new Scanner(System.in); // Define the size of the matrix System.out.print("Enter the size of the matrix: "); int size = scanner.nextInt(); // calling user defined method func(size); } // user defined method public static void func(int size) { // Create a square matrix of size 'size' int[][] matrix = new int[size][size]; // Filling the matrix for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (i == j) { // Filling 1 on the primary diagonal matrix[i][j] = 1; } else if (i + j == size - 1) { // Filling 0 on the secondary diagonal matrix[i][j] = 0; } } } // Print the matrix System.out.println("The resultant matrix is:"); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); } } }
Output
Enter the size of the matrix: 5 The resultant matrix is: 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1
In this article, we explored how to create a matrix and fill primary diagonal with 1 and secondary diagonal with 0 by using Java programming language.