Swift program to find the reverse of a given number using recursion


This tutorial will discuss how to write swift program to find the reverse of a given number using recursion.

Reversing a number is a process in which the position of the digits of a number is interchanged to reverse their order. To find the reverse of the number we use recursive approach. Recursive approach is an approach in which a function call itself to complete the specified task.

Below is a demonstration of the same −

Input

Suppose our given input is −

MyVal = 278938

Output

The desired output would be −

Reverse number = 839872

Method 1

Algorithm

Following is the algorithm −

Step 1 - Create a recursive function.

Step 2 - Set base condition to end the recursive call.

Step 3 - Print the unit digit of the given number.

Step 4 - Call the function recursively and find the remains number.

Step 5 - Process continue till the number is reduced to single digit.

Example

The following program shows how to find the reverse of a given number using recursion.

import Foundation
import Glibc
func reverseNum(N: Int){
   if (N < 10){
      print(N)
   }
   else{
      print(N%10, terminator: "")
      reverseNum(N:N/10)
   }
}
var Num = 1234
print("Reverse of (Num) is ")
reverseNum(N: Num)

Output

Reverse of (Num) is 
4321

Here in the above code, we create a recursive function named reverseNum(). This function is used to reverse the given number = 1234 by calling itself. So the working of the above code is −

1st call with 1234 -

reverseNum(N: 1234):
Print(1234%10) = 4
reverseNum(N:1234/10) = 123

2nd call with 123 -

reverseNum(N: 123):
Print(123%10) = 3
reverseNum(N:123/10) = 12

3rd call with 12 -

reverseNum(N: 12):
Print(12%10) = 2
reverseNum(N:12/10) = 1

4th call with 1 -

reverseNum(N: 1):
Print(1%10) = 1
reverseNum(N:1/10) = 0 // Condition false loop end

Hence the reverse of 1234 is 4321.

Method 2

Algorithm

Following is the algorithm −

Step 1 − Create a recursive function.

Step 2 − Set base condition to end the recursive call.

Step 3 − Declare a variable named ext to extract the unit digit from the given number.

Step 4 − We add the ext to res variable. Here the res is multiply by the 10 before adding the ext number to the reverse number. Because next number is always added to the unit position.

Step 5 − Call the function recursively and reverse the remains number.

Step 6 − Declare a variable to store the number which we want to reverse.

Step 7 − Call the function and pass the number in the function as an argument.

Step 8 − Print the output.

Example

The following program shows how to find the reverse of a given number using recursion.

import Swift
import Foundation
var res = 0
func reverseNUM(num: Int)->Int{

   if (num == 0){
      return res
   }
   if (num > 0){
      let ext = num % 10
      res = res * 10 + ext
      _ = reverseNUM(num: num/10)
   }
   return res
}
var val = 8655454
print("Original Number: ", val)
print("Reverse number: ", reverseNUM(num:val))

Output

Original Number:  8655454
Reverse number:  4545568

Here in the above code, we create a recursive function named reverseNUM(). This function is used to reverse the given number = 8655454 by calling itself. So the working of the above code is −

1st call with 8655454 -

reverseNUM(num: 8655454):
if (8655454 > 0){
   let ext = 8655454 % 10 = 4
   res = res * 10 + ext = 0 * 10 + 4 = 4
   reverseNUM(num: 8655454/10) = 865545
}

2nd call with 865545 -

reverseNUM(num: 865545):
if (865545 > 0){
   let ext = 865545 % 10 = 5
   res = res * 10 + ext = 4 * 10 + 5 = 45
   reverseNUM(num: 865545/10) = 86554
}
Till if(num>0) condition is false.

Updated on: 13-Dec-2022

337 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements