-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimeMatrixFilling.java
More file actions
52 lines (49 loc) · 1.4 KB
/
Copy pathPrimeMatrixFilling.java
File metadata and controls
52 lines (49 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package basic;
import java.util.Scanner;
class PrimeMatrixFilling {
public static void main(String[] args) {
Scanner ob = new Scanner(System.in);
System.out.println("No. of rows");
int m = ob.nextInt();
System.out.println("No. of columns");
int n = ob.nextInt();
int prime[][] = new int[m][n];
int temp = 2, i = 0, j = 0;
for (i = 0; i < m; i++) //Filling Matrix with prime no.
{
for (j = 0; j < n; j++) {
if (checkPrime(temp) == 1) {
prime[i][j] = temp;
} else {
while (checkPrime(temp) != 1) {
temp++;
}
prime[i][j] = temp;
}
temp++;
}
}
System.out.println("Matrix filled with first " + m * n + " prime numbers");
for (i = 0; i < m; i++) //Printing Matrix
{
for (j = 0; j < n; j++) {
System.out.print(prime[i][j] + "\t");
}
System.out.println("");
}
}
public static int checkPrime(int x) //Method to check Prime no.
{
int c = 0;
for (int i = 2; i < x; i++) {
if (x % i == 0) {
c++;
}
}
if (c == 0) {
return 1;
} else {
return 0;
}
}
}