42. Inline Math Operation

#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;

    auto square = [](int x) { return x * x; };

    cout << "Square=" << square(n);
    return 0;
}

 

Solution Details

  • The lambda [](int x) { return x * x; } is assigned to square.
     
  • It behaves like a function but is written inline inside main().
     
  • The program then calls square(n) to get the result.
     

👉 In simple words:
 It’s like writing a tiny throwaway function right where you need it, instead of defining one above main().

 

Significance for Embedded Developers

  • Lambdas make code concise — useful when writing quick transforms, callbacks, or filters in firmware.
     
  • Example: scaling ADC values, handling short event logic in ISRs, or configuring drivers without extra global functions.

     
Loading...

Input

5

Expected Output

Square=25