boolean array

Solutions on MaxInterview for boolean array by the best coders in the world

showing results for - "boolean array"
Tim
16 Jun 2018
1
2import java.util.Scanner;
3
4public class BooleanPrimes {
5    public static void main(String[] argh)
6    {
7        Scanner scanner = new Scanner(System.in);
8        System.out.println("Enter a number: ");
9        int size = scanner.nextInt();
10        boolean[] boolArray = new boolean[size];
11        generateBoolArray(boolArray,size);
12    }
13
14    public static boolean[] generateBoolArray(boolean[] boolArr, int size) // initializing boolean array with true values
15    {
16        for (int i = 2; i < size; ++i)
17        {
18            boolArr[i] = true;
19        }
20        return chickIfIndexisPrime(boolArr, boolArr.length,size);
21    }
22
23    public static boolean[] chickIfIndexisPrime(boolean[] arrIsPrime, int input,int size)
24    {
25        int start = 2;
26        while (start <= input)
27        {
28            int i = 2;
29            boolean isprime = true;
30            while(i < start )
31            {
32                if(start%i == 0) {
33                    isprime = false;
34                }
35                ++i;
36            }
37            if(isprime==true){
38              for(int j=4; j<arrIsPrime.length ;++j)
39              {
40                  if(j%start ==0)
41                  { j=j==start?++j:j;
42                      arrIsPrime[j]=false;
43                  }
44              }
45            }
46            ++start;
47
48        }
49        printArray(arrIsPrime,size);
50        return arrIsPrime;
51    }
52
53
54
55    public static void printArray(boolean[] arr ,int size){
56        System.out.println("The prime numbers from 2 till "+size);
57        int i=0,j = 0 ;
58        while(i<arr.length){
59            if(arr[i]==true){
60                System.out.print(i+",");
61            }
62            ++i;
63        }System.out.println();
64
65        while(j<arr.length){
66            System.out.print("{Index: "+j+" "+arr[j]+"} ");
67            ++j;
68        }
69
70    }
71
72}
similar questions
queries leading to this page
boolean array