
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 it with Palindrome Numbers in Java
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 it with Palindrome Number.
Let's start!
For instance
Suppose we have 4*5 matrix:
After performing the operation on matrix, the result will be:
Enter the number of rows: 4
Enter the number of columns: 5
Matrix with Palindrome Numbers:
1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 111
Algorithm
Step-1: Take the size of the matrix.
Step-2: Create a square matrix.
Step-3: Check for Palindrome number and insert it into the matrix.
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 it with Palindrome Number.
Example
import java.util.Scanner; public class Main { // main method public static void main(String[] args) { // taking the size of the matrix Scanner scanner = new Scanner(System.in); System.out.print("Enter the number of rows: "); int rows = scanner.nextInt(); System.out.print("Enter the number of columns: "); int columns = scanner.nextInt(); // create a square matrix int[][] matrix = new int[rows][columns]; // Fill the matrix with palindrome numbers fillMatrixWithPalindromes(matrix); // Print the matrix System.out.println("Matrix with Palindrome Numbers:"); printMatrix(matrix); } // Function to create a matrix and fill it with Palindrome numbers public static void fillMatrixWithPalindromes(int[][] matrix) { int num = 1; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { // Find the next palindrome number while (!isPalindrome(num)) { num++; } // Fill the matrix with the palindrome number matrix[i][j] = num; num++; } } } // Function to check if a number is an Palindrome number or not public static boolean isPalindrome(int number) { String str = String.valueOf(number); int i = 0; int j = str.length() - 1; while (i < j) { if (str.charAt(i) != str.charAt(j)) { return false; } i++; j--; } return true; } // Function to display the matrix public static void printMatrix(int[][] matrix) { for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); } } }
Output
Enter the number of rows: 4 Enter the number of columns: 5 Matrix with Palindrome Numbers: 1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 111
Approach-2: By Using User Defined Method
In this approach, mattrix 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 it with Palindrome Number.
Example
import java.util.Scanner; public class Main { // main method public static void main(String[] args) { // taking the size of the matrix Scanner scanner = new Scanner(System.in); System.out.print("Enter the number of rows: "); int rows = scanner.nextInt(); System.out.print("Enter the number of columns: "); int columns = scanner.nextInt(); // calling user defined method func(rows, columns); } // user defined method public static void func(int rows, int columns) { // create a square matrix int[][] matrix = new int[rows][columns]; // Fill the matrix with palindrome numbers fillMatrixWithPalindromes(matrix); // Print the matrix System.out.println("Matrix with Palindrome Numbers:"); printMatrix(matrix); } // Function to create a matrix and fill it with Palindrome numbers public static void fillMatrixWithPalindromes(int[][] matrix) { int num = 1; for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { // Find the next palindrome number while (!isPalindrome(num)) { num++; } // Fill the matrix with the palindrome number matrix[i][j] = num; num++; } } } // Function to check if a number is an Palindrome number or not public static boolean isPalindrome(int number) { String str = String.valueOf(number); int i = 0; int j = str.length() - 1; while (i < j) { if (str.charAt(i) != str.charAt(j)) { return false; } i++; j--; } return true; } // Function to display the matrix public static void printMatrix(int[][] matrix) { for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); } } }
Output
Enter the number of rows: 5 Enter the number of columns: 5 Matrix with Palindrome Numbers: 1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 111 121 131 141 151 161
In this article, we explored how to create a matrix and fill that with palindrome numbers by using Java programming language.