Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.47 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. // write the prototype below
  4. int digitSum(int n);
  5.  
  6.  
  7. // complete the main function to read input, call functions, and display output
  8. int main(void) {
  9.   int num = 0;
  10.   scanf("%d", &num);
  11.   int result = digitSum(num);
  12.   printf("%d", result);
  13.   return 0;
  14. }
  15.  
  16. // complete the function below
  17. int digitSum(int n) {
  18.     // base case
  19.   if(n == 0){
  20.     return 0;
  21.   }
  22.     // recursive step
  23.     else {
  24.     return n%10 + digitSum(n / 10);
  25.   }
  26.  
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement