public static int daysPassed(int month, int day, int year){ int daysPassed = 0;
ID: 3566224 • Letter: P
Question
public static int daysPassed(int month, int day, int year){
int daysPassed = 0;
if(month == 1){
daysPassed = day;
}
if(month == 2){
daysPassed = 31 + day;
}
if(month == 3){
if(isLeapYear(year) == true){
daysPassed = 31 + 29 + day;
}
else{
daysPassed = 31 + 28 + day;
}
}
if(month == 4){
if(isLeapYear(year) == true){
daysPassed = 2 * 31 + 29 + day;
}
else{
daysPassed = 2 * 31 + 28 + day;
}
}
if(month == 5){
if(isLeapYear(year) == true){
daysPassed = 2 * 31 + 29 + 30 + day;
}
else{
daysPassed = 2 * 31 + 28 + 30 + day;
}
}
if(month == 6){
if(isLeapYear(year) == true){
daysPassed = 3 * 31 + 29 + 30 + day;
}
else{
daysPassed = 3 * 31 + 28 + 30 + day;
}
}
if(month == 7){
if(isLeapYear(year) == true){
daysPassed = (3 * 31) + 29 + (2 * 30) + day;
}
else{
daysPassed = (3 * 31) + 28 + (2 * 30) + day;
}
}
if(month == 8){
if(isLeapYear(year) == true){
daysPassed = (4 * 31) + 29 + (2 * 30) + day;
}
else{
daysPassed = (4 * 31) + 28 + (2 * 30) + day;
}
}
if(month == 9){
if(isLeapYear(year) == true){
daysPassed = (5 * 31) + 29 + (2 * 30) + day;
}
else{
daysPassed = (5 * 31) + 28 + (2 * 30) + day;
}
}
if(month == 10){
if(isLeapYear(year) == true){
daysPassed = (5 * 31) + 29 + (3 * 30) + day;
}
else{
daysPassed = (5 * 31) + 28 + (3 * 30) + day;
}
}
if(month == 11){
if(isLeapYear(year) == true){
daysPassed = (6 * 31) + 29 + (3 * 30) + day;
}
else{
daysPassed = (6 * 31) + 28 + (3 * 30) + day;
}
}
if(month == 12){
if(isLeapYear(year) == true){
daysPassed = (6 * 31) + 29 + (4 * 30) + day;
}
else{
daysPassed = (6 * 31) + 28 + (4 * 30) + day;
}
}
return daysPassed;
Explanation / Answer
public static int daysPassed(int month, int day, int year){
int daysPassed = 0;
if(month == 1){
daysPassed = day; // for month =1, passed days of the calender from january initial will be assigned // to daysPassed
}
if(month == 2){
daysPassed = 31 + day; // for month =2, total days from january to today of the calender will be assigned // to daysPassed
}
if(month == 3){
if(isLeapYear(year) == true){ // for month =3, total days from january to today of the calender will be assigned // to daysPassed
}
daysPassed = 31 + 29 + day; // in leap year
}
else{
daysPassed = 31 + 28 + day;
}
}
if(month == 4){ for month =2, total days from january to today of the calender will be assigned // to daysPassed
}
if(isLeapYear(year) == true){
daysPassed = 2 * 31 + 29 + day;
}
else{
daysPassed = 2 * 31 + 28 + day;
}
}
if(month == 5){
if(isLeapYear(year) == true){
daysPassed = 2 * 31 + 29 + 30 + day;
}
else{
daysPassed = 2 * 31 + 28 + 30 + day;
}
}
if(month == 6){
if(isLeapYear(year) == true){
daysPassed = 3 * 31 + 29 + 30 + day;
}
else{
daysPassed = 3 * 31 + 28 + 30 + day;
}
}
if(month == 7){
if(isLeapYear(year) == true){
daysPassed = (3 * 31) + 29 + (2 * 30) + day;
}
else{
daysPassed = (3 * 31) + 28 + (2 * 30) + day;
}
}
if(month == 8){
if(isLeapYear(year) == true){
daysPassed = (4 * 31) + 29 + (2 * 30) + day;
}
else{
daysPassed = (4 * 31) + 28 + (2 * 30) + day;
}
}
if(month == 9){
if(isLeapYear(year) == true){
daysPassed = (5 * 31) + 29 + (2 * 30) + day;
}
else{
daysPassed = (5 * 31) + 28 + (2 * 30) + day;
}
}
if(month == 10){
if(isLeapYear(year) == true){
daysPassed = (5 * 31) + 29 + (3 * 30) + day;
}
else{
daysPassed = (5 * 31) + 28 + (3 * 30) + day;
}
}
if(month == 11){
if(isLeapYear(year) == true){
daysPassed = (6 * 31) + 29 + (3 * 30) + day;
}
else{
daysPassed = (6 * 31) + 28 + (3 * 30) + day;
}
}
if(month == 12){
if(isLeapYear(year) == true){
daysPassed = (6 * 31) + 29 + (4 * 30) + day;
}
else{
daysPassed = (6 * 31) + 28 + (4 * 30) + day;
}
}
return daysPassed;
// daysPassed() assign the no. of days passed from january 1 to today i.e any date of any month by considering
// seperate case for leap year
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.