# Haskell program to calculate the sum of natural numbers

In this tutorial, we discuss writing a program to calculate the sum of the natural numbers in the Haskell programming language.

Natural numbers are positive integers starting from 1,2,3...N

In this tutorial, we see different ways to implement a program to compute the sum of natural numbers.

• Program to compute the sum of the natural numbers using a mathematical formula.

• Program to compute the sum of the natural numbers using a recursive function.

• Program to compute the sum of the natural numbers using the list method/function sum.

### Algorithm steps

• Declare or take input the range value(n) of the natural number.

• Implement the program to compute the sum of natural numbers up to n.

• Print or Display the computed sum.

### Example 1

Program to compute the sum of natural numbers using a mathematical formula.

main :: IO()
main = do
-- declaring and initializing the variable for range n
let n = 10
let sum = n*(n+1)/2
-- printing the computed sum
print ("sum of the natural number in range 1 to "++ show n)
print (sum)


### Output

"sum of the natural number in range 1 to 10.0"
55.0


In the above program, we declared and initialized a variable n to load the range value. We computed the sum of the natural numbers up to n by using the logic n*(n+1)/2. We loaded the computed value in a variable sum. Finally, we printed the computed sum using the print function.

Note: “++” is an operator to concatenate two strings. The function show is used to parse numbers into strings. This function takes a single argument and returns a string.

### Example 2

Program to compute the sum of natural numbers using a recursive function

-- function declaration for function sumNatural
sumNatural :: Int->Int

-- function definition for function sumNatural
-- base case
sumNatural 1 = 1
-- all other cases
sumNatural n = n + sumNatural (n-1)

main :: IO()
main = do
-- declaring and initializing the variable for range n
let n = 20
-- computing sum by invoking the function sumNatural and loading it in a variable
let sum = sumNatural n
-- printing the computed sum
print ("sum of the natural number in range 1 to "++ show n)
print (sum)


### Output

"sum of the natural number in range 1 to 20"
210


In the above program, We declared a function sumNatural as such it takes an integer as an argument and returns an integer. In its function definition, we are taking an integer argument n and returning n addition with a recursive call to itself with argument n-1. In the function base, if the argument is 1 the function returns 1. I.e the function returns the sum of the number from 1 to n. In the main function, we declared and initialized the variable for range value n. We invoked the function sumNatural with argument n and loaded the returned value into a variable sum. Finally, we printed the variable sum.

### Example 3

Program to compute the sum of natural numbers using the list method/function sum

main :: IO()
main = do
-- declaring and initializing the variable for range n
let n = 10
-- generating a list of numbers from 1 to n
let numbers = [1..n]
-- computing sum by using the list method sum
let sum1 = sum numbers
-- printing the computed sum
print ("sum of the natural number in range 1 to "++ show n)
print (sum1)


### Output

"sum of the natural number in range 1 to 10.0"
55.0


In the above program, we declared and initialized a variable n to load the range value. We generated a list of numbers from 1 to n using the ”..” operator and loaded the list into a variable numbers. We invoked the function sum with the argument numbers. The function sum takes a number list and returns the sum of the elements in the list. We loaded the returned sum into a variable sum1 and Finally, we printed the variable sum1.

## Conclusion

In this tutorial, we discussed three different ways to implement a program to calculate the sum of natural numbers in Haskell programming language.