0% found this document useful (0 votes)
26 views5 pages

Program 13 ADD DAYS

Uploaded by

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

Program 13 ADD DAYS

Uploaded by

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

Program 13: ADD DAYS

_____________________________________________
import java.util.Scanner;

public class AddDays {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the date (dd/mm/yyyy): ");
String[] parts = sc.nextLine().split("/");
int day = Integer.parseInt(parts[0]);
int month = Integer.parseInt(parts[1]);
int year = Integer.parseInt(parts[2]);

System.out.print("Enter number of days to add: ");


int daysToAdd = sc.nextInt();

int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31,
30, 31, 30, 31};

day += daysToAdd;
while (day > daysInMonth[month - 1]) {
day -= daysInMonth[month - 1];
month++;
if (month > 12) {
month = 1;
year++;
daysInMonth[1] = (year % 4 == 0 && (year %
100 != 0 || year % 400 == 0)) ? 29 : 28;
}
}

System.out.println("New date: " + day + "/" + month


+ "/" + year);
}
}
ALGORITHM
_____________________________________________

Step 1: Prompt the user to enter a date in the format


dd/mm/yyyy.

Step 2: Read and split the date string by the delimiter /,


then convert the parts into integers: day, month, and
year.

Step 3: Prompt the user to enter the number of days to


add.

Step 4: Read the number of days to add and store it in


daysToAdd.

Step 5: Define an array daysInMonth representing the


number of days in each month for a non-leap year.

Step 6: Add daysToAdd to day.

Step 7: While day exceeds the number of days in the


current month:

 Subtract the number of days in the current month


from day.
 Increment month by 1.
 If month exceeds 12:
o Set month to 1.
o Increment year by 1.
o Update the number of days in February for leap
years:
 Set daysInMonth[1] to 29 if year is a leap
year, otherwise set it to 28.

Step 8: Print the new date in the format dd/mm/yyyy.

OUTPUT
_____________________________________________
Variable Table
_____________________________________________

You might also like