Open In App

Sum of digit of a number using recursion

Last Updated : 16 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a number, we need to find sum of its digits using recursion.
Examples: 
 

Input : 12345
Output : 15

Input : 45632
Output :20
          

 

The step-by-step process for a better understanding of how the algorithm works. 
Let the number be 12345. 
Step 1-> 12345 % 10 which is equal-too 5 + ( send 12345/10 to next step ) 
Step 2-> 1234 % 10 which is equal-too 4 + ( send 1234/10 to next step ) 
Step 3-> 123 % 10 which is equal-too 3 + ( send 123/10 to next step ) 
Step 4-> 12 % 10 which is equal-too 2 + ( send 12/10 to next step ) 
Step 5-> 1 % 10 which is equal-too 1 + ( send 1/10 to next step ) 
Step 6-> 0 algorithm stops 
following diagram will illustrate the process of recursion 
 

 

C++




// Recursive C++ program to find sum of digits
// of a number
#include <bits/stdc++.h>
using namespace std;
 
// Function to check sum of digit using recursion
int sum_of_digit(int n)
{
    if (n == 0)
    return 0;
    return (n % 10 + sum_of_digit(n / 10));
}
 
// Driven code
int main()
{
    int num = 12345;
    int result = sum_of_digit(num);
    cout << "Sum of digits in "<< num
       <<" is "<<result << endl;
    return 0;
}
 
// THis code is contributed by
// SHUBHAMSINGH10


C




// Recursive C program to find sum of digits
// of a number
#include <stdio.h>
 
// Function to check sum of digit using recursion
int sum_of_digit(int n)
{
    if (n == 0)
       return 0;
    return (n % 10 + sum_of_digit(n / 10));
}
 
// Driven Program to check above
int main()
{
    int num = 12345;
    int result = sum_of_digit(num);
    printf("Sum of digits in %d is %d\n", num, result);
    return 0;
}


Java




// Recursive java program to
// find sum of digits of a number
import java.io.*;
 
class sum_of_digits
{
    // Function to check sum
    // of digit using recursion
    static int sum_of_digit(int n)
    {
        if (n == 0)
            return 0;
        return (n % 10 + sum_of_digit(n / 10));
    }
 
    // Driven Program to check above
    public static void main(String args[])
    {
        int num = 12345;
        int result = sum_of_digit(num);
        System.out.println("Sum of digits in " +
                           num + " is " + result);
    }
}
 
// This code is contributed by Anshika Goyal.


Python3




# Recursive Python3 program to
# find sum of digits of a number
 
# Function to check sum of
# digit using recursion
def sum_of_digit( n ):
    if n == 0:
        return 0
    return (n % 10 + sum_of_digit(int(n / 10)))
 
# Driven code to check above
num = 12345
result = sum_of_digit(num)
print("Sum of digits in",num,"is", result)
 
# This code is contributed by "Sharad_Bhardwaj".


C#




// Recursive C# program to
// find sum of digits of a number
using System;
 
class GFG {
     
    // Function to check sum
    // of digit using recursion
    static int sum_of_digit(int n)
    {
        if (n == 0)
            return 0;
             
        return (n % 10 + sum_of_digit(n / 10));
    }
 
    // Driven Program to check above
    public static void Main()
    {
        int num = 12345;
        int result = sum_of_digit(num);
        Console.WriteLine("Sum of digits in " +
                           num + " is " + result);
    }
}
 
// This code is contributed by Anant Agarwal.


PHP




<?php
// Recursive PHP program
// to find sum of digits
// of a number
 
// Function to check sum of
// digit using recursion
function sum_of_digit($n)
{
    if ($n == 0)
        return 0;
    return ($n % 10 +
            sum_of_digit($n / 10));
}
 
// Driven Code
$num = 12345;
$result = sum_of_digit($num);
echo("Sum of digits in " . $num . " is " . $result);
 
// This code is contributed by Ajit.
?>


Javascript




<script>
 
// Recursive Javascript program to find sum of digits
// of a number
 
// Function to check sum of digit using recursion
function sum_of_digit(n)
{
    if (n == 0)
    return 0;
    return (n % 10 + sum_of_digit(parseInt(n / 10)));
}
 
// Driven code
var num = 12345;
var result = sum_of_digit(num);
document.write( "Sum of digits in "+ num
   +" is "+result );
 
