How to reverse integers with C# .NET

Say you need to write a function that accepts and integer and returns its reverse. Here come a couple of examples:


So we’re not talking about some mathematical function, but more of a string-reverse. However, note that the negative sign must stay in place, so there’s a little bit of maths in there.

If that doesn’t sound like a problem that you would normally solve in a real life project then you’re probably right. I’m not even sure if there’s a legitimate business use-case for such a function. However, job interview tests often don’t revolve around real-life problems, at least not the initial ones that are meant to filter out candidates that are not a good fit for the position. It’s good to go through the most common problems so that you can breeze through them and have time over for the more difficult ones.

The C# solution is very easy in fact, and I’m sure it’s equally simple in other popular languages. Without any further due here’s one of the many possible solutions out there:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithms
    public class IntReverse
        public int ReverseInteger(int start)
            var reverse = string.Join("", Math.Abs(start).ToString().Reverse());
            return int.Parse(reverse) * Math.Sign(start);

Let’s see how this works.


The above bit takes the incoming integer, takes its absolute value and turns it to a string. Why take the absolute value? We don’t want the ‘-‘ character to end up at the end of the string, so at this point we get rid of it. So at this point we have the following strings:


Next we call the Reverse() extension on this string. It returns an IEnumerable<char>, i.e. not the reversed string, but the constituent characters in a reversed order like here:

28[‘8’, ‘2’]
9876[‘6’, ‘7’, ‘8’, ‘9’]
-456[‘6’, ‘5’, ‘4’]
-1928[‘8’, ‘2’, ‘9’, ‘1’]

The string.Join function takes a delimiter and joins a collection into a string using that delimiter. Since the delimiter is an empty string it means that we don’t want anything in between the joined characters. This is the current state of things at this point:


The values in the Output column are assigned to the reverse variable. It seems we have lost the negative sign on the way, but that’s OK, we can still read it from the incoming start parameter. The Math library has a function called Sign which returns -1 for negative numbers, 0 for 0’s and +1 for positive numbers. So we parse the reverse variable into an integer and multiply it with the correctly signed 1. Hence we have the reversed integer as specified and we can return it.

Here comes a set of unit tests:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Algorithms.Tests
    public class IntReverseTests
        public void ReverseIntTests()
            IntReverse intReverse = new IntReverse();
            Assert.AreEqual(4, intReverse.ReverseInteger(4));
            Assert.AreEqual(28, intReverse.ReverseInteger(82));
            Assert.AreEqual(9876, intReverse.ReverseInteger(6789));
            Assert.AreEqual(-4, intReverse.ReverseInteger(-4));
            Assert.AreEqual(-456, intReverse.ReverseInteger(-654));
            Assert.AreEqual(-1928, intReverse.ReverseInteger(-8291));

They pass as expected.

That’s it!

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

HarsH ReaLiTy

A Good Blog is Hard to Find

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog


Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: