fork download
  1. #include <stdio.h>
  2.  
  3. int main ()
  4. {
  5.  
  6. /* variable definition: */
  7. int count, value, sum;
  8. double avg;
  9.  
  10. /* Initialize */
  11. count = 0;
  12. sum = 0;
  13. avg = 0.0;
  14. // Loop through to input values
  15. while (count < 1)
  16. {
  17. printf("Enter a positive Integer\n");
  18. scanf("%d", &value);
  19.  
  20. if (value >= 0)
  21. {
  22. sum = sum + value;
  23. count = count + 1;
  24. }
  25. else {
  26. printf("Value must be positive\n");
  27. }
  28. }
  29. // Calculate avg. Need to type cast since two integers will yield an integer
  30. avg = (double) sum/count;
  31. printf("average is %lf\n " , avg );
  32.  
  33. return 0;
  34. }
Success #stdin #stdout 0s 9432KB
stdin
string
stdout
Enter a positive Integer
average is 11020.000000