Thursday 19 January 2017

Day for given date...

Write a program to accept a date in a specific string format (dd/mm/yyyy) and accept the name of the day on 1st January of the corresponding year. Find the day for the given date.
Example :
Input:          Enter the date ( in dd/mm/yyyy) format: 5/7/2001
Enter the Day on 1st January in this year: Monday

Output:                  5/7/2001 : Thursday

Test run the program on the following inputs.

Input date              Day on 1st  January                   Output day for Input date

4/941998              Thursday                        Friday
31/8/1998              Friday                                      Tuesday
6/12/2000             Saturday                        Wednesday

The program should include the part for validating the inputs namely the date and the day on 1st January of the year.


Solution:-

import java.util.*;
class date
{
     int arr[]={31,28,31,30,31,30,31,31,30,31,30,31};
     String str1,day,day1;
     int x,i,dayno,mon,yr,leap1;
     void input()//input date and first day of year.
     {
           Scanner sc=new Scanner(System.in);
           System.out.println("Enter the date (in dd/mm/yyyy) format");
           day=sc.nextLine();
           day1=day;
           i=day.indexOf("/");
           dayno=Integer.parseInt(day.substring(0,i));
           day=day.substring(i+1);
           i=day.indexOf("/");
           mon=Integer.parseInt(day.substring(0,i));
           day=day.substring(i+1);
           yr=Integer.parseInt(day);
           leap1=0;
           if(mon>2)
           leap1=leap(yr);
           System.out.println("Enter the Day on 1st January in this year");
           str1=sc.nextLine();
     }
     int leap(int p)//check for leap year.
     {
           if(p%4==0&&p%100!=0||p%400==0)
                return 1;
           else
                return 0;
     }
     void display()
     {
           if (str1.equalsIgnoreCase("Sunday"))
                x=1;
           else if (str1.equalsIgnoreCase("Monday"))
                x=2;
           else if (str1.equalsIgnoreCase("Tuesday"))
                x=3;
           else if (str1.equalsIgnoreCase("Wednesday"))
                x=4;
           else if (str1.equalsIgnoreCase("Thursday"))
                x=5;
          else if (str1.equalsIgnoreCase("Friday"))
                x=6;
           else if (str1.equalsIgnoreCase("Saturday"))
                x=7;
           for(i=0;i<mon-1;i++)
           {
                dayno=dayno+arr[i];
           }
           dayno=dayno+leap1;
           for(i=0;i< dayno-1;i++)
           {
                 x++;
                 if(x==8)
                      x=1;
           }
           System.out.print(day1+ ":");
           switch(x)
           {
                case 1:System.out.print("Sunday");
                break;
                case 2:System.out.print("Monday");
                break;
                case 3:System.out.print("Tuesday");
                break;
                case 4:System.out.print("Wednesday");
                break;
                case 5:System.out.print("Thursday");
                break;
                case 6:System.out.print("Friday");
                break;
                case 7:System.out.print("Saturday");
                break;
           }
     }
     public static void main(String args[])
     {
           date ob=new date();
           ob.input();
           ob.display();
     }
 }





2 comments:

Goldbach Number ISC 2018

Question: A Goldbach number is a positive even integer that can be expressed as the sum of two odd primes. Note: All even integer numbers ...