In: Computer Science
Basically if the year is divisible by 4( think year%4==0) it’s a leap year UNLESS its divisible by 100 in which case its not a leap year UNLESS its also divisible by 400 , in which case it is.
Write a program that evaluates the years from 1890 to 2017 and write out the year if it is a leap year. Otherwise do not write out anything.
Hints
Here is what the output should look like
1892 is a leap year
1896 is a leap year
1904 is a leap year
1908 is a leap year
1912 is a leap year
1916 is a leap year
1920 is a leap year
1924 is a leap year
1928 is a leap year
1932 is a leap year
1936 is a leap year
1940 is a leap year
1944 is a leap year
1948 is a leap year
1952 is a leap year
1956 is a leap year
1960 is a leap year
1964 is a leap year
1968 is a leap year
1972 is a leap year
1976 is a leap year
1980 is a leap year
1984 is a leap year
1988 is a leap year
1992 is a leap year
1996 is a leap year
2000 is a leap year
2004 is a leap year
2008 is a leap year
2012 is a leap year
2016 is a leap year
Given in the question, if the year is divisible by 4( think year%4==0) it’s a leap year UNLESS its divisible by 100 in which case its not a leap year UNLESS its also divisible by 400 , in which case it is. Following is the code:
In Python
def check(year):
if (year % 400) == 0:
return True
if (year % 100) == 0:
return False
if (year % 4) == 0:
return True
return False
for y in range(1890, 2018):
if(check(y)):
print(y, "is a leap Year")
In Java
public class MyClass {
public static boolean check(int year)
{
if (year % 400 == 0)
return true;
if (year % 100 == 0)
return false;
if (year % 4 == 0)
return true;
return false;
}
public static void main(String args[]) {
for(int i=1890; i<=2017; i++) {
if(check(i)) {
System.out.println(i + " is a leap year");
}
}
}
}
In C++
#include <iostream>
using namespace std;
bool check(int year)
{
if (year % 400 == 0)
return true;
if (year % 100 == 0)
return false;
if (year % 4 == 0)
return true;
return false;
}
int main() {
for(int i=1890; i<=2017; i++) {
if(check(i)) {
cout<<i<<" is a leap year\n";
}
}
}