Find the sum of the digits of the integer n.
Input. One 32-bit integer n (the number can
be negative).
Output. Print the sum of the digits of the number n.
Sample
input |
Sample
output |
321 |
6 |
recursion
The input number n
can be negative. In this case, we’ll change its sign (the
sum of the digits of the numbers -n
and n is the same).
Let’s
implement the recursive function sum(n),
that computes the sum of the digits of number n, according to the recursive
relation:
sum(n) =
·
If n < 10, then the sum of its digits equals n;
·
otherwise, compute the sum of the digits
of the number n / 10 and add to it the last digit n % 10 of the
number n.
This problem can also be
solved using a loop (without using recursion).
Example
The function sum
computes the sum of the digits of the number n.
int sum(int n)
{
if (n <
10) return n;
return n % 10
+ sum(n / 10);
}
The main part of the program. Read the input value of n.
scanf("%d",&n);
If the number n is negative, then change its sign to the opposite.
if (n < 0) n = -n;
Compute and print the sum of the digits of the number n.
res = sum(n);
printf("%d\n",res);
Algorithm realization – iteration
Read the input value of n.
scanf("%d",&n);
If the number n is negative, then change its sign to the opposite.
if (n < 0) n = -n;
In the variable sum, find the sum of the digits of the number n.
sum = 0;
Iterate through the digits of the number n. Add each digit of n to the sum sum.
while(n > 0)
{
sum = sum + n % 10;
n = n / 10;
}
Print the answer.
printf("%d\n",sum);
Java realization
import java.util.*;
public class
{
static int sum(int n)
{
if (n < 10) return n;
return n % 10 + sum(n / 10);
}
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
int n = con.nextInt();
if (n < 0) n = -n;
int res = sum(n);
System.out.println(res);
con.close();
}
}
Python realization
n = int(input())
If the number n is
negative, then change its sign to the opposite.
if n < 0: n = -n;
Iterate over the digits of n. Add each digit of n to the
sum sum.
sum = 0
while n > 0:
sum = sum + n % 10;
n = n // 10;
Print the answer.
print(sum)
Python realization – function
The function sum computes the sum of the digits of the number n.
def sum(n):
if
n < 10: return n
return
sum(n // 10) + n % 10
The main part of the
program. Read the input value of n.
n = int(input())
If the number n is
negative, then change its sign to the opposite.
if n < 0: n =
-n;
Compute and print the sum
of the digits of the number n.
print(sum(n))
Python realization – string
Read the input value of n.
n = int(input())
If the number n is
negative, then change its sign to the opposite.
if n < 0 : n = -n
Convert the integer number n to a string: str(n). Then from the string, create a list of digits. For example, if n = 123, then str(n) = “123” and list(map(int, str(n))) = [1, 2, 3]. The map function applies the int function to each character of the string str(n), converting each character into an integer value.
s = list(map(int,str(n)))
Print the answer, the sum of the integers in the list s.
print(sum(s))