Pages

Tuesday, 23 April 2013

Java code to read a date and gives the day number

/*
Program to read a date and print its corresponding day No.
*/
import java.io.*;
class date2dayNo
{
public static void main(String args[]) throws IOException
{
date2dayNo call = new date2dayNo();
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
// Read the date
System.out.print("Enter the Date (dd/mm/yyyy)\n\nDay : ");
int d = Integer.parseInt(br.readLine());
System.out.print("Month : ");
int m = Integer.parseInt(br.readLine());
System.out.print("Year : ");
int y = Integer.parseInt(br.readLine());
call.toDayNo(d,m,y);
}
public void toDayNo(int d, int m, int y)
{
int a[] = {31,28,31,30,31,30,31,31,30,31,30,31};
if(y%4==0) // Check for leap year
a[1] = 29;
int s=0, c=0;
while(c < m-1)
s += a[c++];
s += d;
System.out.println("\nDay No. = " +s);
}
}

/**
* ALGORITHM :
* ---------
* 1. Start
* 2. Accept a date from the user.
* 3. Check whether it is a leap year or not.
* 4. Count and print the corresponding day number.
* 5. End
12*/
/*
OUTPUT :
------
Enter the Date (dd/mm/yyyy)
Day : 1
Month : 10
Year : 1991
Day No. = 274
*/


No comments:

Post a Comment

Back to Top