Y Y Y Yyyyy Y Y Y! "# Y$%&' ( (%) '' '++%&''' Y, Y
Y Y Y Yyyyy Y Y Y! "# Y$%&' ( (%) '' '++%&''' Y, Y
A year is a
leap year if it is divisible by 4 but not 100 or if it is divisible by 400.
import java.util.Scanner;
int year;
if (((year%4==0)&&(year%100!=0))||(year%400==0))
else
}
}
2. Write a program that displays all the leap years, ten per line, in the twenty-first century (from
2001 to 2100).
int columnCounter=1;
if(((year%4==0)&&(year%100!=0))||(year%400==0))
break;
}
}
return columnCounter;
3. Write a program that prompts the user to enter the month and year and displays the number of
days in the month. For example, if the user entered month 2 and year 2000, the program
should display that February 2000 has 29 days. If the user entered month 3 and year 2005, the
program should display that March 2005 has 31 days.
import java.util.Scanner;
public class Days {
public static void main(String[] args) {
Scanner keyboard = new Scanner (System.in);
int m, y, month ;
System.out.print("Enter month number: ");
m=keyboard.nextInt();
System.out.print("Enter year: ");
y=keyboard.nextInt();
String d = null;
switch (m) {
case 2: if (((y%4==0)&&(y%100!=0))||(y%400==0)){
d="29";}
else{
d="28";};break;
case 1:case 3:case 5:case 7:case 8:case 10:case 12:d="31";break;
case 4:case 6:case 9:case 11:d="30";break;
default:d="Invalid";break;}
String g = null;
switch (m) {
case 1:g="January ";break;
case 2:g="Febuary ";break;
case 3:g="March ";break;
case 4:g="April ";break;
case 5:g="May ";break;
case 6:g="June ";break;
case 7:g="July ";break;
case 8:g="August ";break;
case 9:g="September ";break;
case 10:g="October ";break;
case 11:g="November ";break;
case 12:g="December ";break;
default:g="Invalid ";break;}
System.out.println(g+y+" has "+d+" days. ");
}
}
1 1 1 1
e=1+ + + +…+
1! 2 ! 3 ! i!
Write a program that displays the e value for i= 10000, 20000, ….. , and 100000.
Hint
1 1
Since i! = i x (i – 1) x ….. x 2 x 1, is . Initialize e and item to be 1 and keep
i ! i ( i−1 ) !
adding a new item to e. The new item is the previous item divided by i for i = 2, 3, 4, …. .