I am trying to find a way to use loops in order to calculate the result of every combination of 3 operators and 4 integers, and I was wondering if there is a way to have a loop run 4 times, each time using a different operator.
It's intended to work something like this, but I'm not sure how possible it is.
int a = 2;
int b = 3;
for (int i = 0; i < 4; i++) {
if (i == 0) {
operator = '+';
} else if (i == 1) {
operator = '-';
} else if (i == 2) {
operator = '*';
} else if (i == 3) {
operator = '/';
}
printf("Result: %d\n", a operator b);
}
And the output would print results of 5, -1, 6, and 0...
But as I have read, operators cannot be put into variables, which is where I'm stuck.
If anyone could give me some insight as to how I could get around this, it would be greatly appreciated!