Recursive function that find the sum of digits in a integer using C++
###### Posted by Samath
Last Updated: December 24, 2014

Write a function that computes the sum of the digits in an integer.

int sumDigits(long n)

For example, sumDigits(234) returns 2 + 3 + 4 = 9.
Develop a program that prompts the user to enter any integer and display the sum of all its digits.

Solution:

```#include <iostream>
using namespace std;
int sumDigits(int num);
int main(int argc, char *argv[])
{
cout<<"Result: "<<sumDigits(234)<<"\n";
return 0;
}

int sumDigits(int num)
{
if(num == 0)
{
return num;
}
else
{
return sumDigits(num/10) + (num%10);
}
}```

###### C program that prompts the user for a positive integer and then computes the sum of all the digits of the number
Samath | May 04, 2016
###### Java program that reads an integer and prints how many digits the number has
Samath | Feb 11, 2021
###### Write a Python function to calculate the sum of digits of a number.
MaryJns | Aug 17, 2024
###### How can you find the sum of elements in a list using a recursive function in Python?
HenryPk | Aug 22, 2024
###### C++ Summing the digits in an integer
Samath | Jan 06, 2021
###### Summing the Digits in an Integer using Python
Samath | Mar 02, 2015
###### Java program that uses a loop to compute the sum of all odd digits of an input. (For example, if the input is 32677, the sum would be 3 + 7 + 7 = 17.)
Samath | Apr 03, 2022
###### Java program that read a five-digit positive integer and breaks it into a sequence of individual digits
Samath | Feb 10, 2021
###### How do you check if a string contains only digits in Python?
RoseHrs | Aug 14, 2024
###### C++ program that prompts the user to input five decimal numbers, prints the five decimal numbers, converts each decimal number to the nearest integer, adds the five integers and prints the sum and average of the five integers.
Samath | Apr 06, 2022
###### How do you create a recursive CTE to generate a series of numbers?
AliceWk | Jun 20, 2024
###### How can you perform a recursive query using CTEs?
KarenKg | Jul 14, 2024
###### SQL Display two digits after decimal point
SceDev | Feb 08, 2024
###### Sum of two values in C
Samath | Nov 09, 2014