#include <iostream>
#include <string>
using namespace std;
int square(int x) { return x * x; }
int cube(int x) { return x * x * x; }
void apply(int x, int (*op)(int)) {
cout << op(x);
}
int main() {
int n;
string opName;
cin >> n >> opName;
if (opName == "square") {
apply(n, square);
} else if (opName == "cube") {
apply(n, cube);
}
return 0;
}
Solution Details
👉 In simple words:
Think of apply as a box where you put both a number and an instruction (square or cube). The box then gives you the result.
Significance for Embedded Developers
Input
5 square
Expected Output
25