Haskell Program to Check Whether a Number is Even or Odd



In Haskell we have isEven, isOdd and mod functions that can be used for checking whether a given number is even or odd. In the first example, we are going to use (isEven 0 = True and isEven n = isOdd (n - 1)) and (isOdd 0 = False and isOdd n = isEven (n - 1)) function. And in the second example, we are going to use (isEven n = n `mod` 2 == 0) function.

Method 1: Checking whether a number is even or odd using isEven and isOdd function

In this method, the isEven and isOdd functions are defined as mutually recursive functions. The isEven function returns True if the input number is zero, and returns the result of isOdd for n - 1 otherwise. The isOdd function returns False if the input number is zero, and returns the result of isEven for n - 1 otherwise.

Algorithm

  • Step 1 − The isEven function is defined as,

isEven 0 = True
isEven n = isOdd (n - 1).
  • Step 2 − The isOdd function is defined as,

isOdd 0 = False
isOdd n = isEven (n - 1).
  • Step 3 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. In the main function, a variable n is defined with the value 5, and the result of isEven n is checked. If the result is True, the message "Even" is printed to the console. If the result is False, the message "Odd" is printed to the console.

  • Step 4 − The variable named, “n” is being initialized. It will hold the number which is to be checked whether it is even or odd.

  • Step 5 − The result is printed to the console using ‘putStrLn’ after the function is called.

Example 1

In this example, we define isEven and isOdd functions to check whether the number is odd or even.

isEven :: Integer -> Bool
isEven 0 = True
isEven n = isOdd (n - 1)

isOdd :: Integer -> Bool
isOdd 0 = False
isOdd n = isEven (n - 1)

main :: IO ()
main = do
   let n = 5
   if isEven n
      then putStrLn "Even"
      else putStrLn "Odd"

Output

Odd

Method 2: Checking whether a number is even or odd using mod function

In this method, the isEven function simply returns True if the result of n mod 2 is equal to 0, which means the number is evenly divisible by 2 and therefore even. If the result is not equal to 0, the function returns False, meaning the number is not evenly divisible by 2 and therefore odd.

Algorithm

  • Step 1 − The isEven function is defined using mod function as,

isEven n = n `mod` 2 == 0.
  • Step 2 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. In the main function, the isEven function is called with n as the argument, and the result is checked as in the previous implementation. If the result is True, the message "Even" is printed to the console. If the result is False, the message "Odd" is printed to the console.

  • Step 3 − The variable named, “n” is being initialized. It will hold the number which is to be checked whether it is even or odd.

  • Step 4 − The result is printed to the console using ‘putStrLn’ after the function is called.

Example 1

In this example, we define isEven function using mod function to check whether the number is odd or even.

isEven :: Integer -> Bool
isEven n = n `mod` 2 == 0

main :: IO ()
main = do
   let n = 5
   if isEven n
      then putStrLn "Even"
      else putStrLn "Odd"

Output

Odd

Example 2

In this example, the isEven function uses pattern matching to determine if the number is even or odd. If the result of n mod 2 is equal to 0, the function returns the string "Even". If the result is not equal to 0, the function returns the string "Odd".

isEven :: Integer -> String
isEven n
   | n `mod` 2 == 0 = "Even"
   | otherwise      = "Odd"

main :: IO ()
main = do
   let n = 5
   putStrLn (isEven n)

Output

Odd

Conclusion

In Haskell, we can determine if a number is even or odd by using the modulo operator (mod). The modulo operator returns the remainder of the division of one number by another. If the result of the modulo operation between a number n and 2 is equal to 0, the number n is even. If the result is not equal to 0, the number n is odd. We can also use in-built isEven and isOdd functions.


Advertisements