bbahd
bbahd

Reputation: 49

For reversing a number in C++ which ends with zeros

I want to write a program for reversing a number. For reversing a number like 2300 to 32 so that the ending zeros are not printed, I found this method:

#include<iostream>
using namespace std;

int main()
{
       int l;
       cin>>l;
       bool leading = true;
       while (l>0)
       {
           if ((l%10==0)&& (leading==true))
           {
               l /= 10;
               leading = false;    // prints 032 as output
               continue;
           }
        //   leading = false;   this prints correct 32
           cout<<l%10;
           l /= 10;
       }
       
       return 0;
}

The instruction of assigning boolean leading false inside the if statement is not giving a valid answer, but I suppose assigning it false should give 32 as output whether we give it outside or inside if statement as its purpose is just to make it false once you get the last digit to be a non zero. Please tell the reason of difference in outputs.

Upvotes: 3

Views: 1510

Answers (4)

Naveen Kedilaya
Naveen Kedilaya

Reputation: 153

The reason for the difference in output is because when you make leading = false inside the if statement, you are making it false right after encountering the first zero. When you encounter the remaining zeroes, leading will be false and you will be printing it.

When you make leading = false outside the if statement, you are basically waiting till you encounter all zeroes before making it false.

If you are looking to reverse a number, this is the well known logic to do so:

int reverse(int n)
{
    int r;          //remainder
    int rev = 0;    //reversed number
    
    while(n != 0) 
    {
        r = n%10;
        rev = rev*10 + r;
        n /= 10;
    }
    return rev;
}

EDIT:

The above code snippet is fine if you just want to understand the logic to reverse a number. But if you want to implement the logic anywhere you have to make sure you handle integer overflow problems (the reversed number could be too big to be stored in an integer!!). The following code will take care of integer overflow:

int reverse(int n)
{
    int r;          //remainder
    int rev = 0;    //reversed number
    
    while(n != 0) 
    {
        r = n%10;
        if(INT_MAX/10 < rev)
        {
            cout << "Reversed number too big for an int.";
            break;
        }
        else if(INT_MAX-r < rev*10)
        {
            cout << "Reversed number too big for an int.";
            break;
        }
        rev = rev*10 + r;
        n /= 10;
    }
    if(n != 0)
    {
        //could not reverse number
        //take appropriate action
    }
    return rev;
}

Upvotes: 4

molbdnilo
molbdnilo

Reputation: 66371

First, rewrite without continue to make the flow clearer,

while (l > 0)
{
    if ((l % 10 == 0) && (leading == true))
    {
        l /= 10;
        leading = false;    // prints 032 as output
    }
    else
    {
        //   leading = false;   this prints correct 32
        cout << l % 10;
        l /= 10;
    }
}

and move the division common to both branches out of the conditional,

while (l > 0)
{
    if ((l % 10 == 0) && (leading == true))
    {
        leading = false;    // prints 032 as output
    }
    else
    {
        //   leading = false;   this prints correct 32
        cout << l % 10;
    }
    l /= 10;
}

and now you see that the only difference between the two is the condition under which the assignment leading = false happens.

The correct version says, "If this digit is non-zero or a non-leading zero, remember that the next digit is not a leading zero, and print this digit. Then divide."
Your broken version says, "If this is a leading zero, the next digit is not a leading zero." which is pretty obviously not the case.

Upvotes: 2

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122460

When reversing digits of numbers or generally when working with digits and the actual value does not matter then treating the number as an array of digits is simpler than working with the whole int. How to treat a number as an array of digits conveniently? std::string:

#include <iostream>
#include <string>
#include <sstream>

int reverse_number(int x) {
    std::string xs = std::to_string(x);
    std::string revx{ xs.rbegin(),xs.rend()};
    std::stringstream ss{revx};
    int result;
    ss >> result;
    return result;
}

int main() {
    std::cout << reverse_number(123) << "\n";
    std::cout << reverse_number(1230) << "\n";
}

std::to_string converts the int to a std::string. std::string revx{ xs.rbegin(),xs.rend()}; constructs the reversed string by using reverse iterators, and eventually a stringstream can be used to parse the number. Output of the above is:

321
321

Upvotes: 0

Manishyadav
Manishyadav

Reputation: 1726

Just try this ,

#include <iostream>
using namespace std;

int main() {
    int n, reversedNumber = 0, remainder;

    cout << "Enter an integer: ";
    cin >> n;

    while(n != 0) {
        remainder = n%10;
        reversedNumber = reversedNumber*10 + remainder;
        n /= 10;
    }

    cout << "Reversed Number = " << reversedNumber;

    return 0;
}

Working for me...

Upvotes: 0

Related Questions