Swift Program to Calculate the Power of a Number


This tutorial will discuss how to write a swift program to calculate the power of a number.

Power of a number means how many times a number is used in multiplication, for example, 16 ^2 = 16 * 16 = 256 or 16 to the power of 2. It is also known as exponent or indices.

Below is a demonstration of the same −

Suppose our given input is −

Number - 16

Exponent Value is - 3

The desired output is −

Final result is 16^3 = 4096

Calculating power of a number using recursion

We can calculate the power of a number using recursion. Recursion is a process in which a function calls itself to solve a problem.

Algorithm

Following are the algorithm steps to calculate the power of a number using recursion −

  • Step 1 − Create a recursive function. This function returns the power of the given number.

  • Step 2 − Call the recursive function with two parameters that are the base number and the exponent number.

  • Step 3 − Display the final output.

Example

The following program shows how to calculate the power of a number using recursion −

import Foundation import Glibc func powerRecursion(baseNumber: Int, exponentNumber: Int) -> Int{ // Declare a variable of int type it is used to store the power of the number // Here the initial value of the variable is 1 var PowerOfNumber : Int = 1 // Checking if the exponent is greater than 0 if exponentNumber > 0 { // Calculating the power of the number PowerOfNumber = baseNumber * (powerRecursion(baseNumber: baseNumber, exponentNumber: exponentNumber-1)) } return PowerOfNumber } var finalResult = powerRecursion(baseNumber: 18, exponentNumber: 3) print("Power is - ", finalResult)

Output

Power is - 5832

In the above code, we create a recursive function named powerRecursion() to calculate the power of a number by calling itself recursively. After creating this function, we called the function using two parameters that are 13 and 8 and display the final result which is 5832.

Calculating power of a number using pow() function

We can calculate the power of a number using the pow() function. It is used to calculate the power of a number. It takes two arguments and returns the power of the number.

Syntax

pow(baseNumber, exponentNumber)

Example

The following program shows how to calculate the power of a number using the pow() function with user defined input.

import Foundation import Glibc print("Please enter the number") // Here we use Double() function to convert the input into double. var baseNumber = Double(readLine()!)! print("Please enter the exponent") // Here we use Double() function to convert the input into double. var exponentNumber = Double(readLine()!)! var PowerOfNumber = pow(baseNumber, exponentNumber) print("Entered Number is - ", baseNumber) print("Entered exponent is - ", exponentNumber) print("\n Power of the number is - ", PowerOfNumber)

STDIN Input

Entered Number is - 18.0 
Entered exponent is - 3.0

Output

Power of the number is - 5832.0

In the above code, we calculate the power of a number using the pow() function. Here the number is entered by the user using the readLine() function and the entered number is further converted into double using Double() function because pow() function return values in double.

Updated on: 01-Aug-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements