Sum of the digits of a given number using tail recursion

Follow the below steps to solve the problem:

  • Add another variable “Val” to the function and initialize it to ( Val = 0 )
  • On every call to the function add the mod value (n%10) to the variable as “(n%10)+val” which is the last digit in n. Along with passing the variable n as n/10. 
  • So on the First call, it will have the last digit. As we are passing n/10 as n, It follows until n is reduced to a single digit. 
  • n<10 is the base case so When n < 10, then add the n to the variable as it is the last digit and return the val which will have the sum of digits

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check sum of digit using tail recursion
int sum_of_digit(int n, int val)
{
    if (n < 10) {
        val = val + n;
        return val;
    }
    return sum_of_digit(n / 10, (n % 10) + val);
}
 
// Driver code
int main()
{
    int num = 12345;
 
    // Function call
    int result = sum_of_digit(num, 0);
    cout << "Sum of digits is " << result;
    return 0;
}
 
// This code is contributed by subhammahato348


C




// C program for the above approach
#include <stdio.h>
 
// Function to check sum of digit using tail recursion
int sum_of_digit(int n, int val)
{
    if (n < 10) {
        val = val + n;
        return val;
    }
    return sum_of_digit(n / 10, (n % 10) + val);
}
 
// Driver code
int main()
{
    int num = 12345;
 
    // Function call
    int result = sum_of_digit(num, 0);
    printf("Sum of digits is %d", result);
    return 0;
}
 
// This code is contributed by Sania Kumari Gupta


Java




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class sum_of_digits {
 
    // Function to check sum
    // of digit using tail recursion
    static int sum_of_digit(int n, int val)
    {
        if (n < 10) {
            val = val + n;
            return val;
        }
        return sum_of_digit(n / 10, (n % 10) + val);
    }
 
    // Driven code
    public static void main(String args[])
    {
        int num = 12345;
 
        // Function call
        int result = sum_of_digit(num, 0);
        System.out.println("Sum of digits is " + result);
    }
}


Python3




# Python3 program for the above approach
 
# Function to check sum
# of digit using tail recursion
 
 
def sum_of_digit(n, val):
 
    if (n < 10):
        val = val + n
        return val
 
    return sum_of_digit(n // 10, (n % 10) + val)
 
# Driver code
 
 
if __name__ == "__main__":
    num = 12345
 
    # Function call
    result = sum_of_digit(num, 0)
 
    print("Sum of digits is", result)
 
# This code is contributed by subhammahato348


C#




// C# program for the above approach
using System;
 
class GFG {
 
    // Function to check sum
    // of digit using tail recursion
    static int sum_of_digit(int n, int val)
    {
        if (n < 10) {
            val = val + n;
            return val;
        }
        return sum_of_digit(n / 10, (n % 10) + val);
    }
 
    // Driver code
    public static void Main()
    {
        int num = 12345;
 
        // Function call
        int result = sum_of_digit(num, 0);
 
        Console.Write("Sum of digits is " + result);
    }
}
 
// This code is contributed by subhammahato348


Javascript




<script>
 
// Javascript program for the above approach
 
// Function to check sum
// of digit using tail recursion
function sum_of_digit(n, val)
{
    if (n < 10)
    {
        val = val + n;
        return val;
    }
    return sum_of_digit(parseInt(n / 10),
    (n % 10) + val);
}
 
// Driver code
    let num = 12345;
    let result = sum_of_digit(num, 0);
     
    document.write("Sum of digits is " + result);
 
// This code is contributed by subhammahato348
 
</script>


Output

Sum of digits is 15

Time Complexity: O(log N)
Auxiliary Space: O(log N)

Please write comments if you find the above codes/algorithms incorrect, or find better ways to solve the same problem.



Program for Sum of the digits of a given number

Given a number, find the sum of its digits.

Examples : 

Input: n = 687
Output: 21

Input: n = 12
Output: 3

Recommended Practice

Follow the below steps to solve the problem:

  • Get the number
  • Declare a variable to store the sum and set it to 0
  • Repeat the next two steps till the number is not 0
  • Get the rightmost digit of the number with help of the remainder ‘%’ operator by dividing it by 10 and adding it to the sum.
  • Divide the number by 10 with help of ‘/’ operator to remove the rightmost digit.
  • Print or return the sum

Below is the implementation of the above approach:

C++




// C++ program to compute sum of digits in
// number.
#include <bits/stdc++.h>
using namespace std;
 
/* Function to get sum of digits */
class gfg {
public:
    int getSum(int n)
    {
        int sum = 0;
        while (n != 0) {
            sum = sum + n % 10;
            n = n / 10;
        }
        return sum;
    }
};
 
// Driver code
int main()
{
    gfg g;
    int n = 687;
 
    // Function call
    cout << g.getSum(n);
    return 0;
}
// This code is contributed by Soumik


C




// C program to compute sum of digits in
// number.
#include <stdio.h>
 
/* Function to get sum of digits */
int getSum(int n)
{
    int sum = 0;
    while (n != 0) {
        sum = sum + n % 10;
        n = n / 10;
    }
    return sum;
}
 
// Driver code
int main()
{
    int n = 687;
 
    // Function call
    printf(" %d ", getSum(n));
    return 0;
}


Java




// Java program to compute
// sum of digits in number.
import java.io.*;
 
class GFG {
 
    /* Function to get sum of digits */
    static int getSum(int n)
    {
        int sum = 0;
 
        while (n != 0) {
            sum = sum + n % 10;
            n = n / 10;
        }
 
        return sum;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 687;
 
        // Function call
        System.out.println(getSum(n));
    }
}
 
// This code is contributed by Gitanjali


Python3




# Python 3 program to
# compute sum of digits in
# number.
 
# Function to get sum of digits
 
 
def getSum(n):
 
    sum = 0
    while (n != 0):
 
        sum = sum + int(n % 10)
        n = int(n/10)
 
    return sum
 
 
# Driver code
if __name__ == "__main__":
    n = 687
 
    # Function call
    print(getSum(n))


C#




// C# program to compute
// sum of digits in number.
using System;
 
class GFG {
    /* Function to get sum of digits */
    static int getSum(int n)
    {
        int sum = 0;
 
        while (n != 0) {
            sum = sum + n % 10;
            n = n / 10;
        }
 
        return sum;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 687;
 
        // Function call
        Console.Write(getSum(n));
    }
}
 
// This code is contributed by Sam007


PHP




<?php
// PHP Code to compute sum
// of digits in number.
 
// Function to get
// $sum of digits
function getsum($n)
{
    $sum = 0;
    while ($n != 0)
    {
        $sum = $sum + $n % 10;
        $n = $n/10;
    }
    return $sum;
}
 
// Driver Code
$n = 687;
 
// Function call
$res = getsum($n);
echo("$res");
 
// This code is contributed by
// Smitha Dinesh Semwal.
?>


Javascript




<script>
 
// Javascript program to compute sum of digits in
// number.
 
/* Function to get sum of digits */
function getSum(n)
{
    var sum = 0;
    while (n != 0) {
        sum = sum + n % 10;
        n = parseInt(n / 10);
    }
    return sum;
}
 
// Driver code
var n = 687;
document.write(getSum(n));
 
</script>


Output

21

Time Complexity: O(log N)
Auxiliary Space: O(1)

Similar Reads

How to compute in a single line?

...

Sum of the digits of a given number using recursion:

...

Sum of the digits of a given number with input as string:

...

Sum of the digits of a given number using tail recursion:

...