Thread: Reverse of an integer

  1. #16
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    May be this is what you're looking for. Considering that if you give negative number -4563 the output should be -3645. The coding style is bit old and it doesn't use namespace. How it's just a guideline for you.

    Code:
    #include <iostream.h>
    
    int rev(int n)
    {
     static int rev_num=0,zero_num=0;
     if(n)
     {
    	if(n<0)
    	{
    		zero_num=1;
    		rev(n*-1);
    	}
    	 else
    	 {
    		rev_num=(rev_num*10)+n&#37;10;
    		rev(n/10);
    	 }
      }
      else
      {
    	  if(zero_num==1)
    		  rev_num*=-1;
    	  return rev_num;
      }
    }
    
    int main()
    {
     int n;
     cout<<"\n\nEnter The Number :";
     cin>>n;
     cout<<"The Reverse Of Num is :"<<rev(n);
     return 0;
    }

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Freddy: Please don't use that code - using a global variable (static) in a recursive function is definitely not a good idea.

    Also, it would break if you make a loop in main asking for multiple numbers, since the second time around, the variables that are expected to be zero are no longer zero, but whatever value they had left in them from last time.

    It is possible (but not at all necessary) to use recursion to solve the problem, but it should not be done with static variables.

    Also:
    Code:
    rev(n*-1);
    ...
    if(zero_num==1)
    		  rev_num*=-1;
    Why not:
    Code:
    rev(-n);
    if(zero_num==1)
    		  rev_num = -rev_num;
    (and "zero_num" is really a bad variable name here - calling it "negative" would be a better choice - also, it doesn't need to be a static for the solution to work).

    For laughs: Here is a recursive reversing function. I don't think this is the solution the teacher is after, so I don't think I'm spoiling the actual search for a solution (as it requires TWO functions, which is one more than the task asks for):
    Code:
    int doreverse(int num, int& mul)
    {
        if (num > 9)
        {
    	int tmp;
    	tmp = doreverse(num / 10, mul);
    	mul *= 10;
    	tmp += (num &#37; 10) * mul;
    	return tmp;
        }
        return num;
    }
    	
    
    
    int reverse(int num)
    {
        int mul = 1;
        return doreverse(num, mul);
    }
    I'm intentionally ignoring negative numbers, as I don't think that was REALLY intended to be part of the task.

    --
    Mats
    Last edited by matsp; 08-22-2008 at 07:44 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The sign is not even an issue if you break down the integer using arithmetic. If you're using signed integers the result will be appropriately signed.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by citizen View Post
    The sign is not even an issue if you break down the integer using arithmetic. If you're using signed integers the result will be appropriately signed.
    Aside from the logic in my posted code, that is - it checks if the number is greater than 9, do the reversal, else return num - so negative numbers are put back the same way they were.

    Edit: Changing the condition from num > 9 to num / 10 will fix that, and the result is correct (with the minus at the front of course)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    So what if I want to use loops. How can I return, the individual digits in the function. I tried doing it but it returns 0 and gives me a warning.

    Here's what I did.

    Code:
    int reverse(int x)
    {
    	int mult = 1,multB = 1/10,loop = 1;
    	int digitCount = countDigit(x);
    	while (loop < digit)
    	{
    		loop++;
    		mult = mult*10;
    		multB = multB*10;
    	         digit = (((y&#37;mult)-(y%multB))/multB);
    		return digit;
    	}
    	
    }
    Last edited by freddyvorhees; 08-22-2008 at 12:14 PM.

  6. #21
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by freddyvorhees View Post
    So what if I want to use loops. How can I return, the individual digits in the function. I tried doing it but it returns 0 and gives me a warning.

    Here's what I did.

    Code:
    int reverse(int x)
    {
    	int mult = 1,multB = 1/10,loop = 1;
    	int digitCount = countDigit(x);
    	while (loop < digit)
    	{
    		loop++;
    		mult = mult*10;
    		multB = multB*10;
    	         digit = (((y%mult)-(y%multB))/multB);
    		return digit;
    	}
    	
    }
    Since multB is an int, this will be 0, not 0.1.
    Also, where did digit come from?

    Why not just keep dividing by 10 until you're left with 0?
    You could also do it using a recursive function call.

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You only need two variables and a pretty simple loop, along the lines of what cpjust says.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #23
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    You could also do it using a recursive function call.
    I don't know how to do "recursive function call". But suppose I use it, will it still work with the main?

    But anyway, I did a new one.It doesnt have function reverse.

    Code:
    #include <iostream>
    using namespace std;
    
    int countDigit(int x)
    {
    	int count = 0;
    	while (x > 10)
    	{
    		count++;
    		x = x/10;
    	}
    	return count;
    }
    
    int main()
    {
    int expA = 1,expB = 10,counter = 0,digit,num;
    cin>>num;
    countDigit(num);
    while (counter <= countDigit(num))
    {
    	counter++;
    	digit = ((num&#37;expB) - (num%expA))/expA;
    	expA = expA*10;
    	expB = expB*10;
    	cout<<digit;
    }
    cout<<endl;
    return 0;
    }

  9. #24
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    Heres what I have in mind.
    Code:
    #include <iostream>
    using namespace std;
    
    int countDigit(int x)
    {
    	int count = 0;
    	while (x > 10)
    	{
    		count++;
    		x = x/10;
    	}
    	return count;
    }
    
    int reverse(int y)
    {
    int expA = 1,expB = 10,counter = 0,digit;
    
    countDigit(y);
    while (counter <= countDigit(y))
    {
    	counter++;
    	digit = ((y&#37;expB) - (y%expA))/expA;
    	expA = expA*10;
    	expB = expB*10;
    	return digit;
    }
    }
    int main()
    {
    	cout<<reverse(1234);
    }

  10. #25
    C++Pandit
    Join Date
    Jul 2008
    Posts
    49
    maybe this will work.
    Code:
    #include<iostream>
    #include<conio.h>
    using namespace std;
    main()
    {
          int number,a,a1,b,b1;
          cout<<"Enter a three digit number"<<endl;
          cin>>number;
          //Let the number be 123
          a=number/100;//1
          a1=number%100;//23
          b=a1/10;//2
          b1=a1%10;//3
          cout<<"The reverse of the entered number is "<<b1<<b<<a;
          getch();
          return 0;
          
    }

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Saimadhav View Post
    maybe this will work.
    Code:
    #include<iostream>
    #include<conio.h>
    using namespace std;
    main()
    {
          int number,a,a1,b,b1;
          cout<<"Enter a three digit number"<<endl;
          cin>>number;
          //Let the number be 123
          a=number/100;//1
          a1=number%100;//23
          b=a1/10;//2
          b1=a1%10;//3
          cout<<"The reverse of the entered number is "<<b1<<b<<a;
          getch();
          return 0;
          
    }
    And of course that will work for 3, 4, 6, 10 digit numbers?

    And it's not a separate function.

    Freddy: There is a much simpler way to solve the problem - you don't need to count digits, and you don't need to have more than one variable besides the original number.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #27
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    May be this may well work for you.

    Code:
    #include <iostream.h>
    int rev(int rev_num,int n)
    {
     if(n)
     {
      rev_num=rev_num*10+(n&#37;10);
      return rev(rev_num,n/10);
     }
     else
    	return rev_num;
    }
    int main()
    {
     int n;
     cout<<"\n\nEnter The Value Of N :";
     cin>>n;
     cout<<"The Reverse Of Num Is : "<<rev(0,n);
     return 0;
    }

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by chottachatri View Post
    May be this may well work for you.

    Code:
    #include <iostream.h>
    int rev(int rev_num,int n)
    {
     if(n)
     {
      rev_num=rev_num*10+(n&#37;10);
      return rev(rev_num,n/10);
     }
     else
    	return rev_num;
    }
    int main()
    {
     int n;
     cout<<"\n\nEnter The Value Of N :";
     cin>>n;
     cout<<"The Reverse Of Num Is : "<<rev(0,n);
     return 0;
    }
    Yes, that's a better solution than your previous solution - however, it doesn't follow the calling convention specified by the code that says "insert code for reverse here" - the function only takes one parameter. As a consequence, you'd have to add a wrapper function that adds the extra parameter - that means that the solution is two functions - there is a simpler way, using only one function with one parameter and one local variable.

    Also, the solution doesn't have to be recursive - it makes it more complex to do it that way, although a recursive function probably REQUIRES a second parameter, so it basicly means that you are adding an extra function.

    --
    Mats
    Last edited by matsp; 08-23-2008 at 05:23 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #29
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    I really don't have an idea what to do now. Suppose I use my previous code, is there a possible way that I can just return the digits in loop , like I cout it in main?

  15. #30
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your solution isn't quite right - have you actually tried it?

    Some detail comments:
    Code:
    int reverse(int y)
    {
    int expA = 1,expB = 10,counter = 0,digit;
    
    countDigit(y);
    
    What does this do, actually? Nothing, that is what it does (other than spend a little bit of time).
    
    while (counter <= countDigit(y))
    {
    	counter++;
    	digit = ((y%expB) - (y%expA))/expA;
    	expA = expA*10;
    	expB = expB*10;
    	return digit;
    
    You return digit - function is finished when you reach a return statement, so nothing more will be done - 
    you will get ONE digit, that's it. The fact that it is in a loop doesn't change the meaning of return. 
    
    }
    }
    Edit: Fix formatting. Also, expB is always 10x expA, so you can replace expB with (expA*10) and reduce the number of variables by one. But as I said before.

    But there is a much simpler loop that solves the problem.

    Here's some hints:
    What is the FIRST (highest value) digit in the reversed number?
    How do you get that out of the original number?
    What do you need to do to the original number to get the NEXT digit out of it?
    What do you need to do to the first digit to "add on" the next digit from the original number?

    --
    Mats
    Last edited by matsp; 08-23-2008 at 07:01 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 09:22 AM