fork download
  1. #include <stdio.h>
  2.  
  3. // 再帰を用いて 計算する
  4. int rec(int n){
  5. if (n == 1){
  6. return 1; // 基底条件1
  7. } else {
  8. return rec(n - 1) + n*n; // 再帰式
  9. }
  10. }
  11.  
  12. int main(void) {
  13.  
  14. for (int n = 1; n <= 4; n++){
  15. printf("1から%dまでの二乗和の値は%d\n", n, rec(n));
  16. }
  17. return 0;
  18. }
  19.  
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
1から1までの二乗和の値は1
1から2までの二乗和の値は5
1から3までの二乗和の値は14
1から4までの二乗和の値は30