Link to home
Start Free TrialLog in
Avatar of Omar Alaa
Omar Alaa

asked on

C# Reverse int in fast ways

i have been looking for a way to reverse an integer and i have found this

while(left>0)
{
  r = left % 10;
  rev = rev * 10 + r;
  left = left / 10;
}

Open in new window

but the problem is this code is extremely slow. because it will only be a very small part of big operation.
so i tried to code it myself and i have been to this
string[] userinput = Console.ReadLine().Split(' ');
			int i = int.Parse(userinput[0]);
			int j = int.Parse(userinput[1]);
			int k = int.Parse(userinput[2]);
			int Reverse = 0;
			char[] temp;
			string trans = i.ToString();
			temp = trans.ToCharArray();
			temp.Reverse();
			trans = new string(temp);
			Reverse = int.Parse(trans);
			Console.WriteLine(Reverse);

Open in new window

but it always gives the value 0 "about the j and k forget about them its just for the rest of my code" so can anyone help me how can i make it work and if this can't be done any new ways to how to make it in quicker way than the shown above because it's really extremely slow.
Avatar of ste5an
ste5an
Flag of Germany image

Can you explain your need for this operation?

Cause "reverse an int" in our given context depends on the format (base). E.g. 0xef is also an int. What is its "reverse" value? It seems to depend on the base..

So your loop maybe already the fastest way to do it in .NET.

Caveat: an int could be lesser then 0, then your code does not work.
Didn't get the task...
Avatar of Omar Alaa
Omar Alaa

ASKER

I meant for example if the int value is 5273 i want it to be 3725
ASKER CERTIFIED SOLUTION
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Okay. Thanks to letting me know that :D