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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | #include <iostream> using namespace std; // Function to check if a year is a leap year using pass-by-value bool isLeapYearByValue(int year) { if (year % 4 == 0) { if (year % 100 == 0) { if (year % 400 == 0) { return true; // Leap year } else { return false; // Not a leap year } } else { return true; // Leap year } } else { return false; // Not a leap year } } // Function to check if a year is a leap year using pass-by-reference void isLeapYearByReference(int year, bool &isLeapYear) { if (year % 4 == 0) { if (year % 100 == 0) { if (year % 400 == 0) { isLeapYear = true; // Leap year } else { isLeapYear = false; // Not a leap year } } else { isLeapYear = true; // Leap year } } else { isLeapYear = false; // Not a leap year } } int main() { int year; bool isLeapYearByRef; // Input the year cout << "Enter a year: "; cin >> year; // Check if the year is a leap year using pass-by-value bool isLeapByVal = isLeapYearByValue(year); cout << "Using pass-by-value: "; if (isLeapByVal) { cout << year << " is a leap year." << endl; } else { cout << year << " is not a leap year." << endl; } // Check if the year is a leap year using pass-by-reference isLeapYearByReference(year, isLeapYearByRef); cout << "Using pass-by-reference: "; if (isLeapYearByRef) { cout << year << " is a leap year." << endl; } else { cout << year << " is not a leap year." << endl; } return 0; } |
Explanation:
- Pass-by-Value (
isLeapYearByValue
):- Function Definition:
bool isLeapYearByValue(int year)
- This function takes the year as a parameter by value and checks whether it is a leap year.
- It uses nested conditions:
- If the year is divisible by 4, it proceeds to check divisibility by 100.
- If divisible by 100, it further checks divisibility by 400.
- Returns
true
if all conditions for a leap year are met; otherwise, it returnsfalse
.
- Function Definition:
- Pass-by-Reference (
isLeapYearByReference
):- Function Definition:
void isLeapYearByReference(int year, bool &isLeapYear)
- This function takes the year as a parameter by value but uses a reference parameter
isLeapYear
to store the result. - It performs the same checks as
isLeapYearByValue
but updates theisLeapYear
reference variable with the result.
- Function Definition:
- Main Function (
main
):- The program prompts the user to input a year.
- It calculates and displays whether the year is a leap year using both methods.
Example Outputs:
Example 1:
Input:
- Year:
2024
Output:
1 2 3 | Enter a year: 2024 Using pass-by-value: 2024 is a leap year. Using pass-by-reference: 2024 is a leap year. |
Example 2:
Input:
- Year:
1900
Output:
1 2 3 | Enter a year: 1900 Using pass-by-value: 1900 is not a leap year. Using pass-by-reference: 1900 is not a leap year. |
Example 3:
Input:
- Year:
2000
Output:
1 2 3 | Enter a year: 2000 Using pass-by-value: 2000 is a leap year. Using pass-by-reference: 2000 is a leap year. |