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

i have below code

C++
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 16-Nov-21 19:19pm
v2

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
 
Share this answer
 
v2
Comments
CPallini 2-May-16 9:28am    
5.
Sergey Alexandrovich Kryukov 2-May-16 9:31am    
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.
Sergey Alexandrovich Kryukov 2-May-16 10:11am    
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.
'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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-May-16 9:21am    
I guess, we answered at the same time... 5ed.
—SA
CPallini 2-May-16 9:29am    
You know, great minds... :-)
Sergey Alexandrovich Kryukov 2-May-16 9:33am    
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.
Sergey Alexandrovich Kryukov 2-May-16 12:28pm    
:-)
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:
C#
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);
 
Share this answer
 
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
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.
 
Share this answer
 
C#
"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.
 
Share this answer
 
#include<iostream>
#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());
num=stoi(strin);
cout<<"Reverse of num entered: ";
for(char p:strin){
cout<<p;
}

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

int="" main(){
="" int="" num;
="" cout<<"enter="" a="" number="" :="" ";
="" cin="">>num;
reverseDigits(num);
cout<
 
Share this answer
 
Comments
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