fork download
  1. #include <stdio.h>
  2.  
  3. int rec(int n){
  4. if(n <= 0){
  5. return 0;
  6. }
  7. return n + rec(n - 2);
  8. }
  9.  
  10. int main(void) {
  11. int n = 6;
  12. int tmp = n; // 表示用にコピー
  13.  
  14. printf("rec(%d) = ", n);
  15.  
  16. // 表示用ループ
  17. for(tmp; tmp > 0; tmp -= 2){
  18. printf("%d+", tmp);
  19. }
  20.  
  21. // 計算は元の n を使う
  22. printf("=%d\n", rec(n));
  23.  
  24. return 0;
  25. }
  26.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
rec(6) = 6+4+2+=12