-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseInteger.cs
More file actions
35 lines (32 loc) · 1.04 KB
/
ReverseInteger.cs
File metadata and controls
35 lines (32 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
namespace CodeChallenge
{
public class ReverseInteger
{
public int ReversingOfInteger(int x)
{
int reversed = 0;
if (x ==int.MinValue)
{
Console.WriteLine("Number Entered has exceeded the int value to be reversed");
}
//Logic to handle the sign in fron of the number
//and Work with the absolute value
int sign = x < 0 ? -1 : 1;
x = Math.Abs(x);
while (x > 0)
{
//Logic to Extract the last digit
int lastDigit = x % 10;
//Check for overflow given from the question
if (reversed > (int.MaxValue - lastDigit)/ 10)
{
Console.WriteLine("The Reversed format is Greater than the Value of the required number");
}
//then arrange the reversed number
reversed = reversed * 10 + lastDigit;
x /= 10;
}
return reversed * sign;
}
}
}