REVERSE THE NUMBER! With Reverse

Problem
I’ve taken the number as string
then i have reversed it
removed the zeros if any in starting
then printed the value left
What is my Mistake

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int t,temp=0;
    string n;
    cin>>t;
    ++t;
    while(--t)
    {   cin>>n;
        reverse(n.begin(),n.end());
        int sz = n.size();
//vector<int> v;
        while(n[temp]=='0'){
            temp++;
        }
        n.erase(0,temp);


        for(char k:n){
          //  cout<<k<<endl;

          cout<<k;
        }
        cout<<"\n";


    }





}

try this test cases:

2300
98088 // this test case outputs 89 which is wrong (as it uses value of temp from the previous operation i.e. from first test case)

30021
3 … same goes here

So, update temp variable to 0 before taking every new string input

temp=0;
cin>>n;
reverse...

Interesting
Thanks

Do you have any other idea rather than this?

        int n;
	    cin>>n;
	    int rev=0;
	    while(n>0){
            int rem= n%10;        // taking remainder
	        rev= rev*10 + rem ;      // adding that remainder to rev
	        n/=10;
	    }
	    cout<<rev<<endl;
1 Like

Thanks