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.

40,011 questions

51,958 answers

573 users

How to remove the N digit from a number in Java

2 Answers

0 votes
public class MyClass {
    public static int reverse_number(int num) {
        int reminder = 0, reverse = 0;
        while (num != 0) {    
            reminder = num % 10;      
            reverse = reverse * 10 + reminder;    
            num /= 10;    
        }
        return reverse;
    } 
     
    public static int delete_digit(int num, int n_digit) { 
        int rev_new_num = 0; 
        int l = (int)Math.log10(num) + 1; 
            
        for (int i = 0; num != 0; i++) { 
            int digit = num % 10; 
            num = num / 10; 
         
            if (i != l - n_digit) {
                rev_new_num = (rev_new_num * 10) + digit; 
            }
        }
        return reverse_number(rev_new_num); 
    } 
      
    public static void main(String args[]) {
        int num = 37598; 
     
        System.out.println(delete_digit(num, 3));
        System.out.println(delete_digit(num, 1));
        System.out.println(delete_digit(num, 5));
    }
}
  
  
  
/*
run:
  
3798
7598
3759
  
*/

 



answered Apr 22, 2019 by avibootz
0 votes
public class MyClass {
    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);
    }
          
    public static void main(String args[]) {
        int num = 870615;
          
        num = remove_the_N_digit(num, 3);
            
        System.out.println(num);
    }
}
    
    
    
    
/*
run:
    
87015
    
*/

 



answered Apr 22, 2024 by avibootz

Related questions

1 answer 80 views
1 answer 121 views
1 answer 119 views
1 answer 139 views
1 answer 70 views
1 answer 92 views
...