I am taking a computer programming class and I need to write a program that determines if the input is a leap year. I need help writing it and if anyone would know how please answer soon.
Computer Programming? NEED HELP NOW!!!!?
a leap year is:
(divisible by 400) OR ((NOT divisible by 100) AND divisble by 4)
so e.g. in Java this would be:
public boolean isLeapYear(int year) {
return (year%400 == 0) || (year%100 != 0 %26amp;%26amp; year%4 == 0);
}
Reply:Actually, the code should read,
if (year % 4 == 0)
{
cout %26lt;%26lt; "The year is a leap year!" %26lt;%26lt; endl;
}
If the year IS a multiple of 4, performing a modulus by 4 will return 0.
Reply:int enteredYear;
int leapReference = 2000;
int leapResult = leapReference - enteredYear;
if (leapResult % 4 == 0) enteredYear " is a leap year!";
else enteredYear " is not a leap year";
The key here is that you have to find if the year is a multiple of four. Hope that helps!
edit: Thanks for catching the error, i was thinking of the else qualifier when I typed the if statement!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment