I'm very new to programming so forgive if this question is a bit stupid. Anyway I'm making this console program that is supposed to calculate total damage per hit after bonus damage is applied. Example: damage is 100 per hit with 0 initial bonus damage that increases by 50 per hit. The program is supposed to calculate the total damage after N amounts of hits.
This is what I came up with:
#include <stdio.h>
int main(){
int n;
int bonusDam = 0;
int i;
int b;
int a;
scanf("%d", &n);
for (i = 1; i <= n; i ){
b = 100 bonusDam;
bonusDam = bonusDam 50;
printf("Hit %d : %d\n", i, b);
}
return 0;
}
I figured out how to calculate the bonus damage but not the total damage after N amounts of hits. Is a for loop a good idea or no? If i input 3 it'll output "100, 150, 200" but what I want to do is to add them all up like "100 150 200 = 450" Where in the end the console only shows "450"
CodePudding user response:
I just want to add that there is no need for a for loop to calculate the final result
int b = 100 * n 50 * n * (n - 1) / 2;
CodePudding user response:
One easy solution is to set b = 0;
right before the loop and replace
b = 100 bonusDam;
with
b = b 100 bonusDam;
This will effectively turn b into the total damage, by accumulating the value after every iteration. By moving printf("Hit %d : %d\n", i, b);
outside of the loop, you should get the result you want.