How to reverse a number as an integer and not as a string?

前端 未结 19 1912
名媛妹妹
名媛妹妹 2020-12-14 03:29

I came across a question \"How can one reverse a number as an integer and not as a string?\" Could anyone please help me to find out the answer? Reversal should reverse the

相关标签:
19条回答
  • 2020-12-14 03:38
    using System; 
    
    public class DoWhileDemo {   
      public static void Main() { 
        int num; 
        int nextdigit; 
    
        num = 198; 
    
        Console.WriteLine("Number: " + num); 
    
        Console.Write("Number in reverse order: "); 
    
        do { 
          nextdigit = num % 10; 
          Console.Write(nextdigit); 
          num = num / 10; 
        } while(num > 0); 
    
        Console.WriteLine(); 
      }   
    }
    
    0 讨论(0)
  • 2020-12-14 03:39

    multiply it by -1? precise your question please...

    0 讨论(0)
  • 2020-12-14 03:41

    You can't. Since the computer thinks in hexadecimal in any case, it is necessary for you to tokenise the number into Arabic format, which is semantically identical to the conversion to string.

    0 讨论(0)
  • 2020-12-14 03:42

    Old thread, but I did not see this variation:

            int rev(int n) {
                string str = new String(n.ToString().Reverse().ToArray());
                return int.Parse(str);
            }
    
    0 讨论(0)
  • 2020-12-14 03:42
        public static void Main(string[] args)
        {
            int reversed = Reverse(-7360);
        }
    
        public static int Reverse(int number, int left = 0, int right = 0)
        {
            if (number == 0) return left + right;
            return Reverse(number / 10, left * 10 + right * 10, number % 10);
        }
    
    0 讨论(0)
  • 2020-12-14 03:43

    Check below simple and easy -

    public int reverseNumber(int Number)
    {
      int ReverseNumber = 0;
      while(Number > 0)
      {
        ReverseNumber = (ReverseNumber * 10) + (Number % 10);
        Number = Number / 10;
      }
      return ReverseNumber;
    }
    

    Reference : Reverse number program in c#

    0 讨论(0)
提交回复
热议问题