Thursday 19 January 2017

Days Elapsed...

Write a program to input two valid dates, each comprising of day          (2 digits), Month (2 digits) and year (4 digits) and calculate the days elapsed between the two dates. Test your program for the following data values:
i) FIRST DATE :              DAY : 24
                                      MONTH: 09
                                      YEAR: 1960
   SECOND DATE :           DAY : 08
                                      MONTH: 12
                                      YEAR: 1852
       
ii) FIRST DATE :             DAY : 10
                                      MONTH: 01
                                      YEAR: 1852
   SECONDDATE :             DAY : 16
                                       MONTH: 10

                                       YEAR: 1952                


import java.util.*;
class DateCalculation
{
     int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
     int dd,mm,yy;
     void input1()/*input first date*/
     {
           Scanner sc=new Scanner(System.in);
           System.out.print("First Date:   DAY    :");
           dd=sc.nextInt();
           System.out.print("\n              MONTH  :");
           mm=sc.nextInt();
           System.out.print("\n              YEAR   :");
           yy=sc.nextInt();
     }
     void input2()/*input second date*/
     {
           Scanner sc=new Scanner(System.in);
           System.out.print("\nSecond Date:  DAY    :");
           dd=sc.nextInt();
           System.out.print("\n              MONTH  :");
           mm=sc.nextInt();
           System.out.print("\n              YEAR   :");
           yy=sc.nextInt();
     }
     int isLeap(int y)/*Function will check for leap year*/
     {
           if((y%4==0&&y%100!=0)||y%400==0)
           {
                return 29;
           }
           else return 28;     
     }
     int isvalid()/*Check the date*/
     {
           if(isLeap(yy)==29)
           {
                month[2]=29;
           }
           if (mm<1||mm>12)
           {
                return 0;
           }
           if(yy<1000||yy>9999)
           {
                return 0;
           }
           if(dd<1||dd>month[mm])
           {
                return 0;
           }
                return 1;
     }
     int dayno()//Functiom for finding Day Number from year=1000 till inputed year
     {
           int dn=0;
           for(int i=0;i<mm;i++)
           {
                dn=dn+month[i];
           }
           dn=dn+dd;
           for (int i=1000;i<yy;i++)
           {
                if(isLeap(i)==29)
                {
                     dn=dn+366;
                }
                else
                     dn=dn+365;
           }
           return dn;
     }
     void dayselaspsed(DateCalculation x)
     {
           if(isvalid()==0)
           {
                System.out.println("Please Enter a Valid Date");
                System.exit(0);
           }
           if(x.isvalid()==0)
           {
                System.out.println("Please Enter a Valid Date");
                System.exit(0);
           }
           else
           {
                int Days=Math.abs(dayno()-x.dayno());
                System.out.println("Days Elapsed="+Days);
           }                   
     }
     public static void main(String args[])
     {
           DateCalculation obj1 =new DateCalculation();
           DateCalculation obj2 =new DateCalculation();
           obj1.input1();
           obj2.input2();
           obj1.dayselaspsed(obj2);       
     }
}





No comments:

Post a Comment

Goldbach Number ISC 2018

Question: A Goldbach number is a positive even integer that can be expressed as the sum of two odd primes. Note: All even integer numbers ...