Haskell Program To Find The Factorial Of A Positive Number


This tutorial discusses writing a program to find the factorial of a positive number in the Haskell programming language.

In this tutorial, we see

  • Program to find the factorial of a positive number using a recursive function.

  • Program to find the factorial of a positive number using an inbuilt function product.

Algorithm Steps

  • Take input or initialize a variable to store a positive integer.

  • Implement program logic to find the factorial of a number.

  • Print the resulting factorial.

Method: Find The Factorial Of A Positive Number Using A Recursive Function

Example

Program to find the factorial of a positive number using an inbuilt function product

-- function declaration
factorial :: Int->Int

-- function definition
factorial n = product [1..n]

main :: IO()
main = do
-- initializing variable n
   let n = 5
-- printing the resulting factorial
   print ("The factorial of a number " ++ show n ++ " is:")
   print (factorial n)

Output

"The factorial of a number 5 is:"
120

Description

  • We declared a function factorial as such it takes an integer argument and returns an integer. In its function definition, the function accepts an integer n as an argument. The function invokes a function product with an argument list of integers from 1 to n generated using the double dot operator.

  • The function product takes a list of numbers as an argument and returns the product of them. Here the product of all numbers from 1 to n is the factorial of n. The function returns the output of the product function.

  • In the main function, a variable n is initialized with a number value of 5. The function factorial is invoked with the number n as an argument and returned output is printed using the print function.

Note − The function show takes the argument of a number and returns the parsed string of a number. “++” is an operator to concatenate strings in Haskell.

Method 2: Find The Factorial Of A Positive Number Using An Inbuilt Function Product

Example

Program to find the factorial of a positive number using a recursive function

-- function declaration
factorial :: Int->Int

-- function definition
--base case
factorial 0 = 1
factorial n = (n) * factorial (n-1)

main :: IO()
main = do
-- initializing variable n
   let n = 5
-- printing the resulting factorial
   print ("The factorial of a number " ++ show n ++ " is:")
   print (factorial n)

Output

"The factorial of a number 5 is:"
120

Description

  • A function factorial is declared as such it takes an integer argument and returns an integer. In its function definition, it takes an integer argument n and returns the product of the number n with a recursive call to itself with argument n-1.

  • The recursive calls are invoked till the functions attain a base case where the argument is 0. In the base-case function returns 1. This function computes the factorial of a number.

  • In the main function, a variable n is initialized with a number value of 5. The function factorial is invoked with the number n as an argument and returned output is printed using the print function.

Conclusion

In this tutorial, we discussed two ways to implement a program to find the factorial of a positive number in the Haskell programming Language.

Updated on: 15-Dec-2022

644 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements