Write a C++ program in which a user enters a number, and the program will find the Even and odd number. Your program should only accept integers of length 2 or higher and should ask the user to re-enter a value if the value containing total numbers of integers less than 2. The program with goto statement. Even odd program with goto statement.

Explanation

1. Pass-by-Value (checkEvenOddByValue Function)

  • Definition:


    • Parameters: Accepts the number as an integer by value.
    • Operation:
      • The function checks if the number is even or odd using the modulus operator (%).
      • If the number is divisible by 2, it is even; otherwise, it is odd.
      • Displays the result inside the function.
    • Effect:
      • The original value of the number remains unchanged after the function call.

2. Pass-by-Reference (checkEvenOddByReference Function)

  • Definition:


    • Parameters: Accepts the number as a reference.
    • Operation:
      • The function performs the same even or odd check as in the pass-by-value version.
      • Displays the result inside the function.
    • Effect:
      • Since the number is passed by reference, any changes to it would reflect in the original variable, although in this case, the number is only read, not modified.

3. Input Validation Using goto

  • The program uses a goto statement to control the flow based on input validation.
    • Label: The start label marks the point to return to if the user needs to re-enter the number.
    • Condition: The program checks if the entered number has fewer than 2 digits (i.e., if it’s between -9 and 9).
    • Flow: If the condition is true, the program prompts the user to re-enter the value by jumping back to the start label.

Sample Output:

Key Points

  • Even/Odd Check:
    • The program checks whether a number is even or odd using the modulus operator (%).
    • A number is even if it is divisible by 2; otherwise, it is odd.
  • Pass-by-Value:
    • The function receives a copy of the actual argument.
    • Changes made inside the function do not affect the original variable.
  • Pass-by-Reference:
    • The function receives a reference (alias) to the actual argument.
    • Any modifications to the variable inside the function would affect the original variable.
  • Input Validation with goto:
    • The program uses a goto statement to ensure that the user enters a valid number with at least 2 digits.
    • The user is prompted to re-enter the value if the input does not meet the requirement.