Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,844 questions

51,765 answers

573 users

How to check if number can be made prime by deleting a single digit in Java

1 Answer

0 votes
class NumberCanBeMadePrimeNyDeletingADigit_Java {
    public static int remove_the_N_digit(int num, int N) {
        String str = Integer.toString(num);
    
        str = str.substring(0, N) + str.substring(N + 1);
          
        return Integer.parseInt(str);
    }
     
    static boolean isPrime(int n) {
        if (n < 2 || (n % 2 == 0 && n != 2)) {
            return false;
        }
         
        int count = (int)Math.floor(Math.sqrt(n));
        for (int i = 3; i <= count; i += 2) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
        
    }
     
    public static void main(String[] args) {
        int n = 78919;
        int total_digits = (int)Math.log10(n) + 1;
         
        int i = 0;
        while (i < total_digits) {
            int tmp = remove_the_N_digit(n, i);
            if (isPrime(tmp)) {
                System.out.print("yes number = " + tmp);
                break;
            }
            i++;
        }
    }
}


 
/*
run:
      
yes number = 7919
   
*/

 



answered Jan 18, 2024 by avibootz
edited Sep 27, 2024 by avibootz

Related questions

...