fork download
  1. #include <stdio.h>
  2.  
  3. // 課題B-1: 1からnまでの和を求めるコードを完成させてください
  4. int sum(int n){
  5. int total = 0;
  6. int i;
  7.  
  8. for(i = 1; i <= n; i++){
  9. total += i;
  10. }
  11.  
  12. return total;
  13. }
  14.  
  15. int main(void) {
  16. int a, b;
  17. a = 10;
  18. b = sum(a);
  19. printf("1から%dまでの和は%d\n", a, b);
  20. return 0;
  21. }
  22.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
1から10までの和は55