GeeksforGeeks App
Open App
Browser
Continue

Java Program for Sum the digits of a given number

Given a number, find the sum of its digits.
Example :

Input : n = 687
Output : 21

Input : n = 12
Output : 3

1. Iterative:

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`

C++

 `// Include header file``#include ``#include ``#include ` `class` `GFG``{``    ``// Function to get sum of digits``    ``public``:``    ``static` `int` `getSum(``int` `n)``    ``{``        ``int` `sum = 0;``        ``while` `(n != 0)``        ``{``            ``sum = sum + n % 10;``            ``n = n / 10;``        ``}``        ``return` `sum;``    ``}``    ``// Driver program``    ``static` `void` `main(std::vector &args)``    ``{``        ``int` `n = 687;``        ``std::cout << GFG::getSum(n) << std::endl;``    ``}``};``int` `main(``int` `argc, ``char` `**argv){``    ``std::vector parameter(argv + 1, argv + argc);``    ``GFG::main(parameter);``    ``return` `0;``};`

C#

 `// Include namespace system``using` `System;`  `public` `class` `GFG``{``    ``// Function to get sum of digits``    ``public` `static` `int` `getSum(``int` `n)``    ``{``        ``var` `sum = 0;``        ``while` `(n != 0)``        ``{``            ``sum = sum + n % 10;``            ``n = (``int``)(n / 10);``        ``}``        ``return` `sum;``    ``}``    ``// Driver program``    ``public` `static` `void` `Main(String[] args)``    ``{``        ``var` `n = 687;``        ``Console.WriteLine(GFG.getSum(n));``    ``}``}`

Output

```21
```

Time Complexity: O(|n|)
Auxiliary Space: O(1)

How to compute in single line?

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`

Output

```21
```

Time Complexity: O(|n|)
Auxiliary Space: O(1)

2. Recursive

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)``    ``{``    ``int` `n = ``687``;` `    ``System.out.println(sumDigits(n));``    ``}``}` `// This code is contributed by Gitanjali`

Output

```21
```

Time Complexity: O(log10n)
Auxiliary Space: O(log10n)

Please refer complete article on Program for Sum the digits of a given number for more details!

My Personal Notes arrow_drop_up