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