How to delete the first digit from a number in C

2 Answers

0 votes
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int n = 76581;
    char s[10];
 
    sprintf(s, "%d", n); 
    s[0] = '0';
    n = atoi(s);
    
    printf("%d\n", n);

    return 0;
}


/*
run:

6581

*/

 



answered Jun 3, 2020 by avibootz
edited Jun 4, 2020 by avibootz
0 votes
#include <stdio.h>
#include <math.h>

int main(void) {
    int n = 87315;
       
    printf("%i\n", (int)log10(n));
    printf("%i\n", (int)pow(10, (int)log10(n)));
         
    n = n % (int)pow(10, (int)log10(n));
    
    printf("%i\n", n);

    return 0;
}




/*
run:
  
4
10000
7315
  
*/

 



answered Jun 4, 2020 by avibootz

Related questions

2 answers 311 views
1 answer 169 views
1 answer 179 views
1 answer 158 views
2 answers 205 views
1 answer 172 views
4 answers 305 views
...