Problem:
Give a Positive Integer number, find the sum of all of its digits. In Mathematics Digit Sum problems is defined as the digit sum of a natural number that is the sum of all of its digits.
Iterative Algorithm:
- Declare a variable called sum to accumulate the sum of digits.
- Initialize the variable to zero.
- Loop until the number is greater than zero.
- Take Modulus of the number with 10 to extract digit
- Add the digit to the variable sum
- Divide the number by 10 to remove the digit taken in step 5
- Repeat until the number becomes zero.
- The variable sum will have the sum of all the digits of the number.
Recursive Algorithm:
- Base Case: If the number is Zero return 0.
- Take Modulus of number by 10 to extract its digit.
- Divide the number by 10 to remove the digit taken in Step 2.
- Return the Method with result from step 3 + the digit.
- Once Recursive calls end, you will have the result sum of Digits.
Example:
Example:
Number: 12345
Answer: 15
Number: 12345
Answer: 15
Code:
main.cpp
/**********************************************************
* Problem: Digit Sum Problem
*
* Programmer: CodingHelpLine
* Website: https://codinghelpline.org
*
* Description:
* Digit Sum is a mathematical problem that states given a
* natural number, the digit sum stands for the sum of all
* the digits of the given number
*
* Example: 12345
* Digit Sum => 5 + 4 + 3 + 2 + 1 => 15
*
**********************************************************/
#include <iostream>
using std::cout;
using std::endl;
// Function prototypes.
// 1. Iterative Function.
int digitSumIterative(int n);
// 2. Recursive Function
int digitSumRecursive(int n);
int main() {
int n = 12345; // => 15
// call iterative
cout << "Sum of Digits of " << n << " = " << digitSumIterative(n) << endl;
// call Recursive
cout << "Sum of Digits of " << n << " = " << digitSumRecursive(n) << endl;
return EXIT_SUCCESS;
}
// 1. Iterative Function.
// a. declare sum variable, initialize to zero
// b. loop until n > 0
// c. Take modulus
// d. Add to sum
// e. divide n by 10
// repeat until n > 0
int digitSumIterative(int n)
{
int sum = 0;
while(n > 0)
{
int digit = n % 10;
sum += digit;
n = n / 10;
}
return sum;
}
// 2. Recursive Function
int digitSumRecursive(int n)
{
// 1. Base Case
if(n == 0)
{
return 0;
}
// 2. Recursive takes these calculations
// extract digit
// divide n by 10
// call recursive method
int digit = n % 10;
n = n / 10;
return digit + digitSumRecursive(n);
}
* Problem: Digit Sum Problem
*
* Programmer: CodingHelpLine
* Website: https://codinghelpline.org
*
* Description:
* Digit Sum is a mathematical problem that states given a
* natural number, the digit sum stands for the sum of all
* the digits of the given number
*
* Example: 12345
* Digit Sum => 5 + 4 + 3 + 2 + 1 => 15
*
**********************************************************/
#include <iostream>
using std::cout;
using std::endl;
// Function prototypes.
// 1. Iterative Function.
int digitSumIterative(int n);
// 2. Recursive Function
int digitSumRecursive(int n);
int main() {
int n = 12345; // => 15
// call iterative
cout << "Sum of Digits of " << n << " = " << digitSumIterative(n) << endl;
// call Recursive
cout << "Sum of Digits of " << n << " = " << digitSumRecursive(n) << endl;
return EXIT_SUCCESS;
}
// 1. Iterative Function.
// a. declare sum variable, initialize to zero
// b. loop until n > 0
// c. Take modulus
// d. Add to sum
// e. divide n by 10
// repeat until n > 0
int digitSumIterative(int n)
{
int sum = 0;
while(n > 0)
{
int digit = n % 10;
sum += digit;
n = n / 10;
}
return sum;
}
// 2. Recursive Function
int digitSumRecursive(int n)
{
// 1. Base Case
if(n == 0)
{
return 0;
}
// 2. Recursive takes these calculations
// extract digit
// divide n by 10
// call recursive method
int digit = n % 10;
n = n / 10;
return digit + digitSumRecursive(n);
}
Views: 8