Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ISC COMPUTER SCIENCE PRACTICAL 2001 SOLVED QUESTION 3 : DAYS LAPSED

Saturday, March 16, 2019

ISC COMPUTER SCIENCE PRACTICAL 2001 SOLVED QUESTION 3 : DAYS LAPSED

 Input consists of the month number [MM], day of the month [DD] and the year [YYY]. Write a program to calculate and print the corresponding day of the year [in the range of 1 to 366]
Example :

INPUT : Month number  05
               Day 03
               Year 1996 
               OUTPUT :    CORRESPONDING DAY OF THE YEAR IS 124

The problem is quite simple, after taking the input just test whether the year is leap or not:
Following this we will add number of days of each month till the month just before
the input month from the corresponding array of month days for leap no non leap year.
The following code does exactly this
 while(counter < month-1) {
                    days_lapsed += ldays[counter];
                    counter++;
                    }
then we will add days before the input day: for example
if input is 16/5/2019
Since 2019 is non-leap
int[] mdays={31,28,31,30,31,30,31,31,30,31,30,31};
all days of months before the month of May will be added
31+28+31+30 (till april) = 120
then since the day is 16th, 16 more days will be added to 120
to get 120+16=136


import java.util.*;
public class ISC2015Q1 {
    public static void main(String[] args) {
       int day;
             int yr;
             int month;
        Scanner in = new Scanner(System.in);
        DaysCalculation date = new DaysCalculation();
             do {
                    System.out.println("Enter the day (between 1 and 31)");
                    day = in.nextInt();
                    if(day < 1 || day > 31)
                                 System.out.println("INVALID DAY:");
                    }while(day < 1 || day > 31);
             do {
                System.out.println("Enter the month:");
                month = in.nextInt();
                if(month < 1 || month > 12)
                             System.out.println("INVALID MONTH (between 1 and 12):");
                }while(month < 1 || month > 12);
           
             do {
                    System.out.println("Enter the year");
                    yr = in.nextInt();
                    if(Integer.toString(yr).length()!=4)
                                 System.out.println("INVALID YEAR:");
                    }while(Integer.toString(yr).length()!=4);
            
        date.setDate(day,yr,month); 
        System.out.println("OUTPUT:");
        date.daysElapsed();
        in.close();
        }  
    }
class DaysCalculation {
    public void setDate(int day, int yr, int month) {
        this.dayNumber = day;
        this.year = yr;
        this.month = month;
        }
    public void daysElapsed() {
       int counter = 0;
       int days_lapsed=0;
       if(checkLeap(year))     {
             while(counter < month-1) {
                    days_lapsed += ldays[counter];
                    counter++;
                    }
             days_lapsed +=dayNumber;
             }
        else {
             while(counter < month-1) {
                    days_lapsed += mdays[counter];
                    counter++;
                    }
             days_lapsed +=dayNumber;
             }
        System.out.println("Corresponding day of the year:"+days_lapsed);
        }  
    private boolean checkLeap(int year) {
        if(year%400==0)
           leap=true;
        else if (year%100==0)
           leap=false;
        else if (year%4==0)
           leap=true;
        else
           leap=false;
           return leap;
        }   
    //private boolean flag;
    private static boolean leap;  
    private int month;
    private int year;
    private int dayNumber;
    //String[] months = {"January","Feburary","March","April","May","June","July","August","Sepetember","October","November","December"};
    int[] mdays={31,28,31,30,31,30,31,31,30,31,30,31};
    int[] ldays={31,29,31,30,31,30,31,31,30,31,30,31};  
    }

No comments:

Post a Comment