Click here to Skip to main content
15,356,641 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:

i have below code


while (fiveDigNo!= 0)
   reverse = reverse * 10;
   reverse = reverse + fiveDigNo% 10;
   fiveDigNo= fiveDigNo/ 10;

I want to get reverse like 06671. But i am getting 6671.

What I have tried:

Reverse Function .
Updated 16-Nov-21 19:19pm

This is the same. 06671 = 6671. Isn't it obvious?

If you want to obtain "06671", it it not about your number, this is about the way you present the number as a string:
Int32.ToString Method (System)[^],
Int32.ToString Method (String) (System)[^],
Standard Numeric Format Strings[^],
Custom Numeric Format Strings[^].

Strictly speaking, the problem "reverse a number" wasn't correctly defined. For example, you would get different result if you "reverse" the number's octal, hexadecimal digits, and so on. So, you could, alternatively, simply get a string first, via ToString or in some other way, and then reverse that string.

CPallini 2-May-16 9:28am
Thank you, Carlo.
George Jonsson 2-May-16 10:03am
Definitely a 5.
Don't know how many "formatting issues" for either numbers or dates I have seen lately, because people think data is stored in memory the same way as it is shown on screen.
Thank you, George. Worse, many behave as if the only kind of data were strings... Wasted...
George Jonsson 2-May-16 10:18am
True, especially when dates are stored in a database column as a string in some odd format, and then problems occur when the date is to be manipulated in some way.
ridoy 2-May-16 13:22pm
exactly, a 5.
Thank you.
'06671' and '6671' are two different string representations of the same integer number. That is, if you really need the leading 0 in the output then you have to change your integer variable into a string one, sooner or later.
I guess, we answered at the same time... 5ed.
CPallini 2-May-16 9:29am
You know, great minds... :-)
In our culture, the proverb is different, it's something like
"Fools' thoughts match..."
CPallini 2-May-16 11:45am
I guess your culture is right.
ridoy 2-May-16 13:23pm
a 5 too, :)
CPallini 2-May-16 15:34pm
Thank you.
The numbers 06671 and 6671 are numerical identical. If you want a result like "06671" (a string representation) you have to use a string and reverse that:
string numString = fiveDigNo.ToString();
char[] charArray = numString.ToCharArray();
string revString = new string(charArray);
// If you need the numeric value too:
int revNumber = Convert.ToInt32(revString);
nikhil2015 3-May-16 1:04am
string revString = new string(Array); showing error as "Array is a type,which is not valid in the given context"

int revNumber = revString.ToInt32(); as "string doesnot contain a definition for ToInt32" and no extension method 'ToInt32' accepting a first argument of type string could be found
Jochen Arndt 3-May-16 2:23am
I had just typed in the answer and made two mistakes:
Array should be charArray
and it must be Convert.ToInt32(revString)
I will update my answer.
nikhil2015 4-May-16 1:43am
string myString = fiveAddNumber.ToString();
char[] arrayR = myString.ToCharArray();

revString = new string(arrayR);

I got the solution from above code

Thanks Jochen Arndt
I want to get reverse like 06671. But i am getting 6671.
The result is perfectly normal. Because 06671 and 6671 are the same value.
In order to handle a leading zero, the only way is to use a string instead of a numeric value. reversing the string "17660" will give you "06671".

Otherwise, you need to give us more information about the usage of this reverse.
"I want to get reverse like 06671. But i am getting 6671."

That's the same right?

With the reverse function, you might have got a string. Convert 06671 to numeric and you will have the results.
#include <string>
using namespace std;

int reverseDigits(int num)
string strin=to_string(num); //covert integer to char to print zero.
reverse(strin.begin(), strin.end());
cout<<"Reverse of num entered: ";
for(char p:strin){

="" return="" 0;
="" }

int="" main(){
="" int="" num;
="" cout<<"enter="" a="" number="" :="" ";
="" cin="">>num;
Richard Deeming 17-Nov-21 6:07am
Your unformatted, unexplained code-dump is not a "solution" to this already-solved question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900