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