- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Haskell Program to Find Sum of Digits of a Number using Recursion
In Haskell,we can find Sum of Digits of a Number by using recursion along with mod, div and other helper functions. getCurrentTime and NominalDiffTime function. In the first example we are going to use (sumOfDigits n | n < 10 = n | otherwise = (n `mod` 10) + sumOfDigits (n `div` 10)) function. And in the second example, we are going to use helper function.
Algorithm
Step 1 − The recursive sumOfDigits function is defined as,
For example 1 −
sumOfDigits n | n < 10 = n | otherwise = (n `mod` 10) + sumOfDigits (n `div` 10).
For example 2 −
sumOfDigits n = sumOfDigits' n 0 where sumOfDigits' n acc | n < 10 = acc + n | otherwise = sumOfDigits' (n `div` 10) (acc + n `mod` 10).
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. It calls the sumOfDigits function and pass the number as argument to it.
Step 3 − The variable named, “num” is being initialized. It will hold the number whose sum of digits is to be computed.
Step 4 − The resultant sum of the digits of the number is printed to the console, after the function is called.
Example 1
In this example, the sumOfDigits function takes an integer as its input and uses recursion to calculate the sum of its digits. The function first checks if the input number is less than 10, and if so, it returns the number itself. If the input number is greater than or equal to 10, the function uses the modulus operator (mod) to find the last digit of the number and the division operator (div) to remove the last digit. It then adds this digit to the result of a recursive call to the sumOfDigits function with the remaining number. The recursion continues until the input number is less than 10, at which point the final sum of digits is returned.
sumOfDigits :: Integer -> Integer sumOfDigits n | n < 10 = n | otherwise = (n `mod` 10) + sumOfDigits (n `div` 10) main :: IO () main = do let num = 12345 print (sumOfDigits num)
Output
15
Example 2
In this example, the sum of digits of a number using recursion in Haskell can be calculated by using a helper function that takes in the number and an accumulator. The helper function will first check if the number is less than 10. If it is, it will return the accumulator plus the number. If the number is greater than or equal to 10, it will call itself with the quotient of the number divided by 10 and the accumulator plus the remainder of the number divided by 10.
sumOfDigits :: Integer -> Integer sumOfDigits n = sumOfDigits' n 0 where sumOfDigits' n acc | n < 10 = acc + n | otherwise = sumOfDigits' (n `div` 10) (acc + n `mod` 10) main :: IO () main = do let num = 12345 print (sumOfDigits num)
Output
15
Conclusion
The sum of digits of a number can be calculated using different methods, such as using a while loop, a for loop, or recursion. The approach using recursion involves breaking down the number into its individual digits and then adding them together using a recursive function. This function repeatedly calls itself with the input number modified by removing one digit until all digits have been added together and the final sum is returned. In Haskell, the sum of digits of a number can be calculated by using recursion along with helper function or div & mod functions.
- Related Articles
- Java Program to Find Sum of Digits of a Number using Recursion
- C# program to find the sum of digits of a number using Recursion
- Haskell Program to Find Factorial of a Number Using Recursion
- Haskell Program to Find Sum of N Numbers Using Recursion
- Haskell Program to Find the Sum of Natural Numbers using Recursion
- Python Program to Find the Sum of Digits in a Number without Recursion
- How to find the sum of digits of a number using recursion in C#?
- Haskell Program to find the reverse of a given number using recursion
- Haskell Program to Find G.C.D Using Recursion
- Haskell Program to Find the Product of Two Numbers Using Recursion
- Haskell Program to find the given number is PRIME or not using recursion
- Java Program to Find Reverse of a Number Using Recursion
- C++ Program to Find Factorial of a Number using Recursion
- Java Program to Find Factorial of a Number Using Recursion
- Haskell Program to find the LCM of two given numbers using recursion