</script>


Output: 

Sum of digits in 12345 is 15

Besides writing (n==0 , then return 0) in the code given above we can also write it in this manner , there will be no change in the output .

if(n<10) return n; By writing this there will be no need to call the function for the numbers which are less than 10 

Time complexity : O(logn) where n is the given number. 

Auxiliary space : O(logn) due to recursive stack space.
 



Previous Article
Next Article

Similar Reads

Count ways to generate N digit number such that its every digit divisible by previous digit
Given a number N, the task is to count the number of ways to create an N digit number from digits 1 to 9 such that every digit is divisible by its previous digit that is if the number is represented by an array of digits A then A[i + 1] % A[i] == 0. print the answer modulo 109 + 7. Examples: Input: N = 2Output: 23Explanation: For N = 2 possible ans
16 min read
Largest number less than N with digit sum greater than the digit sum of N
Given an integer N, the task is to find the greatest number less than N such that the sum of its digits is greater than the sum of the digits of N. If the condition isn't satisfied for any number then print -1.Examples: Input: N = 100 Output: 99 99 is the largest number less than 100 sum of whose digits is greater than the sum of the digits of 100I
9 min read
Count of N-digit numbers having digit XOR as single digit
Given an integer N, the task is to find the total count of N-Digit numbers such that the Bitwise XOR of the digits of the numbers is a single digit. Examples: Input: N = 1Output: 9Explanation: 1, 2, 3, 4, 5, 6, 7, 8, 9 are the numbers. Input: N = 2Output: 66Explanation: There are 66 such 2-digit numbers whose Xor of digits is a single digit number.
14 min read
Add the given digit to a number stored in a linked list using recursion
Given a linked list which represents an integer number where each node is a digit of the represented integer. The task is to add a given digit N to the represented integer.Examples: Input: LL = 9 -&gt; 9 -&gt; 3 -&gt; NULL, N = 7 Output: 1 -&gt; 0 -&gt; 0 -&gt; 0 -&gt; NULL 993 + 7 = 1000Input: LL = 2 -&gt; 9 -&gt; 9 -&gt; NULL, N = 5 Output: 3 -
10 min read
Count the occurrence of digit K in a given number N using Recursion
Given an integer N greater than 0, the task is to find the occurrence of digit K present in the given number N. Examples: Input: N = 10000, K = 0 Output: 4 Explanation: Occurrence of '0' digit in 10000 is 4. Input: N = 51435, K = 5 Output: 2 Explanation: Occurrence of '5' digit in 51435 is 2 Approach: The idea is to use recursion to extract the lea
3 min read
Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
Given the range L and R, count all numbers between L to R such that sum of digits of each number and sum of square of digits of each number is Prime.Note: 10 &lt;= [L, R] &lt;= 108 Examples: Input: L = 10, R = 20 Output: 4 Such types of numbers are: 11 12 14 16 Input: L = 100, R = 130 Output: 9Such types of numbers are : 101 102 104 106 110 111 113
9 min read
Why is Tail Recursion optimization faster than normal Recursion?
What is tail recursion? Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call. What is non-tail recursion? Non-tail or head recursion is defined as a recursive function in which the recursive call is the f
4 min read
Count of integer whose factorial of digit sum contains every digit of original number
Given an array arr[] of length n which contains positive integers (0 ≤ arr[i] ≤ 109), the task is to count the number of elements present in the given array such that the factorial of their digit sum (we will do digit sum while its value is greater than 10) contains every digit present in the original integer. Examples: Input: arr[] = [653, 663, 24
14 min read
Generate a number such that the frequency of each digit is digit times the frequency in given number
Given a number N containing digits from 1 to 9 only. The task is to generate a new number using the number N such that the frequency of each digit in the new number is equal to the frequency of that digit in N multiplied by the digit itself.Note: The digits in the new number must be in increasing order.Examples: Input : N = 312 Output : 122333 Expl
8 min read
Minimum N-Digit number required to obtain largest N-digit number after performing given operations
Given a positive integer N, the task is to find the minimum N-digit number such that performing the following operations on it in the following order results into the largest N-digit number: Convert the number to its Binary Coded Decimal form.Concatenate all the resulting nibbles to form a binary number.Remove the least significant N bits from the
6 min read
Article Tags :
Practice Tags :