fork download
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. double num,sum,tot;
  6. sum = 0;
  7. tot = 0;
  8. printf("データ: ");
  9. scanf("%lf", &num);
  10.  
  11. while(num != 0)
  12. {
  13. sum+=num;
  14. tot++;
  15. printf("次のデータ: ");
  16. scanf("%lf", &num);
  17. }
  18. if(tot > 0)
  19. {
  20. double average = sum/tot;
  21. printf("\n件数:%d\n", (int)tot);
  22. printf("合計:%.6f\n", sum);
  23. printf("平均:%.6f\n", average);
  24. }
  25. else
  26. {
  27. printf("データがありません。\n");
  28. }
  29.  
  30. return 0;
  31. }
Success #stdin #stdout 0.03s 25928KB
stdin
Standard input is empty
stdout
#include <stdio.h> 

int main(void)
{
    double num,sum,tot;
    sum = 0;
    tot = 0;
    printf("データ: ");
    scanf("%lf", &num);

    while(num != 0)
    {
        sum+=num;
        tot++;
        printf("次のデータ: ");
        scanf("%lf", &num);
    }
    if(tot > 0)
    {
        double average = sum/tot;
        printf("\n件数:%d\n", (int)tot);  
        printf("合計:%.6f\n", sum); 
        printf("平均:%.6f\n", average);
    }
    else
    {
        printf("データがありません。\n");
    } 
    
    return 0;
}