Advertisement
Josif_tepe

Untitled

May 25th, 2021
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. int soberi(int broj) {
  6.     if(broj == 0) {
  7.         return 0;
  8.     }
  9.     int posledna_cifra = broj % 10;
  10.     return soberi(broj / 10) + posledna_cifra;
  11. }
  12. int main() {
  13.     int n;
  14.     scanf("%d", &n);
  15.     printf("%d\n", soberi(n));
  16.     return 0;
  17. }
  18.  
  19. // soberi(123) = soberi(12) + 3 = 3 + 3 = 6
  20. // soberi(12) = soberi(1) + 2 = 1 + 2 = 3
  21. // soberi(1) = soberi(0) + 1 = 0 + 1 = 1
  22. // soberi(0) = 0
  23.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement