0% found this document useful (0 votes)
8 views7 pages

Worksheet 1

Hsjsjsjshgdgdvbsbdjdkdmdnbs

Uploaded by

amandm1200
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Worksheet 1

Hsjsjsjshgdgdvbsbdjdkdmdnbs

Uploaded by

amandm1200
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Prayagraj

Session 2020-2021

Round 10

Subject : Computer Science

Class : XI
Worksheet 1
Question:
Given a date, check if it is valid or not. It may
be assumed that the given date is in range
from 01/01/1800 to 31/12/9999.
INPUT:
Enter day as dd:
18
Enter day as mm:
11
Enter day as yyyy:
2020
OUTPUT:
The given date 18/11/2020 is a Valid Date
INPUT:
Enter day as dd:
30
Enter day as mm:
02
Enter day as yyyy:
2020
OUTPUT:
The given date 30/02/2020 is an Invalid Date

Program Code:

import java.util.*;
class ValidDate
{
int MAX_VALID_YR = 9999;
int MIN_VALID_YR = 1800;

public boolean isLeap(int year)


{

if(((year % 4 == 0) && (year % 100 != 0)) || (year %


400 == 0))
return true;
else
return false;

public boolean isValidDate(int d, int m,int y)


{

if (y > MAX_VALID_YR ||y < MIN_VALID_YR)


return false;
if (m < 1 || m > 12)
return false;
if (d < 1 || d > 31)
return false;

if (m == 2)
{
if (isLeap(y))
return (d <= 29);
else
return (d <= 28);
}
if (m == 4 || m == 6 || m == 9 || m == 11)

return (d <= 30);

return true;
}

public static void main(String args[])


{
Scanner sc= new Scanner(System.in);
ValidDate ob=new ValidDate();
System.out.println("Enter day as dd:");
int dd=sc.nextInt();
System.out.println("Enter month as mm:");
int mm=sc.nextInt();
System.out.println("Enter year as yyyy:");
int yy=sc.nextInt();

if (ob.isValidDate(dd, mm, yy)==true)


System.out.println("The given date "+dd+
"/"+mm+"/"+yy+" is a Valid Date");
else
System.out.println("The given date "+dd+
"/"+mm+"/"+yy+" is an Invalid Date");
}
}

You might also like