KnowledgeBoat Logo

Computer Science

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 both the dates.

Test your program for the following data values:

(a)
FIRST DATE:
Day: 24
Month: 09
Year: 1960

SECOND DATE:
Day: 08
Month: 12
Year: 1852

Output: xxxxxxxx
(these are actual number of days elapsed)

(b)
FIRST DATE:
Day: 10
Month: 01
Year: 1952

SECOND DATE:
Day: 16
Month: 10
Year: 1952

Output: xxxxxxxx
(these are actual number of days elapsed)

Java

Java Arrays

9 Likes

Answer

import java.util.Scanner;

public class KboatDaysBetweenDates
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        int d1[] = new int[3];
        int d2[] = new int[3];
        System.out.println("Enter First Date: ");
        System.out.print("Enter Day: ");
        d1[0] = in.nextInt();
        System.out.print("Enter Month: ");
        d1[1] = in.nextInt();
        System.out.print("Enter Year: ");
        d1[2] = in.nextInt();
        System.out.println("Enter Second Date: ");
        System.out.print("Enter Day: ");
        d2[0] = in.nextInt();
        System.out.print("Enter Month: ");
        d2[1] = in.nextInt();
        System.out.print("Enter Year: ");
        d2[2] = in.nextInt();
        
        int monthDays[] = {31, 28, 31, 30, 31, 30, 
                            31, 31, 30, 31, 30, 31};
                            
        int n1 = d1[2] * 365 + d1[0];
        int n2 = d2[2] * 365 + d2[0];
        
        for (int i = 0; i < d1[1] - 1; i++) {
            n1 += monthDays[i];
        }
        
        for (int i = 0; i < d2[1] - 1; i++) {
            n2 += monthDays[i];
        }
        
        int y = d1[1] <= 2 ? d1[2] - 1 : d1[2];
        n1 += y / 4 - y / 100 + y / 400;
        
        y = d2[1] <= 2 ? d2[2] - 1 : d2[2];
        n2 += y / 4 - y / 100 + y / 400;
        
        int daysElapsed = Math.abs(n2 - n1);
        System.out.println("No of days Elapsed = " + daysElapsed);
    }
}

Output

BlueJ output of 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 both the dates. Test your program for the following data values: (a) FIRST DATE: Day: 24 Month: 09 Year: 1960 SECOND DATE: Day: 08 Month: 12 Year: 1852 Output: xxxxxxxx (these are actual number of days elapsed) (b) FIRST DATE: Day: 10 Month: 01 Year: 1952 SECOND DATE: Day: 16 Month: 10 Year: 1952 Output: xxxxxxxx (these are actual number of days elapsed)BlueJ output of 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 both the dates. Test your program for the following data values: (a) FIRST DATE: Day: 24 Month: 09 Year: 1960 SECOND DATE: Day: 08 Month: 12 Year: 1852 Output: xxxxxxxx (these are actual number of days elapsed) (b) FIRST DATE: Day: 10 Month: 01 Year: 1952 SECOND DATE: Day: 16 Month: 10 Year: 1952 Output: xxxxxxxx (these are actual number of days elapsed)

Answered By

4 Likes


Related Questions