fork download
  1. #include <stdio.h>
  2.  
  3. // 関数のプロトタイプ宣言
  4. int sum_sequence(int n);
  5.  
  6. int main() {
  7. int n, result;
  8.  
  9. // n = 4 の場合
  10. n = 4;
  11. result = sum_sequence(n);
  12. printf("n=%d の場合の合計は %d\n", n, result);
  13.  
  14. // n = 5 の場合
  15. n = 5;
  16. result = sum_sequence(n);
  17. printf("n=%d の場合の合計は %d\n", n, result);
  18.  
  19. return 0;
  20. }
  21.  
  22. // 関数の定義
  23. int sum_sequence(int n) {
  24. int total = 0;
  25. int current_term = 0;
  26. int i;
  27.  
  28. for (i = 1; i <= n; ++i) {
  29. current_term = current_term * 10 + 4;
  30. total += current_term;
  31. }
  32.  
  33. return total;
  34. }
  35.  
Success #stdin #stdout 0.01s 5260KB
stdin
Standard input is empty
stdout
n=4 の場合の合計は 4936
n=5 の場合の合計は 49380