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