Haskell Program to Calculate the Power of a Number


This tutorial discusses writing a program to calculate the power of a number in the Haskell programming language.

The nth power of a number is the value when the number is multiplied by itself n times.

The power of numbers can be expressed in exponential forms “a^n”, a is raised to the power of n. Here a is called the base and n is called an exponent. For example, 2 raised to the power of 5 is 2^5 i.e 32.

In this tutorial, we see different ways to implement a program to calculate the power of a number.

  • Program to calculate the power of a number using the operator “^”.
  • Program to calculate the power of a number using the operator “^^”.
  • Program to calculate the power of a number using the operator “**”.
  • Program to calculate the power of a number using the operator “*”.

Algorithm Steps

  • Take input or initialize the variables.
  • Implement the program logic to compute the power of a number.
  • Print or display the output.

Example 1

Program to calculate the power of a number using the operator “^”

main :: IO()
main = do
-- initializing and declaring variable for base and exponent
   let base = 2
   let expo = 5
-- computing the power and printing the output
   print (show base ++ " raised to the power of " ++ show expo ++ " is:")
   print (base^expo)

Output

"2 raised to the power of 5 is:"
32

In the above program we declared and initialized variables for base and exponent as base and expo with values 2 and 5. We computed the power of the base using the operator “^”. Finally, we printed the output using the print function. The function declaration for operator “^” is (^) :: (Num a, Int b) => a -> b -> a. The base can be an integer or float and the exponent must be an integer.

Note − The function show is used to parse numbers to strings. This function takes a number argument and returns a string. “++” is an operator to concatenate strings in Haskell.

Example 2

Program to calculate the power of a number using the operator “^^”

main :: IO()
main = do
-- initializing and declaring variable for base and exponent
   let base = 2.1
   let expo = 5
-- computing the power and printing the output
   print (show base ++ " raised to the power of " ++ show expo ++ " is:")
   print (base^^expo)

Output

"2.1 raised to the power of 5 is:"
40.841010000000004

In the above program we declared and initialized variables for base and exponent as base and expo with values 2.1 and 5. We computed the power of the base using the operator “^^”. Finally, we printed the output using the print function. The function declaration for operator “^^” is (^^) :: (Float a, Int b) => a -> b -> a. The base is float and the exponent must be an integer.

Example 3

Program to calculate the power of a number using the operator “**”

main :: IO()
main = do
-- initializing and declaring variable for base and exponent
   let base = 2.1
   let expo = 5.2
-- computing the power and printing the output
   print (show base ++ " raised to the power of " ++ show expo ++ " is:")
   print (base**expo)

Output

"2.1 raised to the power of 5.2 is:"
47.374030205310675

In the above program we declared and initialized variables for base and exponent as base and expo with values 2.1 and 5.2. We computed the power of the base using the operator “**”. Finally, we printed the output using the print function. The function declaration for operator “**” is (**) :: (Float a) => a -> a -> a. The base is float and the exponent is Float.

Example 4

Program to calculate the power of a number using the operator “*”

-- function declaration
power :: Int->Int->Int
-- function definition
-- base case 1
power base 0 = 1
-- base case 2
power base 1 = base
power base expo = base*(power base (expo-1))
main :: IO()
main = do
-- initializing and declaring variable for base and exponent
   let base = 2
   let expo = 5
-- computing the power and printing the output
   print (show base ++ " raised to the power of " ++ show expo ++ " is:")
   print (power base expo)

Output

"2 raised to the power of 5 is:"
32

In the above program, we declared a function power as such it takes two integer arguments and returns an integer. In its function defined, we defined two base cases where if the 2nd argument is zero we are returning 1. If the second argument is 1, we are returning the first argument(base). In other cases, the function returns a recursive call with argument base and (expo-1) multiplied with base. I.e this function returns the power of base up to the degree of the expo. In the main function, we declared and initialized variables for base and exponent as base and expo with values 2 and 5. We invoked the function with these two variables as arguments and printed the returned output using the print function.

Conclusion

In this tutorial, we discussed four different ways to implement a program to calculate the power of a number in Haskell programming Haskell.

Updated on: 14-Dec-2022

672 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements