fork download
  1. #include <stdio.h>
  2.  
  3. int main ()
  4.  
  5. {
  6.  
  7. /* variable definition: */
  8.  
  9. int count, value, sum;
  10. double avg;
  11.  
  12. /* Initialize */
  13.  
  14. count = 0;
  15.  
  16. sum = 0;
  17. avg = 0.0;
  18.  
  19. // Loop through to input values
  20.  
  21. while (count > 0)
  22.  
  23. {
  24.  
  25. printf("Enter number of integers you would like to add and find average of\n");
  26.  
  27. scanf("%d", &value);
  28. if (value >= 0) {
  29. sum = sum + value;
  30. count = count + 1;
  31. }
  32. else {
  33. printf("Value must be positive\n");
  34. }
  35.  
  36. }
  37.  
  38. // Calculate avg. Need to type cast since two integers will yield an integer
  39.  
  40. avg = (double) sum/count;
  41.  
  42. printf("average is %lf\n " , avg );
  43.  
  44. return 0;
  45.  
  46. }
Success #stdin #stdout 0s 9416KB
stdin
2
3
2
2
2
2
2
2
3
3
3
3
3
3
3
3
3
3
stdout
average is -nan