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

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,152 questions

40,706 answers

573 users

How to remove the first digit from a number in C++

Booking.com | Official site | The best hotels, flights, car rentals & accommodations


85 views
asked Jan 15, 2021 by avibootz
edited Jan 15, 2021 by avibootz

3 Answers

0 votes
#include <iostream> 
 
unsigned int remove_first_digit(unsigned int n) {
    int divisor = 1 ;
     
    for (int i = n ; i > 9 ; i /= 10 ) {
        divisor *= 10;
    }
     
    return n % divisor;
}
 
int main() {  
    unsigned int n = 8405796; 
     
    std::cout << n << "\n";  
        
    n = remove_first_digit(n);
          
    std::cout << n;  
}  
    
    
    
    
/*
run:
    
8405796
405796
    
*/

 





answered Jan 15, 2021 by avibootz
edited Jan 12 by avibootz
0 votes
#include <iostream> 
#include <sstream> 

unsigned int remove_the_first_digit(unsigned int n) {
    std::cout << n << "\n";  
        
    std::stringstream ss;  
    ss << n;  
       
    std::string s;  
    ss >> s;  
       
    s.erase(s.begin()); 
       
    std::stringstream().swap(ss);
    ss << s;
       
    ss >> n;
    
    return n;
}
 
int main() {  
    unsigned int n = 8405796; 
     
    n = remove_the_first_digit(n);
         
    std::cout << n;  
}  
    
    
    
    
/*
run:
    
8405796
405796
    
*/

 





answered Jan 15, 2021 by avibootz
edited Jan 12 by avibootz
0 votes
#include <iostream>
#include <cmath>
 
unsigned int remove_first_digit(unsigned int num) {
    int total = (int)log10(num); // total - 1 = 6
      
    int first_digit = num / pow(10, (total));
      
    return num - first_digit * pow(10, (total));
}
  
int main(void) {
    int n = 8405796;
     
    n = remove_first_digit(n);
      
    std::cout << n;
}
  
  
  
    
/*
run:
    
405796
   
*/

 





answered Jan 12 by avibootz

Related questions

2 answers 172 views
1 answer 17 views
1 answer 18 views
1 answer 71 views
...