recategorized by
393 views
0 votes
0 votes
Write a complete ANSI C code using recursion to calculate the $sum(s)$ of the digits of an integer number (i) consisting of maximum 5 digits. For example, (1) = if $i=12345$, then your program should print $s=15$, (2) if $ i=457$, then $s=16$.
recategorized by

2 Answers

Best answer
2 votes
2 votes
int calculateSum(int n) {
    if (n == 0) {
        return 0;
    }
    return (n%10) + calculateSum(n/10);
}

 

selected by
0 votes
0 votes
int sum(int n){
    if(n<=9) return n ; 
    return n%10 + sum(n/10);
}

 

Related questions

1 votes
1 votes
1 answer
1
akash.dinkar12 asked Apr 8, 2019
496 views
Write a $C$ program to fins all permutations of a string (having at most 6 characters). For example, a string of $3$ characters like $“abc"$ has 6 possible permutations...
1 votes
1 votes
1 answer
3
go_editor asked Sep 20, 2018
1,583 views
An operating system contains three resource classes. The number of resource units in these classes are $7, 7\ \text{and} \ 10$ respectively. The current resource allocati...
1 votes
1 votes
1 answer
4
go_editor asked Sep 20, 2018
426 views
Show that $\{1,A \bar{B}\}$ is functionality complete, i.e., any Boolean function with variables $A$ and $B$ can be expressed using these two primitives.