Last Updated : 14 Jul, 2025
Try it on GfG Practice
Given a number n, find the sum of its digits.
Examples :
[Approach 1] Digit Extraction - O(log10n) Time and O(1) SpaceInput: n = 687
Output: 21
Explanation: The sum of its digits are: 6 + 8 + 7 = 21Input: n = 12
Output: 3
Explanation: The sum of its digits are: 1 + 2 = 3
C++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.
#include <iostream>
using namespace std;
int sumOfDigits(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 << sumOfDigits(n);
return 0;
}
C
#include<stdio.h>
int sumOfDigits(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", sumOfDigits(n));
return 0;
}
Java
class GfG {
static int sumOfDigits(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(sumOfDigits(n));
}
}
Python
def sumOfDigits(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(sumOfDigits(n))
C#
using System;
class GfG {
static int sumOfDigits(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(sumOfDigits(n));
}
}
JavaScript
function sumOfDigits(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(sumOfDigits(n));
[Approach 2] Using Recursion - O(log10n) Time and O(log10n) Space
C++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)
#include <iostream>
using namespace std;
int sumOfDigits(int n) {
// Base Case
if (n == 0)
return 0;
// Recursive Case
return (n % 10) + sumOfDigits(n / 10);
}
int main() {
cout << sumOfDigits(12345);
return 0;
}
C
#include <stdio.h>
int sumOfDigits(int n) {
// Base Case
if (n == 0)
return 0;
// Recursive Case
return (n % 10) + sumOfDigits(n / 10);
}
int main() {
printf("%d", sumOfDigits(12345));
return 0;
}
Java
import java.io.*;
class GfG {
static int sumOfDigits(int n) {
// Base Case
if (n == 0)
return 0;
// Recursive Case
return (n % 10) + sumOfDigits(n / 10);
}
public static void main(String[] args) {
System.out.println(sumOfDigits(12345));
}
}
Python
def sumOfDigits(n):
# Base Case
if n == 0:
return 0
# Recursive Case
return n % 10 + sumOfDigits(n // 10)
if __name__ == "__main__":
print(sumOfDigits(12345))
C#
using System;
class GfG {
static int sumOfDigits(int n) {
// Base Case
if(n == 0)
return 0;
// Recursive Case
return n % 10 + sumOfDigits(n / 10);
}
static void Main() {
Console.Write(sumOfDigits(12345));
}
}
JavaScript
function sumOfDigits(n) {
// Base Case
if (n == 0)
return 0;
// Recursive Case
return (n % 10) + sumOfDigits(Math.floor(n / 10));
}
console.log(sumOfDigits(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 sumOfDigits(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 << sumOfDigits(n) << endl;
return 0;
}
C
#include <stdio.h>
#include <string.h>
int sumOfDigits(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", sumOfDigits(n));
return 0;
}
Java
class GfG {
// Function to calculate sum of digits using string conversion
static int sumOfDigits(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(sumOfDigits(n));
}
}
Python
def sumOfDigits(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(sumOfDigits(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 sumOfDigits(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;
}
// Driver Code
let n = 12345;
console.log(sumOfDigits(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.
Program to Find the Sum of All Digits of a Number
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4