fork(1) download
  1. #include <stdio.h>
  2.  
  3. double avgup(int n, int total, int count) {
  4. total += n;
  5. count++;
  6. return (double)total / count;
  7. }
  8.  
  9. int main(void) {
  10. int num;
  11. int total = 0;
  12. int count = 0;
  13. double avg;
  14.  
  15. while (1) {
  16. printf("非負の整数を入力してください(負の数で終了):");
  17. scanf("%d", &num);
  18.  
  19. if (num < 0) break;
  20.  
  21. avg = avgup(num, total, count);
  22. total += num;
  23. count++;
  24.  
  25. printf("現在の平均値:%.2f\n", avg);
  26. }
  27.  
  28. printf("終了します。\n");
  29. return 0;
  30. }
Success #stdin #stdout 0s 5324KB
stdin
10 15 30 -10
stdout
非負の整数を入力してください(負の数で終了):現在の平均値:10.00
非負の整数を入力してください(負の数で終了):現在の平均値:12.50
非負の整数を入力してください(負の数で終了):現在の平均値:18.33
非負の整数を入力してください(負の数で終了):終了します。