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