Follow the below steps to solve the problem:
- Get the number
- Get the remainder and pass the next remaining digits
- Get the rightmost digit of the number with help of the remainder ‘%’ operator by dividing it by 10 and adding it to the sum.
- Divide the number by 10 with help of the ‘/’ operator to remove the rightmost digit.
- Check the base case with n = 0
- Print or return the sum
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
class gfg {
public :
int sumDigits( int no)
{
if (no == 0) {
return 0;
}
return (no % 10) + sumDigits(no / 10);
}
};
int main( void )
{
gfg g;
cout << g.sumDigits(687);
return 0;
}
|
C
#include <stdio.h>
int sumDigits( int no)
{
if (no == 0) {
return 0;
}
return (no % 10) + sumDigits(no / 10);
}
int main()
{
printf ( "%d" , sumDigits(687));
return 0;
}
|
Java
import java.io.*;
class GFG {
static int sumDigits( int no)
{
if (no == 0 ) {
return 0 ;
}
return (no % 10 ) + sumDigits(no / 10 );
}
public static void main(String[] args)
{
System.out.println(sumDigits( 687 ));
}
}
|
Python3
def sumDigits(no):
return 0 if no = = 0 else int (no % 10 ) + sumDigits( int (no / 10 ))
if __name__ = = "__main__" :
print (sumDigits( 687 ))
|
C#
using System;
class GFG {
static int sumDigits( int no)
{
return no == 0 ? 0 : no % 10 + sumDigits(no / 10);
}
public static void Main()
{
Console.Write(sumDigits(687));
}
}
|
PHP
<?php
function sumDigits( $no )
{
return $no == 0 ? 0 : $no % 10 +
sumDigits( $no / 10) ;
}
echo sumDigits(687);
?>
|
Javascript
<script>
function sumDigits(no)
{
if (no == 0){
return 0 ;
}
return (no % 10) + sumDigits(parseInt(no/10)) ;
}
document.write(sumDigits(687));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(log N)
Program for Sum of the digits of a given number
Given a number, find the sum of its digits.
Input: n = 687
Output: 21
Input: n = 12
Output: 3
Follow the below steps to solve the problem:
- Get the number
- Declare a variable to store the sum and set it to 0
- Repeat the next two steps till the number is not 0
- Get the rightmost digit of the number with help of the remainder ‘%’ operator by dividing it by 10 and adding it to the sum.
- Divide the number by 10 with help of ‘/’ operator to remove the rightmost digit.
- Print or return the sum
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
class gfg {
public :
int getSum( int n)
{
int sum = 0;
while (n != 0) {
sum = sum + n % 10;
n = n / 10;
}
return sum;
}
};
int main()
{
gfg g;
int n = 687;
cout << g.getSum(n);
return 0;
}
|
C
#include <stdio.h>
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
import java.io.*;
class GFG {
static int getSum( int n)
{
int sum = 0 ;
while (n != 0 ) {
sum = sum + n % 10 ;
n = n / 10 ;
}
return sum;
}
public static void main(String[] args)
{
int n = 687 ;
System.out.println(getSum(n));
}
}
|
Python3
def getSum(n):
sum = 0
while (n ! = 0 ):
sum = sum + int (n % 10 )
n = int (n / 10 )
return sum
if __name__ = = "__main__" :
n = 687
print (getSum(n))
|
C#
using System;
class GFG {
static int getSum( int n)
{
int sum = 0;
while (n != 0) {
sum = sum + n % 10;
n = n / 10;
}
return sum;
}
public static void Main()
{
int n = 687;
Console.Write(getSum(n));
}
}
|
PHP
<?php
function getsum( $n )
{
$sum = 0;
while ( $n != 0)
{
$sum = $sum + $n % 10;
$n = $n /10;
}
return $sum ;
}
$n = 687;
$res = getsum( $n );
echo ( "$res" );
?>
|
Javascript
<script>
function getSum(n)
{
var sum = 0;
while (n != 0) {
sum = sum + n % 10;
n = parseInt(n / 10);
}
return sum;
}
var n = 687;
document.write(getSum(n));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)