Unbelievable result
-
The programme should remove the sum of squares of whole numbers Boundaries are set from 5 square to 9 square In input 5 and 9, the programme produces results 4369973 instead of 255 What is this about?
''
#include <stdio.h>
int main()
{
int min, max, sum;
printf("Input min and max interger limit: ");
scanf("%d %d", &min, &max);while (min != max) { for (int i = min*min; i <= max*max; ++i) sum = sum + i * i; printf("The sum of squares from %d to %d = %d \n", min*min,max*max,sum); printf("Input min and max interger limit again: "); scanf("%d %d", &min, &max); } printf("Well Done\n");
}
''
-
I fixed it. a few. Compare your code and figure out why and why these corrections... I mean, what's the same amount when entering the main cycle?
int main() { int min, max, sum; printf("Input min and max interger limit: "); scanf("%d %d", &min, &max);
while (min < max) { sum = 0; for (int i = min; i <= max; ++i) sum = sum + i * i; printf("The sum of squares from %d to %d = %d \n", min,max,sum); printf("Input min and max interger limit again: "); scanf("%d %d", &min, &max); } printf("Well Done\n");
}
Also corrected the cycle condition
while (min < max)
Because if you lead, say, 9 and 5 (not 5 and 9) - surprise the result:
In fact, you can just write without a cumulative cycle:
printf("The sum of squares from %d to %d = %d \n",
min,max,(max*(1 + max)(1 + 2max) - min*(1 + (2*min - 3)*min))/6);