Not Yet Answered # Reverse Number In C

BobS0327 24 jnawrocki -3

0

I hope the following explanation is helpful. Unfortunately, I can't find any preview option, so I'm not sure how the post will appear

I've iterated line by line thru the while loop in your posted coded as indicated below.

Assume n (input) is 1234

Since reverse is initially zero

```
reverse = reverse * 10;
```

will result in reverse still being equal to zero

Since n is equal to 1234, using the modulus operator will produce 123.4 and the modulus operator will return 4 which is entered into the reverse variable

Remember the reverse is initially set to zero. So, zero plus 4 equals 4

```
reverse = reverse + n%10;
```

The moudulus operator (percent sign) computes the remainder that results from performing integer division.

At this point reverse now contains 4

We now divide n by 10. Integer division will only return the whole excluding the remainder. So, 1234/10 equals 123. n now contains 123.

```
n = n/10;
```

We now loop back up to...

```
reverse = reverse * 10;
```

Since reverse contains 4, multiplying it by 10 will result in reverse now containing 40

Now we again use the modulus operator on n. Remember that n now contains 123. Using the modulus operator will return a remainder of 3 (123/10 gives 12 with a remainder of 3). To reiterate, the modulus operator will ONLY return the remainder value of 3. Thus, the following statement is adding 3 to the value of reverse which is 40, making reverse now equal to 43

```
reverse = reverse + n%10;
```

The next statement divides n which contains 123 by 10 resulting in n containing the whole number 12. The remainder 3 is discarded,

```
n = n/10;
```

We again loop back up to..

```
reverse = reverse * 10;
```

reverse contains 43 prior to executing the above statement. Thus, multiplying reverse by 10 will make reverse equal 430

Keep in mind prior to executing the next statement that n is equal to 12 and reverse is equal to 430. So, using the modulus operator on n will result in it generating a remainder of 2 which is added to reverse, making reverse equal to 432

```
reverse = reverse + n%10;
```

The current value of n before executing the next statement is 12. After executing the next statement, n will contain 1 size integer division will return whole numbers exclusive of remainders (12/10 = 1)

```
n = n/10;
```

Finally, we loop back one last time to

```
reverse = reverse * 10;
```

reverse contains 432 prior to executing the previous statement. So, when we multiple reverse by 10, reverse will now contain 4320

At this point n contains 1. using the modulus operator will generate a remainder of 1 which is added to reverse, making reverse equal to 4321

```
reverse = reverse + n%10;
```

Executing the next statement returns an integer value of ZERO which will cause the while loop to break since n is equal to ZERO

```
n = n/10;
```

The end result is having reverse contain 4321.

0

Cool but probably slow to execute due to all the math. Seems like handling it as an array of characters and using xor swap would be faster.

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"

Here is the sample output that I ...

Hello All ...

Iam Getting An Error With try to excecute the stored procedure .

I have Have Sql database , the stored procedure like so :

```
USE [MPRS]
GO
/****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE ...
```

I don’t want at this stage work on a big separate project as I've already got plenty ...