C programming - do while loop help (code almost done) -
i have created code writes out sum of numbers. every time loops sum of last run saved , added in sum of new run. how make have new loop?
sorry bad english, , thx in advance
int main() { int number = 0; int sum = 0; printf("welcome to\"sum evens\"!"); { printf("\ninput number: "); scanf(" %d", &number); if (number == 0) { printf("goodbye, have nice day!\n"); break; } printf("\nsum:"); if (number % 2 != 0) { number -= 1; } (int = 0; <= number; += 2) { printf(" %d ", i); if (i != number) { printf("+"); } sum += i; } printf("= %d\n", sum); } while (number != 0); system("pause"); return 0; }
watch these sort of problems melt away if declare variables close first use possible.
in case, move int sum = 0;
before for
loop.
Comments
Post a Comment