Click here to Skip to main content
14,304,158 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,

i have below code

fiveDigNo=17660;

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 .
Posted
Updated 30-Jul-19 1:25am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

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.

—SA
   
v2
Comments
CPallini 2-May-16 9:28am
   
5.
   
Thank you, Carlo.
—SA
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...
—SA
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.
—SA
Rate this:
Please Sign up or sign in to vote.

Solution 2

'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.
   
Comments
   
I guess, we answered at the same time... 5ed.
—SA
CPallini 2-May-16 9:29am
   
You know, great minds... :-)
   
In our culture, the proverb is different, it's something like
"Fools' thoughts match..."
—SA
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.
Rate this:
Please Sign up or sign in to vote.

Solution 3

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();
Array.Reverse(charArray);
string revString = new string(charArray);
// If you need the numeric value too:
int revNumber = Convert.ToInt32(revString);
   
v2
Comments
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();
Array.Reverse(arrayR);

revString = new string(arrayR);

I got the solution from above code

Thanks Jochen Arndt
Rate this:
Please Sign up or sign in to vote.

Solution 4

Quote:
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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 6

"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.
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100