Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- static int rec(int x) { // rekurzivna funkcija koja go vraka zbirot od cifrite na eden broj
- if(x == 0) {
- return 0;
- }
- int cifra = x % 10;
- return rec(x / 10) + cifra;
- }
- public static void main(String[] args) {
- int n = 156;
- System.out.println(rec(n));
- }
- /*
- * rec(156) --> rec(15) + 6 = 6 + 6 = 12
- * rec(15) --> rec(1) + 5 = 1 + 5 = 6
- * rec(1) --> rec(0) + 1 = 0 + 1 = 1
- * rec(0) = 0
- * */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement