Categories
Class 11 Class 12

Date of Birth Program ISC 2011 Practical

Design a program which accepts your date of birth in dd mm yyyy format. Check whether the date entered is valid or not. If it is valid, display “VALID DATE”, also compute and display the day number of the year for the date of birth. If it is invalid, display “INVALID DATE” and then terminate the program.

Test your program for the given sample data and some random data.

Example 1:
INPUT:
Enter your date of birth in dd mm yyyy format:
05
01
2010
OUTPUT:
VALID DATE
5

Example 2:
INPUT:
Enter your date of birth in dd mm yyyy format:
03
04
2010
OUTPUT:
VALID DATE
93

Example 3:
INPUT:
Enter your date of birth in dd mm yyyy format:
34
06
2010
OUTPUT:
INVALID DATE

import java.io.*;
class DateOfBirth{
    public static void main(String args[])throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("Enter date in dd mm yyyy format: ");
        String d = br.readLine();
        int date = Integer.parseInt(d.substring(0, 2));
        int month = Integer.parseInt(d.substring(3, 5));
        int year = Integer.parseInt(d.substring(6));
        boolean isValid = true;
        switch(month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
            if(date <= 0 || date > 31)
                isValid = false;
            break;
            case 4:
            case 6:
            case 9:
            case 11:
            if(date <= 0 || date > 30)
                isValid = false;
            case 2:
            if(isLeap(year) && date > 29)
                isValid = false;
            else if(date > 28)
                isValid = false;
        }
        if(!isValid)
            System.out.println("INVALID DATE");
        else{
            System.out.println("VALID DATE");
            int days = date;
            for(int i = 1; i < month; i++){
                switch(i){
                    case 1:
                    case 3:
                    case 5:
                    case 7:
                    case 8:
                    case 10:
                    case 12:
                    days += 31;
                    break;
                    case 2:
                    if(isLeap(year))
                        days += 29;
                    else
                        days += 28;
                    break;
                    case 4:
                    case 6:
                    case 9:
                    case 11:
                    days += 30;
                }
            }
            System.out.println("Day number: " + days);
        }
    }
    public static boolean isLeap(int y){
        if((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
            return true;
        return false;
    }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.