program .. determine if the year is leap or not

hi

Problem :
----------------------------------------------------------------------------
write a program that reads a year and determin and print whtether itis leap year or not
the screen dialogue should appear as follows :
) enter a year :1999 )
( this is not a leap year )

) enter a year :2000)
) this is a leap year )
-----------------------------------------------------------------------------

its easy but the problem is that I do not know what is the condition of the if !!

Here is my program:
--------------------------------------

#include <iostream>
using namespace std;
int main ()
{
int year;
cout<<"Enter a year: ";
cin>>year;
if (year ????????)
cout<<"this is a leap year\n";
else
cout<<"this is not a leap year\n"
return 0;
}
--------------

I put ???????? in the if condition because i do not know what i should write here
help please
am given a hint: use the modulus operator.
but am lost .. what should i do ??
Here's two hints:
1. How often does a leap year occur?
2. What does the mod operator do?

Now, the answers:
1. Every 4 years
2. Returns the remainder of a / b; e.g.
44 % 2 == 0 because 44 is evenly divisble by 2.

Put that together and see what you get.
I got it. :)

if (year%4 == 0)
cout<<"this is a leap year\n";
else
cout<<"Thos is not a leap year \n";


Right?
chrisname wrote:
How often does a leap year occur?
...
Every 4 years
Not really...
http://en.wikipedia.org/wiki/Leap_year#Algorithm
@General,
Yes, that's how you use the modulo operator; but according to wikipedia (thanks, Bazzy) a leap year really occurs when the year is divisble by four, but not 100 (unless it's divisible by 400). Modify your code accordingly. Good luck.
thanks. :)
that's still not right. You have to test more than one condition. A year is a leap year if it's divisible by 4 and not divisible by 100 except when it's divisible by 400.
#include <iostream>
using namespace std;
int main ()
{
int year;
cout<<"Enter a year: ";
cin>>year;
if (year%4==0 && year%100!=0 || year%400==0)
cout<<"this is a leap year\n";
else
cout<<"this is not a leap year\n";
return 0;
}

here the conditions for the leap year is satisfied...
Last edited on
Topic archived. No new replies allowed.