Sum of Digits of a Number
Last Updated :
18 Jun, 2025
Given a number n, find the sum of its digits.
Examples :
Input: n = 687
Output: 21
Explanation: The sum of its digits are: 6 + 8 + 7 = 21
Input: n = 12
Output: 3
Explanation: The sum of its digits are: 1 + 2 = 3
[Approach 1] Digit Extraction - O(log10n) Time and O(1) Space
We can sum the digits of a number by repeatedly extracting the last digit using n % 10
, adding it to the sum, and then removing it by dividing n
by 10 using integer division.
C++
#include <iostream>
using namespace std;
int sumOfDig(int n) {
int sum = 0;
while (n != 0) {
// Extract the last digit
int last = n % 10;
// Add last digit to sum
sum += last;
// Remove the last digit
n /= 10;
}
return sum;
}
int main() {
int n = 12345;
cout << sumOfDig(n);
return 0;
}
C
#include<stdio.h>
int sumOfDig(int n) {
int sum = 0;
while (n != 0) {
// Extract the last digit
int last = n % 10;
// Add last digit to sum
sum += last;
// Remove the last digit
n /= 10;
}
return sum;
}
int main() {
int n = 12345;
printf("%d", sumOfDig(n));
return 0;
}
Java
class GfG {
static int sumOfDig(int n) {
int sum = 0;
while (n != 0) {
// Extract the last digit
int last = n % 10;
// Add last digit to sum
sum += last;
// Remove the last digit
n /= 10;
}
return sum;
}
public static void main(String[] args) {
int n = 12345;
System.out.println(sumOfDig(n));
}
}
Python
def sumOfDig(n):
sum = 0
while n != 0:
# Extract the last digit
last = n % 10
# Add last digit to sum
sum += last
# Remove the last digit
n //= 10
return sum
if __name__ == "__main__":
n = 12345
print(sumOfDig(n))
C#
using System;
class GfG {
static int sumOfDig(int n) {
int sum = 0;
while (n != 0) {
// Extract the last digit
int last = n % 10;
// Add last digit to sum
sum += last;
// Remove the last digit
n /= 10;
}
return sum;
}
static void Main() {
int n = 12345;
Console.WriteLine(sumOfDig(n));
}
}
JavaScript
function sumOfDig(n) {
let sum = 0;
while (n !== 0) {
// Extract the last digit
let last = n % 10;
// Add last digit to sum
sum += last;
// Remove the last digit
n = Math.floor(n / 10);
}
return sum;
}
let n = 12345;
console.log(sumOfDig(n));
[Approach 2] Using Recursion - O(log10n) Time and O(log10n) Space
We can use recursion to find the sum of digits. The idea is to extract the last digit, add it to the sum of digits of the remaining number, and repeat.
Base Case: If the number is 0, return 0.
Recursive Case: Return (n % 10) + sumOfDig(n / 10)
C++
#include <iostream>
using namespace std;
int sumOfDig(int n) {
// Base Case
if (n == 0)
return 0;
// Recursive Case
return (n % 10) + sumOfDig(n / 10);
}
int main() {
cout << sumOfDig(12345);
return 0;
}
C
#include <stdio.h>
int sumOfDig(int n) {
// Base Case
if (n == 0)
return 0;
// Recursive Case
return (n % 10) + sumOfDig(n / 10);
}
int main() {
printf("%d", sumOfDig(12345));
return 0;
}
Java
import java.io.*;
class GfG {
static int sumOfDig(int n) {
// Base Case
if (n == 0)
return 0;
// Recursive Case
return (n % 10) + sumOfDig(n / 10);
}
public static void main(String[] args) {
System.out.println(sumOfDig(12345));
}
}
Python
def sumOfDig(n):
# Base Case
if n == 0:
return 0
# Recursive Case
return n % 10 + sumOfDig(n // 10)
if __name__ == "__main__":
print(sumOfDig(12345))
C#
using System;
class GfG {
static int sumOfDig(int n) {
// Base Case
if(n == 0)
return 0;
// Recursive Case
return n % 10 + sumOfDig(n / 10);
}
static void Main() {
Console.Write(sumOfDig(12345));
}
}
JavaScript
function sumOfDig(n) {
// Base Case
if (n == 0)
return 0;
// Recursive Case
return (n % 10) + sumOfDig(Math.floor(n / 10));
}
console.log(sumOfDig(12345));
[Approach 3] String Conversion
Convert the number to a string and iterate through each character (digit). For each character, subtract the ASCII value of '0'
to get the actual digit, then add it to the sum.
Note: This method is especially useful when the number is too large to fit in standard integer types.
C++
#include <iostream>
#include <string>
using namespace std;
// Function to calculate sum of digits using string conversion
int sumOfDig(int n) {
// Convert number to string
string s = to_string(n);
int sum = 0;
// Loop through each character, convert to digit, and add to sum
for (char ch : s) {
sum += ch - '0';
}
return sum;
}
int main() {
int n = 12345;
cout << sumOfDig(n) << endl;
return 0;
}
C
#include <stdio.h>
#include <string.h>
// Function to calculate sum of digits using string conversion
int sumOfDig(int n) {
// Convert number to string
char s[20];
sprintf(s, "%d", n);
int sum = 0;
// Loop through each character, convert to digit, and add to sum
for (int i = 0; i < strlen(s); i++) {
sum += s[i] - '0';
}
return sum;
}
int main() {
int n = 12345;
printf("%d\n", sumOfDig(n));
return 0;
}
Java
class GfG {
// Function to calculate sum of digits using string conversion
static int sumOfDig(int n) {
// Convert number to string
String s = Integer.toString(n);
int sum = 0;
// Loop through each character, convert to digit, and add to sum
for (char ch : s.toCharArray()) {
sum += ch - '0';
}
return sum;
}
public static void main(String[] args) {
int n = 12345;
System.out.println(sumOfDig(n));
}
}
Python
# Function to calculate sum of digits using string conversion
def sumOfDig(n):
# Convert number to string
s = str(n)
sum = 0
# Loop through each character, convert to digit, and add to sum
for ch in s:
sum += int(ch)
return sum
n = 12345
print(sumOfDig(n))
C#
using System;
class GfG
{
// Function to calculate sum of digits using string conversion
public static int sumOfDigits(int n)
{
// Convert number to string
string s = n.ToString();
int sum = 0;
// Loop through each character, convert to digit, and add to sum
foreach (char ch in s)
{
sum += ch - '0';
}
return sum;
}
public static void Main()
{
int n = 12345;
Console.WriteLine(sumOfDigits(n));
}
}
JavaScript
function sumOfDig(n) {
// Convert number to string
let s = n.toString();
let sum = 0;
// Loop through each character, convert to digit, and add to sum
for (let ch of s) {
sum += parseInt(ch);
}
return sum;
}
let n = 12345;
console.log(sumOfDig(n));
Time Complexity: O(d) – we iterate over each of the d digits, where d ≈ log₁₀(n) (count of digits)
Auxiliary Space: O(d) - to store all d
digits as characters.
Similar Reads
Sum of digit of a number using recursion Given a number, we need to find sum of its digits using recursion.Examples: Input: 12345Output: 15Explanation: Sum of digits â 1 + 2 + 3 + 4 + 5 = 15Input: 45632Output: 20 Approach:To understand the algorithm, consider the number 12345 and refer to the illustration below..Extract the last digit: 123
3 min read
Find sum of digits in factorial of a number Given a number n, write code to find the sum of digits in the factorial of the number. Given n ⤠5000 Examples: Input : 10 Output : 27 Input : 100 Output : 648 Recommended PracticeSum of digits in factorial of a numberTry It!It is not possible to store a number as large as 100! under some data types
11 min read
Sum of an array of large numbers Given an integer K and an array arr[] consisting of N large numbers in the form of strings, the task is to find the sum of all the large numbers of the array. Examples: Input: K = 50, arr[] = {â01234567890123456789012345678901234567890123456789â, â01234567890123456789012345678901234567890123456789â,
10 min read
Sum of digits of a given number to a given power Given a number, we need to find the sum of all the digits of a number which we get after raising the number to a specified power.Examples: Input: number = 5, power = 4 Output: 13 Explanation: Raising 5 to the power 4 we get 625. Now adding all the digits = 6 + 2 + 5 Input: number = 9, power = 5 Outp
3 min read
Check if the sum of digits of a number N divides it Given a number n, the task is to check if the sum of digits of the given number divides the number or not. Examples: Input : n = 12Output : YesExplanation: Sum of digits = 1+2 =3 and 3 divides 12.Input : n = 15Output : NoExplanation: Sum of digits = 1+5 =6 and 15 % 6 != 0.Using Iterative Method - O(
6 min read