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

前端 未结 19 1915
名媛妹妹
名媛妹妹 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:47

    This should do it:

    int n = 12345;
    int left = n;
    int rev = 0;
    while(Convert.ToBoolean(left)) // instead of left>0 , to reverse signed numbers as well
    {
       int r = left % 10;   
       rev = rev * 10 + r;
       left = left / 10;  //left = Math.floor(left / 10); 
    }
    
    Console.WriteLine(rev);
    
    0 讨论(0)
  • 2020-12-14 03:47

    Yay! A bling way. (No, really. I hope that this is more a "How would I do..." question and not something you really need in production)

    public int Reverse(int number) {
      return int.Parse(number.ToString().Reverse().Aggregate("", (s,c) => s+c));
    }
    
    0 讨论(0)
  • 2020-12-14 03:50

    As per Pritam Karmakar comment , Yes this will not work when given number has 0 at end. If you pass number as 150 it will return 51 not 051. so i had write code to display number in Reverse order as per user input please check this.

    int reverseNum = 0, reminder, num;
            Console.WriteLine("Enter Number to Reverse:");
            int.TryParse(Console.ReadLine(), out num);
            bool isZero = false;
            int cnt=0;
            while (num > 0)
            {
                reminder = num % 10;
                reverseNum = (reverseNum * 10) + reminder;
                num = num / 10;
                if (reverseNum == 0)
                    isZero = true;                
                cnt++;
            }
            if (isZero)
            {
                Console.WriteLine(reverseNum.ToString().PadLeft(cnt, '0'));
            }
            else
            {
                Console.WriteLine(reverseNum);
            }
            Console.ReadKey();
    
    0 讨论(0)
  • 2020-12-14 03:50

    Solution suggested by @jerjer and @cfern is perfect but It will not work for negative int number like -1234507.

    Solution : If number is negative int then multiply by -1 after that call your method ReverseInt(-1234507)

        int number = -1234507;
        **number = number > 0 ? number : (number * -1);**
    
        int reverseNumber = 0;
    
        while(number > 0){
            reverseNumber = (reverseNumber * 10) +  (number % 10) ;
            number = number / 10;
        }
        Console.WriteLine(reverseNumber);
    
    0 讨论(0)
  • 2020-12-14 03:55

    a new one:

    using System;
    using System.Linq;
    
    public class Program
    {
        public static void Main()
        {
            int i = 234;
            int j = -123;
            Console.WriteLine(reverse(i));
            Console.WriteLine(reverse(j));
        }
    
        static int reverse(int i)
        {
            int sign = i / Math.Abs(i);
            var abs = string.Concat(Math.Abs(i).ToString().Reverse());
            return int.Parse(abs) * sign;
        }
    }
    

    https://dotnetfiddle.net/VGJJFf

    0 讨论(0)
  • 2020-12-14 03:56
        /// <summary>
        /// Reverse a int using its sting representation.
        /// </summary>
        private int ReverseNumber(int value)
        {
            string textValue = value.ToString().TrimStart('-');
    
            char[] valueChars = textValue.ToCharArray();
            Array.Reverse(valueChars);
            string reversedValue = new string(valueChars);
            int reversedInt = int.Parse(reversedValue);
    
            if (value < 0)
               reversedInt *= -1;
    
            return reversedInt;
        }
    
    0 讨论(0)
提交回复
热议问题