62. LED Toggle Class

Create a class LED that represents a simple LED connected to a microcontroller GPIO pin.

Your class must:

  1. Store two private data members:
    • int pin → GPIO pin number (non-negative integer)
    • int state → LED state (0 = OFF, 1 = ON)
  2. Provide a constructor that:
    • Accepts the GPIO pin number
    • Initializes the LED state to OFF
  3. Provide the following public member functions:
    • void on() → sets the LED state to 1
    • void off() → sets the LED state to 0
    • void toggle() → flips the LED state (0 ↔ 1)
    • int status() → returns the current LED state (0 or 1)
  4. In main():
    • Read three integers from input:
      • pin → GPIO pin number
      • initialState → initial LED state (0 or 1 only)
      • toggleCount → number of times to toggle the LED (≥ 0)
    • Create a LED object using the given pin
    • Set the initial LED state based on initialState
    • Call toggle() exactly toggleCount times
    • Print the final LED state (0 or 1) using status()

Example Input:

5
1
3

Example Output:

0

Explanation:
Start ON → toggle 3 times → OFF

Constraints:

  • initialState is guaranteed to be either 0 or 1
  • toggleCount is guaranteed to be a non-negative integer
  • state and pin must be private
  • Only class methods may modify state
  • Output must be exactly one integer: 0 or 1

 

 

 

Loading...

Input

5 1 3

Expected Output

0