MELWIN
MELWIN

Reputation: 1103

Find the reverse of a number (ex : 2500 reverse 0025) without the help of string or character

Is there any technique for finding the reverse when there are zeros at the end.

While following the algorithm of %10 technique the result is 52. And the 0's are missing.

I have got the reverse by just printing the reminders (with 0's). But I am not satisfied as I wish to display the answer as the value in a variable.

Kindly tell me is there any technique to store a value 005 to a variable and also to display 005 (please don't use String or Character or array).

Upvotes: 1

Views: 3344

Answers (4)

Ziggy
Ziggy

Reputation: 22365

I think the accepted answer is a good one, in that it both refutes the parts of the question that are wrong and also offers a solution that will work. However, the code there is all Java, and it doesn't expose the prettiest API. Here's a C++ version that based on the code from the accepted answer.

(Ha ha for all my talk, my answer didn't reverse the string! Best day ever!)

After going back to school and getting a degree, I came up with this answer: it has the makes the somewhat dubious claim of "not using strings" or converting any values to string. Can't avoid characters, of course, since we are printing the value in the end.

#include <ostream>
#include <iostream>

class ReverseLong {
  public:

    ReverseLong(long value) {
      long num = value;
      bool leading = true;
      this->value = 0;
      this->leading_zeros = 0;

      while (num != 0) {
        int digit = num % 10;
        num = num / 10;

        if (leading && digit == 0) {
          this->leading_zeros += 1;
        } else {
          this->value = this->value * 10 + digit;
          leading = false;
        }
      }
    };

    friend std::ostream & operator<<(std::ostream& out, ReverseLong const & r);

  private:
    long value;
    int leading_zeros;

};

std::ostream & operator<<(std::ostream& out, ReverseLong const & r) {

  for (int i =0; i < r.leading_zeros; i++) {
    out << 0;
  }

  out << r.value;

  return out;
};

int main () {
  ReverseLong f = ReverseLong(2500); // also works with numbers like "0"!

  std::cout << f << std::endl; / prints 0052
};

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533500

Numbers are stored as binary 0 and 1 and so they always have leading 0's which are chopped off. e.g. a 64-bit integer has 64-bit bits, always and when it is printed these leading 0's are dropped.

You need to know how many leading zeros you want to keep and only use that many when you print. i.e. you can record how many leading zeros there were in a normal number without encoding it e.g. by adding a 1 at the start. i.e. 0052 is recorded as 10052 and you skip the first digit when you print.

If you need to store a single value you can do the following. I use do/while so that 0 becomes 10 and is printed as 0. The number 0 is the one place where not all leading zeros are dropped (as it would be empty otherwise)


This appears to be the solution you want and it should be basically the same in C or C++

static long reverse(long num) {
    long rev = 1; // the 1 marks the start of the number.
    do {
        rev = rev * 10 + num % 10;
        num /= 10;
    } while(num != 0);
    return rev;
}

// make the reversed number printable.
static String toStringReversed(long num) {
    return Long.toString(num).substring(1);
}

long l = reverse(2500); // l = 10052

An alternative is to print the digits as you go and thus not need to store it.

e.g.

static void printReverse(long l) {
    do {
        System.out.print(l % 10);
        l /= 10;
    } while(l != 0);
}

or you can have the input record the number of digits.

static void printReverse(long l, int digits) {
    for(int i = 0; i < digits; i++) {
        System.out.print(l % 10);
        l /= 10;
    }
}

// prints leading zero backwards as well
printReverse(2500, 6); // original number is 002500

prints

005200

Upvotes: 5

Buddha
Buddha

Reputation: 4476

You can't store them in a simple integer variable because in binary format

00101 is same as 000101 which is same as 101 which only results into 5. The convertion between a decimal number and binary numbers don't consider leading zeroes so it is not possible to store leading zeroes with the same integer variable.

You can print it but you can't store the leading zeroes unless you use array of ints...

int num = 500;

while(num > 0)
{
    System.out.print(num%10);
    num = num/10;
}

Alternatively you can store the count of leading zeroes as a separate entity and combine them when ever you need to use. As shown below.

int num = 12030;
boolean leading=true;
int leadingCounter = 0;
int rev = 0;
while(num > 0)
{
    int r = num%10;
    if(r == 0 && leading == true)
        leadingCounter++;
    else
        leading = false;
    rev = rev*10 + r;
    num = num/10;
}
for(int i = 1; i <= leadingCounter ; i++)
    System.out.print("0");
System.out.println(rev);

Upvotes: 0

unwind
unwind

Reputation: 399813

You cannot represent an integer with leading zeros as a single integer variable, that information is simply not part of the way bits are allocated in an integer. You must use something larger, i.e. a string or an array of individual (small integer) digits.

Upvotes: 5

Related Questions