fork download
  1. // C code
  2. // This program will calculate the sum of 20 positive integers.
  3. // Developer: Kristina Nguyen
  4. // Date: September 21, 2017
  5.  
  6. #include <stdio.h>
  7.  
  8. int main ()
  9. {
  10. /* variable definition: */
  11. int count, value, sum, count1;
  12. double avg;
  13. /* Initialize */
  14. count = 0;
  15. sum = 0;
  16. avg = 0.0;
  17. count1 = 0;
  18. printf("How many numbers do you want to sum?\n");
  19. scanf("%d", &count1);
  20. // Loop through to input values
  21. while (count < count1)
  22. {
  23. printf("Enter a positive Integer\n");
  24. scanf("%d", &value);
  25. if (value >= 0)
  26. { sum = sum + value; count = count + 1;}
  27. else {
  28. printf("Value must be positive\n");}
  29. }
  30. // Calculate avg. Need to type cast since two integers will yield an integer
  31. avg = (double) sum/count;
  32. printf("average is %lf\n " , avg );
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0s 10320KB
stdin
2
1
2
stdout
How many numbers do you want to sum?
Enter a positive Integer
Enter a positive Integer
average is 1.500000