This is what I got so far in my program:
When I compile I get no errors. When I run the executable though, the value of sum is one less than it should be, which in turn makes avg off as well. Where is my logic wrong?Code:#include <stdio.h> #include <stdlib.h> void main (void) { int num, sum, valid, invalid; float avg; printf("Please enter a positive value or -1 to terminate the program:"); scanf("%d", &num); while (num!=-1) { sum=num+num; valid++; printf("Please enter a positive value or -1 to terminate the program:"); scanf("%d", &num); } avg=sum/(float)valid; printf("sum=%d\n", sum); printf("valid=%d\n", valid); printf("avg=%.2f\n", avg); }



LinkBack URL
About LinkBacks


