Finding CB numbers

import java.util.*;
public class Main {

public static boolean cbnum(long n) {

	if (n == 2 || n == 3 || n == 5 || n == 7 || n == 11 || n == 13 || n == 17 || n == 19 || n == 23 || n == 29) {
		return true;
	}
	if (n > 29 && (n % 2 != 0 || n % 3 != 0 || n % 5 != 0 || n % 7 != 0 || n % 11 != 0 || n % 13 != 0 || n % 17 != 0
			|| n % 19 != 0 || n % 23 != 0 || n % 29 != 0)) {
		return true;
	}
	return false;
}

public static void main(String args[]) {

	Scanner scn = new Scanner(System.in);
	int n = scn.nextInt();
	scn.nextLine();
	String s = scn.nextLine();
	int start = 0, end = 1, count = 0;
	while (start < s.length()) {
		while (end <= s.length()) {
			long val = Long.parseLong(s.substring(start, end));
			if (cbnum(val)) {
				count++;
				start = end;
				break;

			} else {
				end++;
			}
		}
		start++;
		end = start + 1;
	}
	System.out.println(count);
}

}
some of the testcases going wrong plz correct the code where i went wrong

@karthik1989photos,
Your code is partially correct. So just need to add a condition to check if that index has been already included or not as mentioned in the question.

Suggested Approach:

  1. check for all sub-strings of the given string of digits, starting from the all strings of length 1 and then gradually checking for the strings of increasing size:

1.1. check if the sub-string is CB number: for this, create a function
1.1.1. if sub-string is 1 or 0 return false.
1.1.2. if sub-string is any of the {2,3,5,7,11,13,17,19,23,29}, then return true.
1.1.3. if sub-string is divisible by any of the {2,3,5,7,11,13,17,19,23,29}, then return false.
1.1.4. return false

1.2. now if it is a substring:
1.2.1. call a function that marks the index as visited for indexes which are part of that CB number.

1.3. increment the count for CB number.

  1. Print the count

You need to add 1.2 part.

Also for input:
8
88888888
Your code is giving output as 3 and the correct output should be 0. So please check that as well.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.