Advertisement
Josif_tepe

Untitled

Jun 26th, 2023
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.55 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4.  
  5.  
  6. public class Main {
  7.     static int rec(int x) { // rekurzivna funkcija koja go vraka zbirot od cifrite na eden broj
  8.         if(x == 0) {
  9.             return 0;
  10.         }
  11.         int cifra = x % 10;
  12.         return rec(x / 10) + cifra;
  13.     }
  14.     public static void main(String[] args) {
  15.         int n = 156;
  16.         System.out.println(rec(n));
  17.  
  18.     }
  19.     /*
  20.     * rec(156) --> rec(15) + 6 = 6 + 6 = 12
  21.     * rec(15) --> rec(1) + 5 = 1 + 5  = 6
  22.     * rec(1) --> rec(0) + 1 = 0 + 1 = 1
  23.     * rec(0) = 0
  24.     * */
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement