I have created a funktion that should be able to calculate addition, multiplication and the square-root in-between 2 integers (both positive and negative). However, my code only works for positive integers.
For instance if I insert 3 and 5 it will calculate (3)+(4)+(5) = 12, but if I put in -3 and -5 the answer will be 0. The same thing happens if I put in 5 as my first integer and then 3 as my second, instead of 3 first and then 5. I can't figure out what's wrong with my code. I'd really appreciate some guidance and help with my problem!
int calculate(int num1, int num2, char op) {
int answer, x;
if (op == 'a') {
answer = 0;
for (x = num1; x <= num2; x++) {
answer += x;
}
}
if (op == 'm') {
answer = num1;
for (x = num1 + 1; x <= num2; x++) {
answer = answer * (x);
}
if (op == 's') {
answer = 0;
for (x = num1; x <= num2; x++) {
answer = answer + (x * x);
}
}
}
return answer;
}