fork download
  1. //CMIS102 Hands-On Lab 5
  2. //Exercise 4: What happens if you entered a value other than an integer?
  3. //Laura Sanglas
  4. //Date: June 26 2016
  5.  
  6. #include <stdio.h>
  7. int main ()
  8. {
  9. //Introducing a new variable, lastcount
  10. int count, value, sum, n;
  11. double avg;
  12.  
  13. /* Initialize */
  14. count = 0;
  15. sum = 0;
  16. avg = 0.0;
  17.  
  18. //Prompt user number of values that will be introduced
  19.  
  20. printf("Enter number of values to be introduced");
  21. scanf("%d", &n);
  22. printf("\n%d values to be introduced", n);
  23.  
  24. // Loop through to input values
  25. while (count < n){
  26.  
  27. printf("\nEnter a positive Integer");
  28. scanf("%d", &value);
  29.  
  30. if (value >= 0) {
  31. sum = sum + value; count = count + 1;
  32. }
  33.  
  34. else {
  35. printf("Value must be positive\n");
  36.  
  37. }
  38. }
  39.  
  40.  
  41. avg = (double) sum/count;
  42.  
  43. printf("\n\naverage is %lf\n " , avg );
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0s 2164KB
stdin
5
1
2
3
4
5
stdout
Enter number of values to be introduced
5 values to be introduced
Enter a positive Integer
Enter a positive Integer
Enter a positive Integer
Enter a positive Integer
Enter a positive Integer

average is 3.000000