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