fork download
  1. #include <stdio.h>
  2. //課題B-1: 1からnまでの和を求めるコードを完成させてください
  3.  
  4. int sum(int n){
  5. int x = 0;
  6. while( n > 0 )
  7. //(n-->0)にすると、最初の足し算からnの値が1少ない値になるので、最後にn--をくっつける
  8. {
  9. x = x+n;
  10. n--;
  11. }
  12. return x;
  13. }
  14.  
  15. int main(void) {
  16. int a,b;
  17. a = 10;
  18. b = sum(a);
  19. printf("1から%dまでの和は%d", a,b );
  20. return 0;
  21. }
  22.  
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
1から10までの和は55