#include#include using namespace std; int main() { auto now = chrono::system_clock::now(); auto now_c = chrono::system_clock::to_time_t(now); tm* parts = localtime(&now_c); int current_year = parts->tm_year + 1900; // get current year int birth_year; cout << "Enter your birth year: "; cin >> birth_year; int age = current_year - birth_year; cout << "Your age is: " << age << endl; return 0; }
#includeusing namespace std; bool isLeapYear(int year) { if (year % 4 == 0) { if (year % 100 == 0) { if (year % 400 == 0) { return true; } else { return false; } } else { return true; } } else { return false; } } int main() { int year; cout << "Enter a year: "; cin >> year; if (isLeapYear(year)) { cout << year << " is a leap year" << endl; } else { cout << year << " is not a leap year" << endl; } return 0; }
#includeThis example prints all the leap years between a given range using a function that checks for leap years. Library: stdio, stdlibusing namespace std; void printLeapYears(int start_year, int end_year) { for (int year = start_year; year <= end_year; year++) { if (year % 4 == 0) { if (year % 100 == 0) { if (year % 400 == 0) { cout << year << endl; } } else { cout << year << endl; } } } } int main() { int start_year, end_year; cout << "Enter start year: "; cin >> start_year; cout << "Enter end year: "; cin >> end_year; cout << "Leap years between " << start_year << " and " << end_year << " are:" << endl; printLeapYears(start_year, end_year); return 0; }