fork download
  1. #include <stdio.h>
  2. //正の奇数nを引数とし,以下の正の奇数の和を求める関数
  3. int rec(int n){
  4. if (n%2==0){
  5. if(n==2){
  6. return 2;
  7. }else{
  8. return n+rec(n-2);
  9. }
  10. }
  11. else{
  12. if(n==1){
  13. return 1;
  14. }
  15. else{
  16. return rec(n-2)+n;
  17.  
  18. }
  19.  
  20. }
  21.  
  22. }
  23.  
  24. int main(void) {
  25. int n = 11;
  26. printf("%d以下の正の奇数の和は%d\n", n, rec(n));
  27. return 0;
  28. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
11以下の正の奇数の和は36