# Swift Program to Reverse a Number

This tutorial will discuss how to write a Swift program to reverse a number.

Reversing a number is a process in which the position of the digits of a number is interchanged to reverse their order.

Below is a demonstration of the same −

Suppose we enter the following input −

Number = 098627


Following is the desired output −

Reversed order = 726890


## Algorithm

Following is The algorithm to reverse the number −

• Step 1 − Start

• Step 2 − Create a variable named “number”. Now the value of the “number” variable is either user-defined or pre-defined.

• Step 3 − Create another variable named “reverseNum” with value 0.

• Step 4 − Display the original number on the output screen.

• Step 5 − Run a while loop with condition: number != 0.

• Step 6 − Multiply the “reverseNum” with 10

• Step 7 − Add “reverseNum” to the remainder of the number and assign the result to “reverseNum”, here we get the remainder of the “number” by using the module of 10.

• Step 8 − Divide the “number” by 10 to make the current number.

• Step 9 − Display the final output on the screen

• Step 10 − Stop

## Example 1

The following program shows how to reverse a number.

import Foundation
import Glibc

var number = 7826
var reverseNum = 0

print("Original Number-", number)

while(number != 0){
reverseNum = reverseNum * 10
reverseNum = reverseNum + number % 10
number = number/10
}
print("Reverse Number-", reverseNum)


## Output

Original Number- 7826
Reverse Number- 6287


Here in the above code, first, we create two variables with values named number = 7826 and reverseNum = 0. Now we run a while loop till number != 0. This will loop reverse the number = 7826 using the following code −

reverseNum = reverseNum * 10
reverseNum = reverseNum + number % 10
number = number/10


The working of the above code is −

reverseNum = 0
reverseNum = 0 + 7826 % 10 = 6
Number = 7826/10 = 782
reverseNum = 6 * 10 = 60
reverseNum = 60 + 782 % 10 = 60 + 2 = 62
Number = 782/10 = 78
reverseNum = 62 * 10 = 620
reverseNum = 620 + 78 % 10 = 620 + 8 = 628
Number = 78/10 = 7
reverseNum = 628 * 10 = 6280
reverseNum = 6280 + 7 % 10 = 6280 + 7 = 6287
Number = 7/10 = 0
Now we display the output on the output screen which is 6287.


## Example 2

The following program shows how to reverse a number by taking user input.

import Foundation
import Glibc

print("Hey!Enter the number")
var reverseNum = 0

print("Original entered number-", number)

while(number != 0){
reverseNum = reverseNum * 10
reverseNum = reverseNum + number % 10
number = number/10
}
print("Reverse number-", reverseNum)


## STDIN Input

Hey!Enter the number
2342355


## Output

Original entered number- 2342355
Reverse number- 5532432


Here, in the above code, we take the value of the number from the user that is 2342355 using the readLine() function and display the reverse of the number which is 5532432. Here, the working of the code to reverse a number is same as we already discussed in the above example.