Loops Posted May 10, 2020

For any given number we would need to return the number in reverse, with the starting place to the end place. For example, for an input of 1423 we would need to return 3241 which is its exact reverse. To achieve this, we would use the same principle we use for splitting the number; the Divisibility by 10 rule.

```
public static int GetReversedNumber(int n)
{
int r, sum = 0, input = n;
if (n < 10) return n;
while (n != 0)
{
r = n % 10;
sum = sum * 10 + r;
n = n / 10;
}
return sum;
}
```

**How it works:**

The logic starts in the same way as splitting the numbers, but for every time a remainder is obtained, it is added up on to a sum and before adding up the remainder the sum is multiplied with a 10 so that it grows in one place in the number.

```
For an input 1423:
sum = 0*10 + 3 = 3
sum = 3*10 + 2 = 32
sum = 32*10 + 4 = 324
sum = 324*10 + 1 = 3241
```

Enjoy this article?
*Buy Me a Coffee*

Join the community! Like on Facebook Follow on Twitter

Compute and generate a compressed string for a given string containing repetitions

Find all possible natural numbers below a given limit such that a3+b3 = c3+d3

Find all the pairs in a given ordered set of numbers whose sum is equal to a given input sum

Find all the pairs in a given unordered set of numbers whose sum is equal to a given input sum

Sort the given unordered set of Binary digits

(UPDATED to .NET 6) Implementing CQRS with MediatR in ASP.NET Core

(UPDATED to .NET 6) Calling Stored Procedures in ASP.NET Core / EF Core

(UPDATED to .NET 5) Integrating OData with ASP.NET Core Web API

Integrating MongoDB with ASP.NET Core (.NET 6)

Implementing Role based and Claims based Authorization in ASP.NET Core (.NET 5)