Search Tutorials

Tuesday 23 April 2013

Java code to read day number and year its corresponding date

Java program to read the day number and year and showing its corresponding date.

import java.io.*;
class dayNo2date
{
 public static void main(String args[]) throws IOException
 {
 dayNo2date call = new dayNo2date();
 BufferedReader br = new BufferedReader(new
 InputStreamReader(System.in));
 // Read day no. and year
 System.out.print("Enter the Day No. : ");
 int n = Integer.parseInt(br.readLine());
 System.out.print("Enter the Year : ");
 int y = Integer.parseInt(br.readLine());
 call.toDate(n,y);
 }
 public void toDate(int n, 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, m=0;
 // Calculate Month
 while(s<n)
 s+=a[m++];
 s-=a[m-1];
 int d = n-s; // Calculate Day
 // Print the Date
 System.out.println("\nDate = " +d +"-" +m +"-" +y);
 }
}

ALGORITHM:-

1. Accept the day no. and the year from the user.

2. Check whether it is a leap year or not.

3. Count and print the corresponding date.

4. End

OUTPUT:-

Java code to read day number and year its corresponding date


Enter the Day No. : 230

Enter the Year : 2008

Date = 17-8-2008

Related Programs:-

Find the HCF & LCM of two numbers using recursion

Read a Date and give a day number

Multiply two Matrices

Search an element using Linear Search or Binary Search

Bubble Sort, Selection Sort, Insertion Sort and Quick Sort

2 comments:

Back to Top