Monday, July 27, 2009

Complete the function .- Prints out the decimal digits of number in reverse order using c++?

#include %26lt;iostream%26gt;





using namespace std;





const int NUMBER_BASE = 10;





void reverseDigits(int number)


{





/* You are to write the code here to complete this function */





}





int main()


{


int n;


cout%26lt;%26lt;"Enter a 7 Digit number : ";


cin%26gt;%26gt;n;


reverseDigits(n);


return 0;


system("pause");


}

Complete the function .- Prints out the decimal digits of number in reverse order using c++?
It would be easier to store each digit in a different variable. Then use cout to print them in reverse order.





cout %26lt;%26lt; 7 %26lt;%26lt; 6 %26lt;%26lt; 5 %26lt;%26lt; 4 %26lt;%26lt; 3 %26lt;%26lt; 2 %26lt;%26lt; 1;
Reply:use the shift operator %26gt;%26gt; for this.


there probably is a one line but this will also work a binary reverse. not exactly what you want ...





int x = 0;


for (int i = 0;i%26lt;32) { // 32 size of int


x = x%26lt;%26lt;1;


x = x + n%2;


n%26gt;%26gt;1;


}





//


int x = 0;


for (int i =0;i%26lt;7;i++) {


x = x + n%10;


n = n/10;


x *= 10;


}








and ? does it work already ?


No comments:

Post a Comment