Results 1 to 7 of 7

Thread: reverse number

  1. #1
    Junior Member
    Join Date
    Jul 2007
    Answers
    14

    reverse number

    hi friendz,

    how can i display the given number in reverse order,whatever may b the data type without using character array?


  2. #2
    Moderator
    Join Date
    Jun 2007
    Answers
    2,074

    Re: reverse number

    try to use instring and substring ,length or the corresponding C/ C++ function in a simple for loop.


  3. #3
    Junior Member
    Join Date
    Jul 2007
    Answers
    14

    Re: reverse number

    can u plz explain me with example..


  4. #4
    Moderator
    Join Date
    Jun 2007
    Answers
    2,074

    Re: reverse number

    first find out the length of the string.
    then run the for loop decrement by -1.
    inside the loop findout the last character of the string and store to another variable .Continue the process till you have read all the characters of the string and concatinate to the variable which is storing the last character.


  5. #5
    Expert Member
    Join Date
    Jun 2006
    Answers
    410

    Re: reverse number

    let me give you the pseudo code

    actual_number number;
    rev_number number;

    rev_number =0;
    while actual_number >0 loop

    rev_number = rev_number * 10 + (actual_number mod 10);
    actual_number = actual_number /10;
    end loop;


    How the above code works,

    let us assume that the actual number is 124

    and rev_number = 0 before it starts executing the loop,

    while 124 >0 true =>
    rev_number = rev_number * 10 + (actual_number mod 10)
    = 0 *10 + 4 =4
    actual_number = 124/10 =12

    while 12 >0 true =>

    rev_number = rev_number * 10 + (actual_number mod 10)
    = 4 *10 + 2 =42
    actual_number = 12/10 =1


    while 1 >0 true =>

    rev_number = rev_number * 10 + (actual_number mod 10)
    = 42 *10 + 1 =421
    actual_number = 1/10 =0

    while 1 >0 false => come out of the loop


    Note that I have give you only the logic not the code as i don't remember the syntax of C.

    Hope this helps you...


  6. #6
    Moderator
    Join Date
    Jun 2007
    Answers
    2,074

    Re: reverse number

    The code logic will work only for number datatypes ,but if you use the corresponding c/c++ functions of length,substring,instring you can also reverse strings.


  7. #7
    Junior Member
    Join Date
    Jul 2007
    Answers
    14

    Re: reverse number

    ok.. thanks to all...


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact