Sum of the digits of a given number using recursion

Follow the below steps to solve the problem:

  • Get the number
  • Get the remainder and pass the next remaining digits
  • 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 the ‘/’ operator to remove the rightmost digit.
  • Check the base case with n = 0
  • Print or return the sum

Below is the implementation of the above approach:

C++




// C++ program to compute
// sum of digits in number.
#include <iostream>
using namespace std;
class gfg {
public:
    int sumDigits(int no)
    {
        if (no == 0) {
            return 0;
        }
 
        return (no % 10) + sumDigits(no / 10);
    }
};
 
// Driver code
int main(void)
{
    gfg g;
 
    // Function call
    cout << g.sumDigits(687);
    return 0;
}


C




// C program to compute
// sum of digits in number.
#include <stdio.h>
 
int sumDigits(int no)
{
    if (no == 0) {
        return 0;
    }
 
    return (no % 10) + sumDigits(no / 10);
}
 
// Driver code
int main()
{
    // Function call
    printf("%d", sumDigits(687));
    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 sumDigits(int no)
    {
        if (no == 0) {
            return 0;
        }
 
        return (no % 10) + sumDigits(no / 10);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // Function call
        System.out.println(sumDigits(687));
    }
}
 
// This code is contributed by Gitanjali


Python3




# Python program to compute
# sum of digits in number.
 
 
def sumDigits(no):
    return 0 if no == 0 else int(no % 10) + sumDigits(int(no/10))
 
 
# Driver code
if __name__ == "__main__":
 
    # Function call
    print(sumDigits(687))
 
# This code is contributed by
# Smitha Dinesh Semwal


C#




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


PHP




<?php
// PHP program to compute
// sum of digits in number.
function sumDigits($no)
{
return $no == 0 ? 0 : $no % 10 +
                      sumDigits($no / 10) ;
}
 
// Driver Code
 
// Function call
echo sumDigits(687);
 
// This code is contributed by aj_36
?>


Javascript




<script>
// Program to compute
// sum of digits in number
  // Function to get sum of digits
          
     function sumDigits(no)
     {
        if(no == 0){
          return 0 ;
        }
 
        return (no % 10) + sumDigits(parseInt(no/10)) ;
      }
       
// Driver code
      document.write(sumDigits(687));
       
// This is code is contributed by simranarora5sos
</script>


Output

21

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

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:

...