How do I remove the first number from an integer?

后端 未结 3 928
悲&欢浪女
悲&欢浪女 2021-01-19 05:13

I need to intake a number like: 200939915

After doing this, which I know how, I need to remove the first number so it becomes: 00939915

What is the best way

相关标签:
3条回答
  • 2021-01-19 05:58
    char *c = "200939915";
    char *d = c + 1;
    
    0 讨论(0)
  • 2021-01-19 05:59

    I will probably attract the downvoters but here is what I would do:

    #include <iostream>
    #include <math.h>
    
    int main()
    {
        int number;
        std::cin >> number;
    
        int temp = number;
        int digits = 0;
        int lastnumber = 0;
        while(temp!=0)
        {
            digits++;
            lastnumber = temp % 10;
            temp = temp/10;
        }
    
        number =  number % (lastnumber * (int)pow(10,digits-1));
        std::cout << number << std::endl;
        return 0;
    }
    

    obviously since you want it in c change std::cin to scanf (or whatever) and std::cout to printf (or whatever). Keep in mind though that if you want the two 00 to remain in the left side of the number, Kane's answer is what you should do. Cheers

    0 讨论(0)
  • 2021-01-19 06:14

    An alternative method, although I like Matt Kane's best:

    unsigned long n = 42424242;
    n = n % (unsigned long)pow(10.0, (double)floor(log10(n)));
    // n = 2424242;
    
    0 讨论(0)
提交回复
热议问题