# Program for Sum the digits of a given number

Given a number, find sum of its digits.

Examples :

```Input : n = 687
Output : 21

Input : n = 12
Output : 3
```

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

General Algorithm for sum of digits in a given number:

1. Get the number
2. Declare a variable to store the sum and set it to 0
3. Repeat the next two steps till the number is not 0
4. Get the rightmost digit of the number with help of remainder ‘%’ operator by dividing it with 10 and add it to sum.
5. Divide the number by 10 with help of ‘/’ operator
6. Print or return the sum

Below are the solutions to get sum of the digits.
1. Iterative:

## C++

 `// C program to compute sum of digits in  ` `// number. ` `# include ` `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; ` ` ``cout<< g.getSum(n); ` ` ``return` `0; ` `} ` `//This code is contributed by Soumik `

## C

 `// C program to compute sum of digits in  ` `// number. ` `# include ` ` `  `/* 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; ` `} ` ` `  `int` `main() ` `{ ` `  ``int` `n = 687; ` `  ``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 program ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `n = ``687``; ` ` `  `        ``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` ` `  `n ``=` `687` `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 program ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 687; ` `        ``Console.Write(getSum(n)); ` `    ``} ` `} ` ` `  `// This code is contributed by Sam007 `

## PHP

 ` `

Output :

`21`

How to compute in single line?
Below function has three lines instead of one line but it calculates sum in line. It can be made one line function if we pass pointer to sum.

## C++

 `# include ` `using` `namespace` `std; ` `/* Function to get sum of digits */` `class` `gfg ` `{ ` ` ``public``: ` ` ``int` `getSum(``int` `n) ` ` ``{ ` `    ``int` `sum; ` ` `  `    ``/* Single line that calculates sum */` `    ``for` `(sum = 0; n > 0; sum += n % 10, n /= 10); ` ` `  `    ``return` `sum; ` ` ``} ` `}; ` ` `  `//driver code ` `int` `main() ` `{ ` ` ``gfg g;    ` ` ``int` `n = 687; ` ` ``cout<< g.getSum(n); ` ` ``return` `0; ` `} ` `//This code is contributed by Soumik `

## C

 `# include ` `/* Function to get sum of digits */` `int` `getSum(``int` `n) ` `{ ` `    ``int` `sum; ` ` `  `    ``/* Single line that calculates sum */` `    ``for` `(sum = 0; n > 0; sum += n % 10, n /= 10); ` ` `  `    ``return` `sum; ` `} ` ` `  `int` `main() ` `{ ` `  ``int` `n = 687; ` `  ``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; ` ` `  `        ``/* Single line that calculates sum */` `        ``for` `(sum = ``0``; n > ``0``; sum += n % ``10``, ` `                                  ``n /= ``10``); ` ` `  `        ``return` `sum; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `n = ``687``; ` ` `  `        ``System.out.println(getSum(n)); ` `    ``} ` `} ` ` `  `// This code is contributed by Gitanjali `

## Python3

 `# Function to get sum of digits  ` ` `  `def` `getSum(n): ` ` `  `    ``sum` `=` `0` ` `  `    ``# Single line that calculates sum  ` `    ``while``(n > ``0``): ` `        ``sum` `+``=` `int``(n``%``10``) ` `        ``n ``=` `int``(n``/``10``) ` ` `  `    ``return` `sum` ` `  `# Driver code ` ` `  `n ``=` `687` `print``(getSum(n)) ` ` `  `# This code is contributed by ` `# Smitha Dinesh Semwal `

## C#

 `// C# program to compute  ` `// sum of digits in number. ` `using` `System; ` ` `  `class` `GFG ` `{ ` `    ``static` `int` `getSum(``int` `n) ` `    ``{ ` `        ``int` `sum; ` ` `  `        ``/* Single line that calculates sum */` `        ``for` `(sum = 0; n > 0; sum += n % 10, ` `                                ``n /= 10); ` ` `  `        ``return` `sum; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 687; ` `        ``Console.Write(getSum(n)); ` `    ``} ` `} ` ` `  `// This code is contributed by Sam007 `

## PHP

 ` 0; ``\$sum` `+= ``\$n` `% 10,  ` `                                  ``\$n` `/= 10); ` `    ``return` `\$sum``; ` `} ` ` `  `// Driver Code ` `\$n` `= 687; ` `echo``(getsum(``\$n``)); ` ` `  `// This code is contributed by  ` `// Smitha Dinesh Semwal. ` `?> `

Output :

`21`

2. Recursive
Thanks to ayesha for providing the below recursive solution.

## C++

 `#include ` `using` `namespace` `std; ` `class` `gfg ` `{ ` ` ``public``: ``int` `sumDigits(``int` `no) ` ` ``{ ` `   ``return` `no == 0 ? 0 : no%10 + sumDigits(no/10) ; ` ` ``} ` `}; ` ` `  `//driver code ` `int` `main(``void``) ` `{ ` `    ``gfg g; ` `    ``cout<

## C

 `int` `sumDigits(``int` `no) ` `{ ` `   ``return` `no == 0 ? 0 : no%10 + sumDigits(no/10) ; ` `} ` ` `  `int` `main(``void``) ` `{ ` `    ``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) ` `    ``{ ` `        ``return` `no == ``0` `? ``0` `: no%``10` `+  ` `                  ``sumDigits(no/``10``) ; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `    ``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 ` `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() ` `    ``{ ` `        ``Console.Write(sumDigits(687)); ` `    ``} ` `} ` ` `  `// This code is contributed by Sam007 `

## PHP

 ` `

Output :

`21`

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

My Personal Notes arrow_drop_up

Improved By : jit_t, SoM15242, RishabhPrabhu

Article Tags :
Practice Tags :

8

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.