Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to clean up my nest of if-else statements for a calendar program. Wh

ID: 3736062 • Letter: I

Question

I'm trying to clean up my nest of if-else statements for a calendar program. When January is selected ('m=1'), my syntax is causing February's 28 days to be incorrectly returned. I would use a switch statement to simplify, but I have two arguments, as February has to additionaly account for a leap year.

int getndim(int m, int y)

{ int d;

if( m==1)

d = 31;

if((( m==2 && y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))

d = 29;

else d =28;

if( m==3)

d = 31;

if( m==4)

d = 30;

if( m==5)

d = 31;

if( m==6)

d=30;

.................

return (d); }

Explanation / Answer

Please find the program below : -

=======================

public class Main {

EXECUTION OUTPUT :

==================

Feb[Leap Year] = 29
Feb[NON Leap Year] = 28
JAN = 31
DEC = 31