In: Computer Science
Write this in c++
Leap year and Second in year program
1. If a year is divisible by 4 with no remainder, it is a leap
year,
2. unless it is a centurial year (ending in 00).
3. Then it is not a leap year,
4. unless it is a year divisible by 400, in which case it is still
a leap year.
For example:
1904 is a leap year—evenly divisible by 4.
1900 is not a leap year—it is evenly divisible by 4, and also
evenly divisible by 100. However, it is not evenly divisible by
400.
2000 is a leap year— it is evenly divisible by 4, evenly divisible
by 100, but also evenly divisible by 400.
Your calculation should ignore all other variations in the length
of the calendar year, such as leap seconds or the change from the
Julian to the Gregorian system.
import java.util.Scanner;
public class YearToSeconds {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
while (true) {
System.out.println("Enter year (-99 to quit): ");
int year =
sc.nextInt();
if(year==-99)
break;
long seconds =
0;
if
(isLeap(year))
seconds = 365 * 24 * 3600;
else
seconds = 366 * 24 * 3600;
System.out.println(year + " has " + seconds + " seconds");
}
}
private static boolean isLeap(int year) {
boolean leap = false;
// if any year is divisable by 4
than there are many chances for leap year
// except few
if (year % 4 == 0) {
// if it is
divisable by 100 than it shoud also divisable by 400 like 2000
etc
if (year % 100
== 0) {
// year is divisible by 400, so the year is a
leap year
if (year % 400 == 0)
leap = true;
else
leap = false;
} else
leap = true;
} else
leap =
false;
return leap;
}
}

Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me