# Haskell Program to calculate the sum of all odd numbers up to N

This tutorial will help us in calculating the sum of all odd numbers up to N. Haskell uses a functional programming paradigm, which means that it uses functions to transform data, rather than using loops and variables to keep track of state changes. There are different ways to calculate the sum of all odd numbers u to N in Haskell.

## Method 1: Using List Comprehension

In this method, the function sumOddNumbers uses a list comprehension to generate a list of all odd numbers up to n, and then uses the sum function to calculate the sum of the numbers in the list. The final result is displayed to the user.

### Algorithm

• Step 1 − A variable named, “n” is initialized and will hold the value up to which the sum of all odd numbers is to be printed.

• Step 2 − The function sumOddNumbers is defined using list comprehension as, sumOddNumbers n = sum [x | x <- [1,3..n], x mod 2 /= 0].

• 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.

• Step 4 − A variable named, “sum” is being initialized. It will hold the sum of all odd numbers up to N, once the sumOddNumbers function is being called.

• Step 5 − The final result is displayed.

### Example

In this example, we are going to learn about to calculate the sum of all odd numbers up to N

main :: IO ()
main = do
let n = 10
let sum = sumOddNumbers n
putStrLn ("Sum of all odd numbers up to " ++ show n ++ " is: " ++ show sum)
sumOddNumbers :: Int -> Int
sumOddNumbers n = sum [x | x <- [1,3..n], x mod 2 /= 0]


### Output

Sum of all odd numbers up to 10 is: 25


## Method 2: Using Recursion

In This method , the sumOddNumbers function uses recursion to calculate the sum. The base case is when the input is 1, in which case the function returns 1.

### Algorithm

• Step 1 − A variable named, “n” is being initialized and will hold the value up to which the sum of all odd numbers is to be printed.

• Step 2 − The function sumOddNumbers is defined using recursion where, the base case is when the input is 1, in which case the function returns 1. For other cases, the function checks if the input is even or odd. If the input is even, it recursively calls itself with the input decremented by 1. If the input is odd, it adds the input to the result of a recursive call with the input decremented by 2.

• 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.

• Step 4 − A variable named, “sum” is being initialized. It will hold the sum of all odd numbers up to N, once the sumOddNumbers function is being called.

• Step 5 − The final result is displayed.

### Example

In the following example, we are going to calculate the sum of all odd numbers up to N

main :: IO ()
main = do
let n = 10
let sum = sumOddNumbers n
putStrLn ("Sum of all odd numbers up to " ++ show n ++ " is: " ++ show sum)
sumOddNumbers :: Int -> Int
sumOddNumbers 1 = 1
sumOddNumbers n | n mod 2 == 0 = sumOddNumbers (n-1)
| otherwise = n + sumOddNumbers (n-2)


### Output

Sum of all odd numbers up to 10 is: 25


## Method 3: Using foldl Function

This method uses a fold function on a list of odd numbers up to N, generated using list comprehension. The fold function takes a binary operator (in this case +) and an initial accumulator value (0) and applies the operator to the elements of the list and the accumulator, in this way it gives the sum of all odd numbers.

### Algorithm

• Step 1 − A variable named, “n” is being initialized and will hold the value up to which the sum of all odd numbers is to be printed.

• Step 2 − The function sumOddNumbers is defined using foldl function which is defined as, sumOddNumbers n = foldl (+) 0 [x | x <- [1,3..n], x mod 2 /= 0].

• 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.

• Step 4 − A variable named, “sum” is being initialized. It will hold the sum of all odd numbers up to N, once the sumOddNumbers function is being called.

• Step 5 − The result is displayed.

### Example

In this example, we are going to calculate the sum of all odd numbers up to N using foldl function.

main :: IO ()
main = do
let n = 10
let sum = sumOddNumbers n
putStrLn ("Sum of all odd numbers up to " ++ show n ++ " is: " ++ show sum)
sumOddNumbers :: Int -> Int
sumOddNumbers n = foldl (+) 0 [x | x <- [1,3..n], x mod 2 /= 0]


### Output

Sum of all odd numbers up to 10 is: 25


## Conclusion

There are different ways to calculate the sum of all odd numbers up to N. In Haskell, the sum of all odd numbers up to N can be calculated by using list comprehension, by using foldl function or by using recursion.

Updated on: 19-Jan-2023

341 Views 