The most efficient way to reverse a number

后端 未结 10 751

I am looking for an efficient algorithm to reverse a number, e.g.

Input: 3456789

Output: 9876543

In C++ there are p

10条回答
  •  伪装坚强ぢ
    2021-01-13 05:17

    This is the easiest one:

    #include
    using namespace std;
    
    int main()
    {
    int number, reversed=0;
    cout<<"Input a number to Reverse: ";
    cin>>number;
        while(number!=0)
      {
        reversed= reversed*10;
        reversed=reversed+number%10;
        number=number/10;
      }
    cout<<"Reversed number is: "<

提交回复
热议问题