import java.io.*;
class Fill_Prime
{
boolean isPrime(int n) // Function for checking whether a number is prime or not
{
int c = 0;
for(int i = 1; i<=n; i++)
{
if(n%i == 0)
c++;
}
if(c == 2)
return true;
else
return false;
}
public static void main(String args[])throws IOException
{
Fill_Prime ob = new Fill_Prime();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the number of rows: ");
int r=Integer.parseInt(br.readLine());
System.out.print("Enter the number of columns: ");
int c=Integer.parseInt(br.readLine());
int A[][]=new int[r]1; // 2D array for storing 'r*c' prime numbers
int B[] = new int [r*c]; // 1D array for storing 'r*c' prime numbers
int i = 0, j;
int k = 1; // For generating natural numbers /* First saving the 'r*c' prime numbers into a 1D Array */
while(i < r*c)
{
if(ob.isPrime(k)==true)
{
B[i] = k;
i++;
}
k++;
}
/* Saving the 'r*c' prime numbers from 1D array into the 2D Array */
int x = 0;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
A[i][j] = B[x];
x++;
}
}
/* Printing the resultant 2D array */
System.out.println("The Filled Array is :");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
}
}
class Fill_Prime
{
boolean isPrime(int n) // Function for checking whether a number is prime or not
{
int c = 0;
for(int i = 1; i<=n; i++)
{
if(n%i == 0)
c++;
}
if(c == 2)
return true;
else
return false;
}
public static void main(String args[])throws IOException
{
Fill_Prime ob = new Fill_Prime();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the number of rows: ");
int r=Integer.parseInt(br.readLine());
System.out.print("Enter the number of columns: ");
int c=Integer.parseInt(br.readLine());
int A[][]=new int[r]1; // 2D array for storing 'r*c' prime numbers
int B[] = new int [r*c]; // 1D array for storing 'r*c' prime numbers
int i = 0, j;
int k = 1; // For generating natural numbers /* First saving the 'r*c' prime numbers into a 1D Array */
while(i < r*c)
{
if(ob.isPrime(k)==true)
{
B[i] = k;
i++;
}
k++;
}
/* Saving the 'r*c' prime numbers from 1D array into the 2D Array */
int x = 0;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
A[i][j] = B[x];
x++;
}
}
/* Printing the resultant 2D array */
System.out.println("The Filled Array is :");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
}
}