- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Create a Matrix and Fill it With Prime Numbers
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 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. In simpler terms, a prime number is a number that is only divisible by 1 and itself, without any remainder.
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, making it a prime number.
Let's start!
To show you some instances
Instance-1
Suppose we have 3*3 matrix i.e. 3 rows and 3 columns.
After performing the operation on matrix, the result will be:
Enter the number of rows: 3
Enter the number of columns: 3
The matrix filled with prime numbers is:
2 3 5 7 11 13 17 19 23
Instance-2.
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 number of rows: 4
Enter the number of columns: 4
The matrix filled with prime numbers is:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53
Algorithm
Step-1: Take the dimension of the matrix.
Step-2: Create matrix for prime numbers.
Step-3: Check if number is Prime number or not and insert it into 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 Prime Numbers.
Example
import java.util.Scanner; public class Main { // main method public static void main(String[] args) { // taking the dimension 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(); // 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; } }
Output
Enter the number of rows: 3 Enter the number of columns: 3 The matrix filled with prime numbers is: 2 3 5 7 11 13 17 19 23
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 it with Prime Numbers.
Example
import java.util.Scanner; public class Main { // main method public static void main(String[] args) { // calling user defined method func(); } // user defined method public static void func() { // taking the dimension 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(); // 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; } }
Output
Enter the number of rows: 4 Enter the number of columns: 4 The matrix filled with prime numbers is: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53
In this article, we explored how to create a matrix and fill that with prime numbers by using Java programming language.