0% found this document useful (0 votes)
8 views

PROGRAM 15 WEEK NUMBER

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)
8 views

PROGRAM 15 WEEK NUMBER

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 15 : WEEK

NUMBER
_____________________________________________
import java.util.Scanner;

public class WeekNumbers {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a 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]);

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

// Days from start of year


int daysFromStart = daysSinceStart(year, month,
day, daysInMonth);
// Calculate week number
int weekNumber = (daysFromStart / 7) + 1;

System.out.println("Week number of the year: " +


weekNumber);
}

private static int daysSinceStart(int year, int month, int


day, int[] daysInMonth) {
int days = 0;
for (int y = 0; y < year; y++) {
days += (y % 4 == 0 && (y % 100 != 0 || y % 400
== 0)) ? 366 : 365;
}
for (int m = 0; m < month - 1; m++) {
days += daysInMonth[m];
}
if (month > 2 && year % 4 == 0 && (year % 100 !=
0 || year % 400 == 0)) {
days++;
}
days += day;
return days;
}} _____________________________________________

ALGORITHM
_____________________________________________ Step 1:
Input the date in dd/mm/yyyy format.

Step 2: Split the date into day, month, and year


components.

Step 3: Initialize an array daysInMonth[] to store the


number of days in each month.

Step 4: If month > 2 and year is a leap year, add 1 to


the total days.

Step 5: Calculate the number of days since the start of


the year using the daysSinceStart function.

Step 6: Divide the total days by 7 and add 1 to get the


week number.

Step 7: Output the week number

_____________________________________________
OUTPUT
_____________________________________________
Variable Table
_____________________________________________

You might also like