Advertisement
BorjanCrvenkov

rekurzija zadaca 5 so 1 funkcija

Dec 22nd, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. /*
  2. За даден број n, да се напише рекурзивна функција која ќе ги изброи
  3. појавувањата на цифрата 8. Притоа, доколку до некоја цифра 8 има уште една
  4. цифра 8 веднаш лево од неа, нејзиното појавување се брои двојно
  5. Пример:
  6. count8(8) -> 1
  7. count8(818) -> 2
  8. count8(8818) -> 4
  9.  
  10. */
  11.  
  12. #include <stdio.h>
  13.  
  14. int count8(int broj,int predcifra){
  15. if(broj==0){
  16. return 0;
  17. }
  18. else{
  19. if(broj%10!=8){
  20. return 0+count8(broj/10,broj%10);
  21. }
  22. if(broj%10==8 && predcifra==8){
  23. return 2+count8(broj/10,broj%10);
  24. }
  25. if(broj%10==8){
  26. return 1+count8(broj/10,broj%10);
  27. }
  28. }
  29. }
  30.  
  31. int main()
  32. {
  33. int a;
  34. scanf("%d",&a);
  35. printf("%d",count8(a,0));
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement