c++logic

reverse the position of integer digits?


i have to reverse the position of integer like this

input = 12345

output = 54321

i made this but it gives wrong output e.g 5432

#include <iostream>
using namespace std;

int main(){
 int num,i=10;   
 cin>>num;   

 do{
    cout<< (num%i)/ (i/10);
    i *=10;
   }while(num/i!=0);

 return 0;
}

Solution

  • Your loop terminates too early. Change

    }while(num/i!=0);
    

    to

    }while((num*10)/i!=0);
    

    to get one more iteration, and your code will work.