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