0% found this document useful (0 votes)
6 views6 pages

Worksheet 5

Nsbsnksnsnsbsbsbdbbdbdbdbd

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)
6 views6 pages

Worksheet 5

Nsbsnksnsnsbsbsbdbbdbdbdbd

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/ 6

Prayagraj

Session 2020-2021

Round 10

Subject : Computer Science

Class : XI

Worksheet 5
Question:
Write a program to accept a date in the string
format dd/mm/yyyy. Check whether the date
entered is valid or not. If it is valid, then input a
certain number of days. Then calculate and print
the future date after adding the given number of
days if the future date is valid. If the date entered is
invalid, then display a proper error message.

Example:

INPUT:
Date : 07 / 04 / 2013
Enter Number of days after : 7
OUTPUT:
Entered Date : 07 / 04 / 2013
Future Date : 14 / 04 / 2013

Programming Code:

import java.util.*;
class FutureDate
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int
month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
System.out.print("Enter the date in (dd/mm/yyyy)
format: ");
String date=sc.nextLine().trim();
int p,q,count=0;
p=date.indexOf("/");
int d=Integer.parseInt(date.substring(0,p));
q=date.lastIndexOf("/");
int m=Integer.parseInt(date.substring(p+1,q));
int y=Integer.parseInt(date.substring(q+1));
System.out.println("Entered Date: "+date);
if((y%400==0) || ((y%100!=0)&&(y%4==0))) //
Checking for leap year
month[2]=29;

if(m<0 || m>12 || d<0 || d>month[m] || y<0 ||


y>9999) // Performing Date Validation
{
System.out.println("Invalid Date");
}
else
{
System.out.print("Enter number of days after
which future date is to be found: ");
int days=sc.nextInt();
while(count<days)
{
d++;
count++;

/* If day exceeds the maximum days of a


month then day should start from 1
and month should increase */

if(d>month[m])
{
d=1;
m++;
}

/* If month exceeds 12 then month should


start from 1
and year should increase */
if(m>12)
{
m=1;
y++;
if((y%400==0) || ((y%100!=0)&&(y%4==0)))
month[2]=29;
else
month[2]=28;
}
}
System.out.println("Future Date :
"+d+"/"+m+"/"+y);
}
}
}

You might also like