简体   繁体   中英

how do i reverse a number which starts with zero in cpp?

how do i reverse a number which starts with zero in c++? like 000021 to 120000 reverse a number which starts with zero


#include <iostream>
using namespace std;
int main()
{
    int T;
    cin >> T;
    while (T--) {
        int n, l;
        cin >> n;
        while (n != 0) {
            l = n % 10;
            cout << l;
            n /= 10;
            if (n != 0)
            {
                cout << " ";
            }
        }
        cout << endl;
    }
    return 0;
}

you need to take input number as string.

  • reverse the input string.
   string s = "00012";
   string str="";
       for(int i=s.size()-1;i>=0;i--){
             str+=s[i];
       }
   cout<<stoi(str);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM