Tuesday 31 May 2022

Question . Write a program in c++ to find the entered year is leap year or not.

#include <iostream>

using namespace std;

int main()

{

  int year;

  cout << "Enter a year: ";

  cin >> year;

 

  // leap year if perfectly divisible by 400

  if (year % 400 == 0) {

    cout << year << " is a leap year.";

 }

  // not a leap year if divisible by 100

  // but not divisible by 400

  else if (year % 100 == 0) {

    cout << year << " is not a leap year.";

  }

  // leap year if not divisible by 100

  // but divisible by 4

  else if (year % 4 == 0) {

    cout << year << " is a leap year.";

  }

  // all other years are not leap years

  else {

    cout << year << " is not a leap year.";

  }

  return 0;

}

 

 

Output 1

Enter a year: 1900
1900 is not a leap year.

Output 2

Enter a year: 2012
2012 is a leap year.

 

No comments:

Post a Comment