Vous êtes sur la page 1sur 6

Yearly Calendar

Q: Write a program that asks for a year and the first day of January on that particular year and prints the calendar of that year.

CODE
import java.util.*; import java.io.*; class yearly_calendar { public static void main()throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Which Year's Calendar Do You Want To Be Displayed?"); int year=Integer.parseInt(br.readLine()); int month,c=1,m=0,no_of_days=0; System.out.println("When Was The First Of January?"); System.out.println("Press 0 : Sunday"); System.out.println("Press 1 : Monday"); System.out.println("Press 2 : Tueday"); System.out.println("Press 3 : Wednesday"); System.out.println("Press 4 : Thursday"); System.out.println("Press 5 : Friday"); System.out.println("Press 6 : Saturday"); System.out.println("Now Enter :"); int day=Integer.parseInt(br.readLine()); System.out.println("---------------------------------------------"); System.out.println("\t\tCALENDAR FOR "+year); for(month=1;month<=12;month++) { if(month==1||month==3||month==5||month==7||month==8||month==10|| month==12) no_of_days=31; if(month==4||month==6||month==9||month==11) no_of_days=30; if(month==2) { if(year>1700 &&((year%4==0)&&(year%100!=0))||(year<1700 &&(year %4==0))) no_of_days=29; else no_of_days=28; } if(month==1) System.out.println("\t\tJANUARY-"+no_of_days+"DAYS"); if(month==2)

System.out.println("\t\tFEBRUARY-"+no_of_days+"DAYS"); if(month==3) System.out.println("\t\tMARCH-"+no_of_days+"DAYS"); if(month==4) System.out.println("\t\tAPRIL-"+no_of_days+"DAYS"); if(month==5) System.out.println("\t\tMAY-"+no_of_days+"DAYS"); if(month==6) System.out.println("\t\tJUNE-"+no_of_days+"DAYS"); if(month==7) System.out.println("\t\tJULY-"+no_of_days+"DAYS"); if(month==8) System.out.println("\t\tAUGUST-"+no_of_days+"DAYS"); if(month==9) System.out.println("\t\tSEPTEMBER-"+no_of_days+"DAYS"); if(month==10) System.out.println("\t\tOCTOBER-"+no_of_days+"DAYS"); if(month==11) System.out.println("\t\tNOVEMBER-"+no_of_days+"DAYS"); if(month==12) System.out.println("\t\tDECEMBER-"+no_of_days+"DAYS"); System.out.println("SUN\tMON\tTUE\tWED\tTHU\tFRI\tSAT"); m=0; while(day>0) { System.out.print("\t"); day--; m++; } c=1;//c is for comparison with the number of days while(c<=no_of_days) { System.out.print(c+"\t"); c++; m++;//m is for comparison with the day of the week if(m==7) { System.out.println(); m=0; } } System.out.println("\n"); day=m; } System.out.println("---------------------------------------------------"); } }

OUTPUT
Which Year's Calendar Do You Want To Be Displayed? 2010 When Was The First Of January? Press 0 : Sunday Press 1 : Monday Press 2 : Tueday Press 3 : Wednesday Press 4 : Thursday Press 5 : Friday Press 6 : Saturday Now Enter : 5 --------------------------------------------CALENDAR FOR 2010 JANUARY-31DAYS SUN MON TUE WED THU FRI SAT 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 SUN 7 14 21 28 SUN 7 14 21 28 SUN 4 11 18 25 MON 1 8 15 22 FEBRUARY-28DAYS TUE WED THU FRI 2 3 4 5 9 10 11 12 16 17 18 19 23 24 25 26 SAT 6 13 20 27

MARCH-31DAYS MON TUE WED THU 1 2 3 4 8 9 10 11 15 16 17 18 22 23 24 25 29 30 31 APRIL-30DAYS MON TUE WED THU 1 5 6 7 8 12 13 14 15 19 20 21 22 26 27 28 29 MAY-31DAYS

FRI 5 12 19 26

SAT 6 13 20 27

FRI 2 9 16 23 30

SAT 3 10 17 24

SUN 2 9 16 23 30 SUN 6 13 20 27 SUN 4 11 18 25

MON TUE 3 10 17 24 31 4 11 18 25

WED THU FRI 5 12 19 26 6 13 20 27 7 14 21 28

SAT 1 8 15 22 29

JUNE-30DAYS MON TUE WED THU 1 2 3 7 8 9 10 14 15 16 17 21 22 23 24 28 29 30 JULY-31DAYS MON TUE WED THU 1 5 6 7 8 12 13 14 15 19 20 21 22 26 27 28 29 AUGUST-31DAYS TUE WED THU 3 4 5 10 11 12 17 18 19 24 25 26 31

FRI 4 11 18 25

SAT 5 12 19 26

FRI 2 9 16 23 30

SAT 3 10 17 24 31

SUN 1 8 15 22 29 SUN 5 12 19 26 SUN 3 10 17 24 31

MON 2 9 16 23 30

FRI 6 13 20 27

SAT 7 14 21 28

SEPTEMBER-30DAYS MON TUE WED THU FRI 1 2 3 6 7 8 9 10 13 14 15 16 17 20 21 22 23 24 27 28 29 30 OCTOBER-31DAYS MON TUE WED THU FRI 1 4 5 6 7 8 11 12 13 14 15 18 19 20 21 22 25 26 27 28 29

SAT 4 11 18 25

SAT 2 9 16 23 30

SUN 7 14 21 28 SUN 5 12 19 26

NOVEMBER-30DAYS MON TUE WED THU FRI 1 2 3 4 5 8 9 10 11 12 15 16 17 18 19 22 23 24 25 26 29 30 DECEMBER-31DAYS MON TUE WED THU FRI 1 2 3 6 7 8 9 10 13 14 15 16 17 20 21 22 23 24 27 28 29 30 31

SAT 6 13 20 27

SAT 4 11 18 25

--------------------------------------------------ALGORITHM 1. 2. 3. 4. Declare the necessary package. Declare the class. Declare the main function. In the main function, Declare the required variables. Ex => year, month, day, c, m, no_of_days. Initialize 1 to c, 0 to m and 0 to no_of_days. Accept the year for which the calendar is to be displayed from the user and store it in year. Accept the day on the first of January from the user and store it in day. Run a for loop from 1 to l2 with month as iterative variable. In the for loop, If month is 1,3,5,7,8,10,12 then store 31 in no_of_days. If month is 4,6,9,11 then store 30 in no_of_days. If month is 2 then check whether it is a leap year. If it is a leap year store 29 in no_of_days otherwise store 28. Print the month along with the number of days. Print Sunday, Monday, . . . ., Saturday. Initialize 0 to m. Run a while loop till day is greater than 0. In the while loop, Print a tab. Decrement day by 1. Increment m by 1. End the while loop. Store 1 in c. Run a while loop till no_of _days with c as the iterative variable. In the second while loop,

Print c along with a tab. Increment c by 1. Increment m by 1. If m is equal to 7 go to the next line and store 0 in m. Go to the next line. End the second while loop. Go to the next line. Store m in day. End the for loop. 5. End the main function. 6. End the class. VARIABLE LISTING
VARIABLE TYPE SCOPE DESCRIPTION

year month day c m no_of_days

int int int int int int

main() main() main() main() main() main()

To store the year. To store the month. To store the day. For comparison with number of days. For comparison with day of the week. To store the no of days in a month.

Vous aimerez peut-être aussi