It is to reverse the number i don't know why compiler throwing wrong answer?

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

int main() {
int n;
string s,rev;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>s;
reverse(s.begin(), s.end());
cout<<s<<endl;
}
return 0;
}

Your code print 0 before the integer. Example, suppose the number is 1200, it prints 0012, whereas it should be 12.

2 Likes

make array of strings to store the reversed strings and then print each string separately without any leading zeros . :blush:

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

int main() {
int n;
string s,rev;
cin>>n;
cin>>s;
reverse(s.begin(), s.end());
cout<<s<<endl;
return 0;
}

1 Like
#include <iostream>
using namespace std;

int main() {
    int n, reversedNumber = 0, remainder;

    cout << "Enter an integer: ";
    cin >> n;

    while(n != 0) {
        remainder = n%10;
        reversedNumber = reversedNumber*10 + remainder;
        n /= 10;
    }

    cout << "Reversed Number = " << reversedNumber;

    return 0;
}

// this can be a good method and this can also be used in various other programs