
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 Prime Numbers in Java
As per the problem statement, we have to create an empty matrix and fill that matrix with the prime numbers starting from the smallest prime number i.e. 2. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
For instance, let's take the number 7. When we divide 7 by any number other than 1 and 7, we get a remainder. For example, dividing 7 by 2 gives a remainder of 1, and dividing 7 by 3 gives a remainder of 1 as well. Therefore, 7 has no divisors other than 1 and 7, which means it is a prime number.
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. They have several properties and operations associated with them, such as addition, subtraction, multiplication, transposition, and determinant calculation.
Input/Output Scenario
Suppose we have 3*3 matrix ?
number of rows = 3 number of columns = 3
The matrix filled with prime numbers is:
2 3 5 7 11 13 17 19 23
Using Static Initialization of Matrix Elements
In this approach, the matrix dimensions are initialized in the program. Then, we create a matrix. Before inserting any number, we check if the number is a prime number or not.
Example
In this example, we will see how to create a matrix and fill that with prime numbers by using Java programming language.
public class Main { // main method public static void main(String[] args) { // dimension of the matrix int rows = 3; int columns = 3; // creating matrix for prime numbers int[][] matrix = new int[rows][columns]; // Starting number to check for prime int number = 2; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { while (!isPrime(number)) { number++; } matrix[i][j] = number; number++; } } // Display the matrix System.out.println("The matrix filled with prime numbers is:"); for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); } } // Method to check if a number is prime private static boolean isPrime(int number) { if (number < 2) { return false; } for (int i = 2; i <= Math.sqrt(number); i++) { if (number % i == 0) { return false; } } return true; } }
When you run the above code, it will show the following result ?
The matrix filled with prime numbers is: 2 3 5 7 11 13 17 19 23
User Defined Method
In this approach, create a user-defined method and inside this method initialize the dimension of the matrix. Then call this method in the main() method to create and fill the matrix with prime numbers.
Example
The following example practically illustrates the above approach.
public class Main { // main method public static void main(String[] args) { // calling user defined method func(); } // user defined method public static void func(){ // dimension of the matrix int rows = 4; int columns = 4; // creating matrix for prime numbers int[][] matrix = new int[rows][columns]; // Starting number to check for primality int number = 2; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { while (!isPrime(number)) { number++; } matrix[i][j] = number; number++; } } // Display the matrix System.out.println("The matrix filled with prime numbers is:"); for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { System.out.print(matrix[i][j] + " "); } System.out.println(); } } // Method to check if a number is prime private static boolean isPrime(int number) { if (number < 2) { return false; } for (int i = 2; i <= Math.sqrt(number); i++) { if (number % i == 0) { return false; } } return true; } }
On executing, this code prints the below result ?
The matrix filled with prime numbers is: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53