Reverse of a number with leading zeroes

后端 未结 10 1600
庸人自扰
庸人自扰 2021-01-14 05:17

How do we reverse a number with leading zeroes in number ? For ex: If input is 004, output should be 400.

I wrote below program but it works only when n

10条回答
  •  独厮守ぢ
    2021-01-14 05:56

    A recursive approach, but easily converted to a loop...

    #include 
    
    int f(int value = 1)
    {
        char c;
        return (std::cin.get(c) && isdigit(c))
               ? (c - '0') * value + f(10 * value)
               : 0;
    }
    
    
    int main()
    {
        std::cout << f() << '\n';
    }
    

提交回复
热议问题