Haskell Program to divide two numbers
Program
main = do
let a = 12
let b = 4
putStrLn "Division of two numbers"
print (a / b)
This Haskell program performs division between two numbers and prints the result.
-
main = do
- Defines the main function where input and output operations occur.
-
let a = 12
andlet b = 4
- Declares two immutable variables,
a
andb
, with values12
and4
, respectively.
- Declares two immutable variables,
-
putStrLn "Division of two numbers"
- Prints a message indicating the operation being performed.
-
print (a / b)
- Performs division (
12 / 4
). /
is the division operator in Haskell, used for floating-point division.- If
a
andb
are both integers, Haskell will throw a type error since/
works withFloat
orDouble
. - To fix this,
a
andb
should be explicitly converted usingfromIntegral a / fromIntegral b
.
- Performs division (
Output
$ ghc divide_two_numbers.hs
[1 of 1] Compiling Main ( divide_two_numbers.hs, divide_two_numbers.o )
Linking divide_two_numbers ...
$ ./divide_two_numbers
Division of two numbers
3.0