Date Program
This program is asked in ISC Computer Science Practical Paper 2019
Q.1 - Design a Program to accept a day number (between 1 and 366), year (in 4 digit) from the user to generate and display the corresponding date. Also, accept 'N' (1 <= N <= 100) from the user to compute and display the future date corresponding to 'N' days after the generated date. Display an error message if the value of the day number, year, and N are not within the limit or not according to the condition specified.
Example :-
Input : Day Number : 255
N Days : 42
Year : 2018
Output : Date is : 12 TH September 2018 N Date is : 24 TH October 2018
Question Paper :-
Algorithm and Program given below :-
Algorithm :-
Define an Array containing days number leaving blank(0th position)
Define another Array containing Month names leaving blank(0th position)
Now Input Day Number, N number of days, Year from the user
In a variable store the sum of Day number and N number (for calculating date after n days)
Now check if the year entered is leap year or not
If it is leap year than increment the days number in the array from 2nd position by 1
Now check if the days entered are in the range or not
If the days are in the range than calculate the date (date can be calculated by subtracting the entered days number from the original days number)
If the days are not in the range than change the value of k to 1 and display days are out of range and Program will not execute further.
Now check if the 'N' number of days are in the range or not.
If it is in range than calculate date after 'N' days
If it is not in the range than change the value of k to 1 and display 'N days are out of range' and Program will not execute further.
If days and N days both are in the range than Display Date and Date after N days.
Another condition is If the year entered is not a Leap year
Than do not increment the value of days number in the array.
Rest of the program will remain the same
Compile and Run the Program
Program :-
import java.io.*; import java.util.*; class Date { public static void main(String args[]) { Scanner in= new Scanner(System.in); int n,day,year,i,p,k=0; int ar[]={0,31,59,90,120,151,181,212,243,273,304,334,365}; String br[]={"","January","February","March","April","May","June","July","August","September","October","November","December"}; System.out.println("Enter Day Number"); day= in.nextInt(); // Input Day Number System.out.println("Enter N Number"); n=in.nextInt(); // Input N number of days System.out.println("Enter Year"); year=in.nextInt(); // Input Year p=day+n; // Storing Day no. after N days int a=1,b=1,date=0,nday=0,year1=year; if(year%4==0) //Condition for Leap year { for(i=2;i<=12;i++) { ar[i]+=1; } if(day>=1&&day<=366) // condition for correct day number { for(i=0;i<=12;i++) { if(ar[i]<day) { a=i; } if(ar[i]>day) { break; } } date=day-ar[a]; // Calculating Date } else { k=1; } if(k==1) { System.out.println("Days are Out of Range"); } else if(k!=1 && n>=1&&n<=100) { if(p>ar[12]) { p=p-ar[12]; year1=year1+1; } for(i=0;i<=12;i++) { if(ar[i]<p) { b=i; } if(ar[i]>p) { break; } } nday=p-ar[b]; //Storing date after n days. } else { k=1; System.out.println("N Days are Out of range "); } if(k!=1) { System.out.println("Date is : "+date+" TH " + br[a+1]+" "+year); //printing date System.out.println("N Date is : "+nday+" TH " + br[b+1]+" "+year1);// printing date after n days } } else { if(day>=1&&day<=365) { for(i=0;i<=12;i++) { if(ar[i]<day) { a=i; } if(ar[i]>day) { break; } } date=day-ar[a]; //calculating & storing date } else { k=1; } if(k==1) { System.out.println("Days are Out of Range"); } if(n>=1&&n<=100) { if(p>ar[12]) { p=p-ar[12]; year1=year1+1; } for(i=0;i<=12;i++) { if(ar[i]<p) { b=i; } if(ar[i]>p) { break; } } nday=p-ar[b]; } else { k=1; System.out.println("N Days are Out of range "); } if(k!=1) { System.out.println("Date is : "+date+" TH " + br[a+1]+" "+year); //printing date System.out.println("N Date is : "+nday+" TH " + br[b+1]+" "+year1);// printing date after n days } } } // end of main method } // end of class
Note : The program is written an easy way so try to understand it. If you face any problem in understanding this program than feel free to contact/comment.
Source Code : Click here
Program Video : Watch Now
All the Best :)
Keep Learning :)
Comments