Search
 
SCRIPT & CODE EXAMPLE
 

CPP

leap year c++

#include <iostream>
using namespace std;

int main() {

  int year;

  cout << "Enter a year: ";
  cin >> year;

  // if year is divisible by 4 AND not divisible by 100
  // OR if year is divisible by 400
  // then it is a leap 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;
}
Comment

c++ Program to check if a given year is leap year

// C++ program to check if a given
// year is leap year or not
#include <bits/stdc++.h>
using namespace std;
 
bool checkYear(int year)
{
    // If a year is multiple of 400,
    // then it is a leap year
    if (year % 400 == 0)
        return true;
 
    // Else If a year is multiple of 100,
    // then it is not a leap year
    if (year % 100 == 0)
        return false;
 
    // Else If a year is multiple of 4,
    // then it is a leap year
    if (year % 4 == 0)
        return true;
    return false;
}
 
// Driver code
int main()
{
    int year = 2000;
 
    checkYear(year) ? cout << "Leap Year":
                      cout << "Not a Leap Year";
    return 0;
}
 
// This is code is contributed
// by rathbhupendra
Comment

PREVIOUS NEXT
Code Example
Cpp :: sum of a matrix c++ 
Cpp :: dynamic allocation c++ 
Cpp :: c++ in cmd 
Cpp :: check if element in dict c++ 
Cpp :: matrix dynamic memory c++ 
Cpp :: getline 
Cpp :: std::copy C ++ 
Cpp :: check if a key is in map c++ 
Cpp :: c ++ splitlines 
Cpp :: how to generate number in c++ 
Cpp :: find a number in vector c++ 
Cpp :: getline() 
Cpp :: initialize vector 
Cpp :: c++ how to return an empty vector 
Cpp :: inheritance example in C plus plus 
Cpp :: c++ array pointer 
Cpp :: standard template library in c++ 
Cpp :: linux c++ sigint handler 
Cpp :: convert wchar_t to to multibyte 
Cpp :: how to access a vector member by its index 
Cpp :: cpp ignore warning in line 
Cpp :: adding variables c++ 
Cpp :: opencv compile c++ 
Cpp :: c++ program to convert fahrenheit to celsius 
Cpp :: move assignment operator c++ 
Cpp :: c++ print array of arrays with pointer 
Cpp :: delete c++ 
Cpp :: c++ function pointer as variable 
Cpp :: build a prefix array cpp 
Cpp :: c++ last element of vector 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =