Reverse a number

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sonia.sardana
    New Member
    • Jul 2006
    • 95

    Reverse a number

    Que- If a five digit number is input through the keyboard, write a program to reverse the number.

    Code:
    void main()
    {
       int num=12345, res=0, rem;
       clrscr();
       while(num > 0)
       {
          rem=num% 10;
          res=res* 10 + rem;
          num=num/10;
       }
       printf("Reverse of a number=%d",res);
       getch();
    }
    According to me logic is rite, but the result coming out to be is not correct... Check it our,,, & reply
    Last edited by Ganon11; Mar 20 '08, 06:19 PM. Reason: Please use the [CODE] tags provided.
  • Sick0Fant
    New Member
    • Feb 2008
    • 121

    #2
    Originally posted by sonia.sardana
    Que- If a five digit number is input through the keyboard, write a program to reverse the number.

    void main()
    {
    int num=12345, res=0, rem;
    clrscr();
    while(num > 0)
    {
    rem=num% 10;
    res=res* 10 + rem;
    num=num/10;
    }
    printf("Reverse of a number=%d",res) ;
    getch();
    }

    According to me logic is rite, but the result coming out to be is not correct... Check it our,,, & reply
    It should (and does) work.

    Comment

    • sonia.sardana
      New Member
      • Jul 2006
      • 95

      #3
      If we enter the number upto four digit,answer is rite, Suppose I enter 1234 Answer is 4321.
      But If we enter five digit numb,suppose 12345, Answer is not coming 54321.
      If we declare num as long, even then the o/p is not coming out to be correct for 5 digit number.

      Comment

      • Sick0Fant
        New Member
        • Feb 2008
        • 121

        #4
        Far be it from me to doubt you, but I copied and pasted your code into an editor and compiled it, and it produced the desired output.

        Comment

        • sonia.sardana
          New Member
          • Jul 2006
          • 95

          #5
          Ok thx, but i face still d same probs.

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Exactly what output are you getting?

            e.g. You enter 12345.

            What is output?

            Comment

            • sonia.sardana
              New Member
              • Jul 2006
              • 95

              #7
              When I enter 12345, answer is -11215.
              When I enter the numb upto 4 digit, answer is coming correct.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by sonia.sardana
                When I enter 12345, answer is -11215.
                When I enter the numb upto 4 digit, answer is coming correct.
                The ints on your system are two bytes wide so they can only store numbers in
                the range [-2^15,2^15-1], e.g. the number 54321 - 2^15 == -11215, so your
                algorithm is correct, the ints are just to narrow (try four byte longs).

                kind regards,

                Jos

                Comment

                • sonia.sardana
                  New Member
                  • Jul 2006
                  • 95

                  #9
                  then wat to do to get the correct answer of Five digit number,,,,,,,Ev en if I declare the numb as long datatype....... ..even then the answer is not coming out to be correct

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by sonia.sardana
                    then wat to do to get the correct answer of Five digit number,,,,,,,Ev en if I declare the numb as long datatype....... ..even then the answer is not coming out to be correct
                    Make all your variable long ints and print them as such: %ld instead of %d.

                    kind regards,

                    Jos

                    Comment

                    • vicky08
                      New Member
                      • Mar 2008
                      • 2

                      #11
                      Dear Sonia.;
                      ur code is absolutely correct. firstly check ur compiler size n gv 3/4 digit num.
                      as it is working in my sys. i.e visual studio..
                      ck it out n info.

                      Regards
                      Vicky...

                      Comment

                      Working...