fork(1) download
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. /* varialbe definition */
  6. int count, value, sum, stop;
  7. double avg;
  8.  
  9. /* Initialize */
  10. count = 0;
  11. sum = 0;
  12. avg = 0.0;
  13. stop = -999;
  14.  
  15. // Loop through to input values
  16. printf("Enter -999 to end the loop\n");
  17. scanf("%d", &value);
  18. while (value != stop)
  19. {
  20. printf("Enter a positive integer\n");
  21. scanf("%d", &value);
  22. if (value >=0 && value != stop) {
  23. sum = sum + value;
  24. count = count + 1;
  25. }
  26. if (value = -999) {
  27. printf("Loop has ended\n");
  28. }
  29. else {
  30. printf("Value must be positive\n");
  31. }
  32. }
  33. // Calculate avg. Need to type cast since two integers will yeild an integer
  34.  
  35. avg = (double) sum/count;
  36. printf("average is %f\n " , avg );
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0s 9432KB
stdin
150
130
900
340
-999
stdout
Enter -999 to end the loop
Enter a positive integer
Loop has ended
average is 130.000000