Member Avatar for Member #13457

i have done a simple program, which i asked..(don't know how many days ago.)

but i find that there is a problem..

if i give an input (for example) 1234

it shall reverse back 4321 (it does)

when i key in 100, by right it should return 001, but it returned as 1.

my question is how to make that two zeros in front visible. (is there any mistake(s) for my "this" program??)

#include <iostream>
using namespace std;

long reverse();

int main()
{ long a;
a = reverse();
cout << "The reversed number is " << a << endl;

return 0;
}

long reverse()
{
long x, y, z = 0;

cout << "Enter an integer to be reversed: ";
cin >> x;
while (x > 0){
y = x%10;
x = x/10;
z = 10 * z + y;
}
return z;
}

thank you....

Dani AI

Generated

When the reversed value is stored in an integer type the leading zeros vanish: numeric types represent quantity, not textual formatting. Reversing 100 with arithmetic produces the number 1, so printing it prints "1". That is the root cause of the behavior reported in the original post.

Several replies already sketched the main options. mentioned formatting or working with text, and pointed toward string input, showed a digit-array approach, and demonstrated printing digits directly. For reliably preserving any leading zeros the most straightforward and robust approach is to treat the input as text and reverse the characters — this preserves every digit exactly as entered.

A minimal, practical C++ example (reads the token, preserves leading zeros, handles an optional minus sign):

#include <iostream>
#include <string>
#include <algorithm>

int main() {
    std::string s;
    if (!(std::cin >> s)) return 0;
    bool neg = !s.empty() && s[0] == '-';
    std::string t = neg ? s.substr(1) : s;
    std::reverse(t.begin(), t.end());
    if (neg) std::cout << '-' << t << '\n';
    else     std::cout << t << '\n';
    return 0;
}

Troubleshooting notes: if further numeric computation is required, keep both the original string (for formatting/width) and a parsed integer; use <iomanip> (std::setw + std::setfill('0')) to print a numeric value padded to a stored width. Guard against non-digit input, very long numbers (overflow if parsed), and whitespace handling (use std::getline when full-line input is needed). This keeps output faithful to the original entry while allowing numeric work when necessary.

Recommended Answers

All 9 Replies

The trouble here is you are using integers to reverse the number. That works for most things, but the number '1' could be '01' or '001' or '0001' and so on and you use a generic integer printing routine.

Two easy solutions I can think of here; one is to return how many digits the answer should be and make a string out of the result with leading zeroes (using sprintf(), say). the other is to do the reversal as a string of chars.

Both will take a small investment of time. The string reversal favors using the reverse() algorithm, whereas the sprintf is more of a clever formatting trick.

You should declear the number as a string or to convert it as a string. In your way it treat it as an integer and takes off the zeros of the front which don't mean anything for the compiler.

hi kho,
here you take input as a integer.The 001,00001,01,1 is same thing to the cout.
If you want to see output as 001 then you take input in a string.

thanking you.....
Rishi

Two easy solutions I can think of here; one is to return how many digits the answer should be and make a string out of the result with leading zeroes (using sprintf(), say). the other is to do the reversal as a string of chars.

You do have to use arrays in the method I know, but int arrays, not strings, which I think are easier to manipulate.

#include <iostream.h>
#include <conio.h>

int main()
{
 int a[5]={0,0}; //5 because max value of int is 32767
 int no, size=0;
 cin>>no;
 int r, t, i;
 t=no;
 while (no)  //for claculating the no of digits
 {
  r=no%10;
  no/=10;
  size++;
 }
 while (t) //storing digits in the array
 {
  r=t%10;
  t/=10;
  a[i]=r;
  i++;
 }
 for (i=0; i<size; i++)  //display
 cout<<a[i];
 getch();
 return 0;
}

I don't under stand the part with the remainder. What does this part do?

r=no%10;

r = no % 100;
==================
no = 120;
120 % 100 = 20;
130 % 100 = 30;
100 % 100 = 0;

I dont konw how to describle it in Eglish.

I don't under stand the part with the remainder. What does this part do?

The remainder gives you the individual digit. After the remainder is taken, the no. is divided by 10. Since in int no decimal pts are allowed, the original no. has one digit less.
Then the remainder is taken again and the next digit can be stored

This program gets very easy with the use if recursive function if the job is only to print the number in the reverse order.

#include <iostream>
 
 #include <conio>

 void rev(int); 

 void main()
 {
        int a;

       cin >> a;
     
       rev(a);

      getch();
 }

 void rev(int a)
 {
        if(a)
        {
               cout << a%10;
               rev(a/10);
         }
 }

>This program gets very easy with the use if recursive
>function if the job is only to print the number in the reverse order.
So, was it worth resurrecting a three year old thread to post some bad code?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.