-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrimeIndexMatrix.java
40 lines (33 loc) · 1000 Bytes
/
PrimeIndexMatrix.java
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
/******************************************************************************
Write a Java program to create a two-dimensional array (m x m) A[][] such
that A[i][j] is false if i and j are prime otherwise A[i][j] becomes true
Input:
3 3
Output:
true true true
true true true
true true false
*******************************************************************************/
import java.util.*;
public class Main
{
public static void notPrimeIndex(int n,int m){
for(int i=0;i<n;i++){
for(int j=0;j<m;j++)
System.out.print((!isprime(i) || !isprime(j))+" ");
System.out.println();
}
}
public static boolean isprime(int n){
if(n<2) return false;
else if(n==2) return true;
for(int i=2;i<n/2;i++)
if(n%i==0) return false;
return true;
}
public static void main(String[] args) {
Scanner x=new Scanner(System.in);
int a=x.nextInt(),b=x.nextInt();
notPrimeIndex(a,b);
}
}