Java Program to Create a Matrix and Fill it with Prime Numbers Last Updated : 13 Jan, 2021 Comments Improve Suggest changes Like Article Like Report Prime Numbers are the number which can be divisible by only 1 and itself, it cannot be divisible by any other number. The task here is to find the prime numbers and store them in the form of a matrix. Example: Prime numbers are: 2, 3, 5, 7 Output: 2 3 5 7 Prime numbers : 2, 3, 5, 7, 9, 11, 13, 17, 19 Output: 2 3 5 7 9 11 13 17 19 Approach: First, the class should be created and inside the class a method is created to check the prime number.This method will take the number as input and then check whether it is prime or not. If it is prime then it will return true and if not then return false.Now, the main method will be created, inside the main method the object of Matrix class is made so that the isPrime() method can be accessed from the main.The number of rows and number of columns will be taken from the user and then using this, the another array will be created which is of size rows * columns.The prime numbers will be stored in this array.Now, by iterating through result[] array, the prime numbers will gets stored into the Matrix[] array. After that the result will gets printed. Java // Java Program to Create a Matrix and Fill // it with Prime Numbers import java.util.Scanner; public class MatrixWithPrimeNumbers { // Function to check a number is prime or not boolean isPrime(int num) { int counter = 0; for (int i = 1; i <= num; i++) { if (num % i == 0) counter = counter + 1; } if (counter == 2) return true; else return false; } public static void main(String args[]) { // creating object of Matrix With Prime Numbers // class MatrixWithPrimeNumbers mwp = new MatrixWithPrimeNumbers(); // Enter the number of rows and column. int row = 4; int col = 4; // 2D array for storing prime numbers int Matrix[][] = new int[row][col]; // size of resultant matrix int res = row * col; // 1D array for storing prime numbers int Result[] = new int[res]; int i = 0, j; int k = 1; // Calling the method to check prime and // then result will be stored into the array while (i < res) { if (mwp.isPrime(k) == true) { Result[i] = k; i++; } k++; } // the result is now stored into the form // of matrix (in 2D array) int x = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { Matrix[i][j] = Result[x]; x++; } } // printing the result in 2D Array. System.out.println("The Resultant Matrix is : "); for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { System.out.print(Matrix[i][j] + " "); } System.out.println(); } } } OutputThe Resultant Matrix is : 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 Comment More infoAdvertise with us Next Article Java Program to Create a Matrix and Fill it with Prime Numbers S snigdha_yambadwar Follow Improve Article Tags : Java Java Programs Practice Tags : Java Similar Reads Java Program to Display All Prime Numbers from 1 to N For a given number N, the purpose is to find all the prime numbers from 1 to N.Examples: Input: N = 11Output: 2, 3, 5, 7, 11Input: N = 7Output: 2, 3, 5, 7 Approach 1:Firstly, consider the given number N as input.Then apply a for loop in order to iterate the numbers from 1 to N.At last, check if each 4 min read Java Program for Frequencies of even and odd numbers in a matrix Given a matrix of order m*n then the task is to find the frequency of even and odd numbers in matrix Examples: Input : m = 3, n = 3 { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } Output : Frequency of odd number = 5 Frequency of even number = 4 Input : m = 3, n = 3 { 10, 11, 12 }, { 13, 14, 15 }, { 16, 17, 1 3 min read Java Program to Print matrix in antispiral form Given a 2D array, the task is to print matrix in anti spiral form:Examples: Output: 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Input : arr[][4] = {1, 2, 3, 4 5, 6, 7, 8 9, 10, 11, 12 13, 14, 15, 16}; Output : 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1 Input :arr[][6] = {1, 2, 3, 4, 5, 6 7, 8, 9, 10, 11, 12 3 min read Java Program to Find the Determinant of a Matrix The determinant of a matrix is a special calculated value that can only be calculated if the matrix has same number of rows and columns (square matrix). It is helpful in determining the system of linear equations, image processing, and determining whether the matrix is singular or non-singular.In th 5 min read Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range A prime number is a whole number greater than 1, which is only divisible by 1 and itself. The first few prime numbers are 2 3 5 7 11 13 17 19 23. Given a range, L to R, the task is to generate all the prime numbers that exist in the Range. Examples Input: 1 10 Output 2 3 5 7 Input: 20 30 Output: 23 5 min read Like