# Swift program to find the product of two numbers using recursion

SwiftServer Side ProgrammingProgramming

#### Swift 4 Online Training

38 Lectures 1 hours

#### Swift Basics: Learn to Code from Scratch [For Beginners]

13 Lectures 2 hours

#### How to create Tic-Tac-Toe Game for iPhone using Swift in Xcode - iOS App

7 Lectures 1 hours

This tutorial will discuss how to write swift program to find the product of two numbers using recursion.

Recursion is a process in which a function call itself directly or indi rectly to solve some specific task or problem. Using recursion we can also find the product of two numbers.

## Product of Positive Numbers

Given two positive numbers, now we find their product using recursion.

Below is a demonstration of the same −

Input

Suppose our given input is −

Num1 = 12
Num2 = 4


Output

The desired output would be −

Product = 48


### Algorithm

Following is the algorithm −

• Step 1− Create a function with two arguments that are P and Q.

• Step 2− Use if else to specify the conditions. If P is less than Q the swap the number by calling the function recursively.

• Step 3− If Q is not equal to 0 then find the product by calculating Q times sum of P by calling the function recursively.

• Step 4− If any number among the given number is 0 then the result is 0.

• Step 5− Declare two variables and store two numbers in them. Here the number can be pre defined or user defined.

• Step 6− Call the function and pass these numbers as a parameters.

• Step 7− Print the output

### Example

The following program shows how to fi nd the product of two numbers using recursion.

import Foundation
import Glibc

// Function to find the product of two numbers
// Using recursion
func findProduct(P: Int, Q: Int) -> Int{

// If P is less than Q the swap the numbers
if (P < Q){
return findProduct(P: Q, Q: P)
}

// Find the product by
// calculating Q times sum of P
else if (Q != 0){
return (P + findProduct(P: P, Q: Q - 1))
}

// If any number is 0 among given two numbers
// then return 0
else{
return 0
}
}
var num1 = 12
var num2 = 3
print("Product of \(num1) and \(num2):", findProduct(P:num1, Q: num2))


### Output

Product of 12 and 3: 36


Here in the above code, we create a recursive function named findProduct() to find th e product of two numbers. So the working of the findProduct() function is −

Here we have two numbers that are 12 and 3. Now we fist check if P is less than Q. If yes then swap the numbers, otherwise, move to the next condition −

if (12< 3) //False{
return findProduct(P: P, Q: Q)
}


Now we find the product by calling the function recursively −

1st call with P = 12 and Q = 3: findProduct(P: 12, Q: 3) = 12 + findProduct(P: 12, Q: 3-1)= 12 + findProduct(P: 12, Q: 2)

2nd call with P = 12 and Q = 2: findProduct(P: 12, Q: 3) = 12 + findProduct(P: 12, Q: 2-1)= 12 + findProduct(P: 12, Q: 1)

3rd call with P = 12 and Q = 1: findProduct(P: 12, Q: 1) = 12 + findProduct(P: 12, Q: 1-1)= 12 + findProduct(P: 12, Q: 0)

Returned from 3rd call = 12 + 0

Returned from 2nd call = 12 + 12 = 24

Returned from 2nd call = 12 + 24 = 36

Hence the product of 12 and 3 is 36

## Product of Positive and Negative Numbers

Given two numbers, now we find their product using recursion. Here both the numbers can be positive or negati ve or one number can be positive and another number is negative, or one number is positive and another number is 0.

Below is a demonstration of the same −

Input

Suppose our given input is −

Num1 = -12
Num2 = 4


Output

The desired output would be −

Product = -48


### Algorithm

Following is the algorithm −

• Step 1− Create a function with two arguments that are P and Q.

• Step 2− Use if else to specify the conditions. If Q is negative then sawp the parameters and again call the function.

• Step 3− If both the parameters a re negative then find their product of their absolute values by calling the function recursively.

• Step 4− If P > Q then swap their position so the recursion will take less time.

• Step 5− If Q is not equal to 0 till then recursively call the function and ret urn the product of P times Q - 1.

• Step 6− If any number among the given number is 0 then the result is 0.

• Step 7− Declare two variables and store two numbers in them. Here the number can be pre-defined or user defined.

• Step 8− Call the function and pass thes e numbers as a parameters.

• Step 9− Print the output

### Example

The following program shows how to find the product of two numbers using recursion.

import Foundation
import Glibc

// Function to find the product of two numbers
// Using recursion
func findProduct(P: Int, Q: Int) -> Int{

// If P > 0 and Q < 0 then swap the position of P
// and M to keep the value of second
// parameter positive
if (P > 0 && Q < 0){
return findProduct(P: Q, Q: P)
}

// If both P and Q is less than 0 then
// return their product
else if (P < 0 && Q < 0){
return findProduct(P: (-1 * P), Q: (-1 * Q))
}

// If P > Q then swap their position so the recursion
// will take less time
else if (P > Q) {
return findProduct(P: Q, Q: P)
}

// Find the product by
// calculating Q times sum of P
else if (Q != 0)
{
return (P + findProduct(P: P, Q: Q - 1))
}
// If any number is 0 among given two numbers
// then return 0
else {
return 0
}
}
var num1 = -12
var num2 = 14

var num3 = -34
var num4 = -23
print("Product of \(num1) and \(num2):", findProduct(P:num1, Q: num2))
print("Product of \(num3) and \(num4):", findProduct(P:num3, Q: num4))


### Output

Product of -12 and 14: -168
Product of -34 and -23: 782


Here in the above code we create a recursive function named findProduct() to find the product of two numbers. Here we find the product if both the numbers positive, or negative, or one number can be positive and another number is negative, or one number is positive and another number is 0. So in the above code, we have the following numbers −

-12 and 14 and their product is -168

-34 and -23 and their product is 782

Updated on 10-Oct-2022 12:42:50