In this program, you will ask the user to enter a year and check whether the year is a leap year or not.
For checking leap year. the following conditions should satisfy.
#include <iostream>
using namespace std;
int main() {
int year;
cout << "Enter a year to check leap year:";
cin >> year;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
cout << year << " is a leap year.";
}
else {
cout << year << " is not a leap year";
}
return 0;
}
Enter a year to check leap year:2000 2000 is a leap year
Enter a year to check leap year:2010 2010 is not a leap year
#include <iostream>
using namespace std;
int main() {
int year;
cout << "Enter a year to check leap year: ";
cin >> year;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
cout << year << " is a leap year.";
}
else {
cout << year << " is not a leap year.";
}
}
else {
cout << year << " is a leap year.";
}
}
else {
cout << year << " is not a leap year.";
}
return 0;
}
Enter a year to check leap year: 2020 2020 is a leap year.
#include <iostream>
using namespace std;
/* function for checking leap year. it will return true or false */
bool leapYear(int year){
bool isLeapYear = false;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
isLeapYear = true;
}
}
else isLeapYear = true;
}
return isLeapYear;
}
int main(){
int year;
cout<<"Enter a year to check leap year: ";
cin>>year;
//Calling leapYear function
bool flag = leapYear(year);
if(flag == true)
cout<<year<<" is a leap Year";
else
cout<<year<<" is not a leap Year";
return 0;
}
Enter a year to check leap year: 2024 2024 is a leap Year
Time Complexity: O(1)