Having problems with writing the code..

The program is to determine if the year the user enter is a leap year. If the year is 1582 or before and divisible by 4 it is a leap year..if it is after 1582 and divisible by 400 it is a leap year..if the year is after 1582 and divisible by 100 but not 400 it is NOT a leap year..if the year is after 1582 and it is divisible by 4 but not 100 it is not a leap year..other wise it is not a leap year..also if the user enters a negative number display an error.

Here is the code I wrote so far..I'm having problems with the "if year is after 1582 and divisible by 100 but not 400 it is NOT a leap year"..how would I be able to write.. if the year is divisible by 100 but not by 400?


#include <iostream>
using namespace std;
int main()
{
int year;
char ans;

do
{
cout << "Please enter a year to see if it is a leap year. \n";
cin >> year;

if((year <= 1582) && (year % 4))
{
cout << "The year you entered is a leap year \n";
}

else
{
cout << "The year you entered is not a leap year \n";
}

if((year > 1582) && (year % 400))
{
cout << "The year you entered is a leap year. \n";
}
else
{
cout <<"The year you entered is not a leap year \n";
}

if((year > 1582) && (year % 100))
{
cout << "The year you entered is not a leap year. \n";

}
if(year < 0)
{
cout<< "You have entered an invalid year.\n";
}
cout << "Would you like to run the program again?\n";
cin >> ans;

}while (ans == 'Y' || ans == 'y');
return 0;
}
Last edited on
Your assumptions of leap year might be wrong for the, divisible by 4, but not 100 part.

Have you tried structuring it as

1
2
3
4
if( divisible by 4 ){
  if( after 1582 ){}
  else {}
}

Topic archived. No new replies allowed.