fork download
  1. // C code
  2. // This program will calculate the sum of 10 positive integers.
  3. // Developer: Daniel Toueg
  4. // Date: September 19, 2018
  5. #include <stdio.h>
  6. int main ()
  7. {
  8. /* variable definition: */
  9. float count, value, sum;
  10. double avg;
  11. /* Initialize */
  12. count = 0;
  13. sum = 0;
  14. avg = 0.0;
  15. // Loop through to input values
  16. while (count < 10)
  17. {
  18. printf("Enter a positive decimal\n");
  19. scanf("%f", &value);
  20. if (value >= 0) {
  21. sum = sum + value;
  22. count = count + 1;
  23. }
  24. else {
  25. printf("Value must be positive\n");
  26. }
  27. }
  28. // Calculate avg. Need to type cast since two decimals will yield a decimal
  29. avg = (double) sum/count;
  30. printf("average is %lf\n " , avg );
  31. return 0;
  32. }
  33.  
Success #stdin #stdout 0s 9424KB
stdin
1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0
10.0
stdout
Enter a positive decimal
Enter a positive decimal
Enter a positive decimal
Enter a positive decimal
Enter a positive decimal
Enter a positive decimal
Enter a positive decimal
Enter a positive decimal
Enter a positive decimal
Enter a positive decimal
average is 5.500000