# Haskell Program to Generate Multiplication Table

In this tutorial, we discuss writing a program to generate a multiplication table in the Haskell programming language.

## Example

Multiplication table of 12 up to 10 is

12 * 1 = 12
12 * 2 = 24
12 * 3 = 36
12 * 4 = 48
12 * 5 = 60
12 * 6 = 72
12 * 7 = 84
12 * 8 = 96
12 * 9 = 108
12 * 10 = 120


In this tutorial, we see

• Program to generate a multiplication table of a number up to the constant range of 10. (Tail recursion)

• Program to generate a multiplication table of a number up to a variable range. (Head recursion)

Note

Tail recursion is a recursive function with the recursive call as the last expression.

Head recursion is a recursive function with the recursive call as the first expression.

### Algorithm steps

• Declare or take input a number to which a multiplication table should be generated.

• Implement the program to generate a multiplication table.

• Print or Display the multiplication table.

### Example 1

Program to generate multiplication table of a number up to 10

-- function declaration for function generate
generate :: Int->Int->IO()

-- function definition for function generate
-- base case
generate n 10 = do
putStrLn (show n ++ " * " ++ show 10++" = " ++ show (n*10))
generate n i = do
putStrLn (show n ++ " * " ++ show i++" = " ++ show (n*i))
generate n (i+1)

-- function declaration for function generateTable
generateTable :: Int->IO()

-- function definition for function generateTable
generateTable n = generate n 1

main :: IO()
main = do
-- declaring and initializing a variable for the number
let num = 12
-- invoking the generateTable function
putStrLn ("The multiplication table of " ++ show num ++ " is:")
generateTable num


### Output

The multiplication table of 12 is:
12 * 1 = 12
12 * 2 = 24
12 * 3 = 36
12 * 4 = 48
12 * 5 = 60
12 * 6 = 72
12 * 7 = 84
12 * 8 = 96
12 * 9 = 108
12 * 10 = 120


Note

• ++” is an operator to concatenate strings.

• putStrLn is a function to display output on the console with a trailing newline (‘
’)

• Show is a function to take an argument of a number and return them by parsing it into a String.

In the above program,

• We declared a function generate, which takes two Integer arguments and returns an IO action. In its function definition, The function takes two integer arguments n and i. The function prints the multiplication of the two arguments in a format and recursively calls itself by incrementing the second argument until the second argument is equal to 10.

• I.e The function generate prints the multiplication table of a number from ‘i’ to 10.

• We declared a function generateTable, which takes an Integer as an argument and returns an IO action. In its function definition, we take an integer n as an argument and we are calling the function generate with n and 1 as arguments.

• The function generateTable is a helper function that can be invoked with a single argument and this function invokes the generate function with a second argument as ‘1’ which returns an IO action printing the multiplication table of number n.

• In the main function we declare and initialize a variable num and finally we printed the multiplication table by invoking the function generateTable with argument num.

### Example 2

Program to generate multiplication table of a number up to a variable range.

-- function declaration for function generateTable
generateTable :: Int->Int->IO()

-- function definition for function generateTable
-- base case
generateTable n 1 = do
putStrLn (show n ++ " * " ++ show 1++" = " ++ show (n*1))

generateTable n i = do
generateTable n (i-1)
putStrLn (show n ++ " * " ++ show i++" = " ++ show (n*i))

main :: IO()
main = do
-- declaring and initializing a variable for the number
let num = 12
let len = 20
-- invoking the generateTable function
putStrLn ("The multiplication table of " ++ show num ++ " upto length " ++ show len ++ " is:")
generateTable num len


### Output

The multiplication table of 12 upto length 20 is:
12 * 1 = 12
12 * 2 = 24
12 * 3 = 36
12 * 4 = 48
12 * 5 = 60
12 * 6 = 72
12 * 7 = 84
12 * 8 = 96
12 * 9 = 108
12 * 10 = 120
12 * 11 = 132
12 * 12 = 144
12 * 13 = 156
12 * 14 = 168
12 * 15 = 180
12 * 16 = 192
12 * 17 = 204
12 * 18 = 216
12 * 19 = 228
12 * 20 = 240


In the above program,

• We declared a function generateTable which takes two integers and returns an IO action. In its function definition we take two integers n and i as arguments which recursively call itself with argument n and i-1 until the second argument i is equal to 1. We are using a head recursive technique until the base case is attainted and the function calls are stored in the stack. As the last call in the function is printing the record in the multiplication of arguments. The table is printed in reverse order from 1 to n even though we started with n. This technique is an application of head recursion.

• The above function prints the multiplication table of number num up to range i.

• In the main function we declared and initialized values for number and range and finally printed the multiplication table by invoking the function generateTable.

## Conclusion

In this tutorial, we discussed two different ways to implement a program to generate a multiplication table in the Haskell programming language.