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
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();
}
}
multiply it by -1? precise your question please...
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.
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);
}
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);
}
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#