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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
#include <iostream> using namespace std; // Function to check if a number is even or odd using pass-by-value void checkEvenOddByValue(int num) { if (num % 2 == 0) { cout << "The number " << num << " is even (By Value)." << endl; } else { cout << "The number " << num << " is odd (By Value)." << endl; } } // Function to check if a number is even or odd using pass-by-reference void checkEvenOddByReference(int &num) { if (num % 2 == 0) { cout << "The number " << num << " is even (By Reference)." << endl; } else { cout << "The number " << num << " is odd (By Reference)." << endl; } } int main() { int number; // Label for the goto statement start: cout << "Enter a number with at least 2 digits: "; cin >> number; // Check if the number has fewer than 2 digits if (number >= -9 && number <= 9) { cout << "Please enter a number with at least 2 digits." << endl; goto start; // Go back to the start label to re-enter the value } // Check even/odd using pass-by-value cout << "=== Pass-by-Value Check ===" << endl; checkEvenOddByValue(number); // Check even/odd using pass-by-reference cout << "\n=== Pass-by-Reference Check ===" << endl; checkEvenOddByReference(number); return 0; } |
Explanation
1. Pass-by-Value (checkEvenOddByValue
Function)
- Definition:
1void checkEvenOddByValue(int num)
- 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.
- The function checks if the number is even or odd using the modulus operator (
- Effect:
- The original value of the number remains unchanged after the function call.
2. Pass-by-Reference (checkEvenOddByReference
Function)
- Definition:
1void checkEvenOddByReference(int &num)
- 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.
- Label: The
Sample Output:
1 2 3 4 5 6 7 8 |
Enter a number with at least 2 digits: 8 Please enter a number with at least 2 digits. Enter a number with at least 2 digits: 23 === Pass-by-Value Check === The number 23 is odd (By Value). === Pass-by-Reference Check === The number 23 is odd (By Reference). |
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.
- The program checks whether a number is even or odd using the modulus operator (
- 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.
- The program uses a