
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print Multiplication Table in Triangular Form using Haskell
This tutorial discusses writing a program to print the multiplication table in the triangular form in the Haskell programming language.
For example, the multiplication table for the number 10 in the triangular format is
1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 36 7 14 21 28 35 42 49 8 16 24 32 40 48 56 64 9 18 27 36 45 54 63 72 81 10 20 30 40 50 60 70 80 90 100
Algorithm Steps
Take input or initialize an integer variable n for which the multiplication table should be printed.
Implement the program logic to print the multiplication table.
Example
Program to print multiplication table of a number in the triangular form.
-- function declaration for tablehelper function tablehelper :: Int->Int -> IO() -- function definition for tablehelper function tablehelper n i = if(i==n) then printTable n 1 else do printTable i 1 tablehelper n (i+1) -- function declaration for printTable function printTable :: Int->Int->IO() -- function definition for printTable function printTable n i = if (n==i) then putStrLn((show (n*i)) ++ " ") else do putStr ((show (n*i)) ++ " ") printTable n (i+1) main :: IO() main = do -- initializing variable for n let n = 10 -- invoking the function tablehelper print ("The multiplication table for number " ++ show n ++ " in the triangular form is:") tablehelper n 1
Output
"The multiplication table for number 10 in the triangular form is:" 1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 36 7 14 21 28 35 42 49 8 16 24 32 40 48 56 64 9 18 27 36 45 54 63 72 81 10 20 30 40 50 60 70 80 90 100
In the above program,
We declared a function tableHelper as such it takes two integer arguments and returns an IO action. IO actions are the operations that use input and display consoles. The print function is an IO action as it displays output on the console.
In the function definition of the function tableHelper, two arguments are integer accepted n and a where n is the number for which the multiplication table is to be generated and a is a variable used for iteration. The value of the n is compared to i. If the value of n is equal to i, the control is transferred to the then block which is a base case where the printTable function is invoked with arguments n and 1.
If the value of n is not equal to i, the control is transferred to the else block, where the function printTable function is invoked with argument i and 1. Finally, the function calls itself recursively with arguments n and i+1. The function terminates when i+1 is equal to n.
We declared another function printTable as such it takes two integer arguments and returns an IO action. In its function definition, it accepts two integers n and i.
The function compares the value of n and i, if the n is equal to i the control is transferred to the else block which is a base case where the function prints the multiplicated value of n and i using the putStrLn function. If the n is not equal to i, the control is transferred to the else block where the prints the multiplicated value of n and i using the putStr function. And the function recursively calls itself with arguments n and (i+1) until the base case is reached.
These two recursive function works as a nested loop. In the main function, a variable is initialized for n. The function tableHelper is invoked with arguments n and 1 where n is the initial step of the iteration.
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.
Conclusion
In this tutorial, we discussed implementing a program to print the multiplication table in the triangular form in the Haskell programming language.