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