In: Computer Science
I am trying to write a java program that determines if an inputted year is a leap year or not, but I am not able to use if else statements how would I do it. I don't need the code just advice.
/*without using if else but using ternary */
x = (year%4==0 && year%100!=0 || uear%400==0)?"Leap Year":"Not leap year";
All years which are perfectly divisible by 4 are leap years except for century years (years ending with 00) which is leap year only it is perfectly divisible by 400.
For example: 2012, 2004, 1968 etc are leap year but, 1971, 2006 etc are not leap year. Similarly, 1200, 1600, 2000, 2400 are leap years but, 1700, 1800, 1900 etc are not.
So, lets do it using if elese
if (year % 4 == 0) // check basic condition to be leap
year
{
if (year % 100 == 0) // check for century year
{
if (year % 400 == 0) // if century year must divide by 400
cout << year << " is a leap year.";//1600
else // if not then
cout << year << " is not a leap year.";
}
else // if not century year then also leap year 2012
cout << year << " is a leap year.";
}
else
cout << year << " is not a leap year."; // if not
divide by 4
/* IF YOU UNDERSTAND PLEASE UPVOTE */