Find the 183rd Day
When working in C# we find the language in itself supports so many functions. However, knowing about these will not only save you time and effort, but it can help you to find a more optimized solution to your problem. In this problem today, our aim is to figure out the date that would be on the 183rd day from a given date.
Problem Statement
You’re provided an input date in the form of simple inputs( mentioned below ) and your aim is to calculate the date on the 183rd day from that date.
Given
Expected Input
12
January
2002
Expected Output
14 July 2002
Approach
There are two things which we are going to use for this problem,
- Enum data type
- DateTime data type
ENUM
Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain.
DATETIME
It is a predefined structure type in C# Language. It has several inbuilt methods for you to easily work on DateTime type of values.
Note
Attaching articles for both if you are a novice.
Now to proceed with this problem we need to check 2 things,
- Converting the given input into the required format.
- Presenting output in the required format.
We have used enum in order to convert the month name into the required number and then again converting it back to the month name.
CODE
using System.IO;
using System;
class Program {
enum months {
January = 1,
Feburary = 2,
March = 3,
April = 4,
May = 5,
June = 6,
July = 7,
August = 8,
September = 9,
October = 10,
November = 11,
December = 12
}
static void Main() {
int dd = Int32.Parse(Console.ReadLine());
string m = Console.ReadLine();
int yr = Int32.Parse(Console.ReadLine());
int mn = (int)((months) Enum.Parse(typeof(months), m));
DateTime d1 = new DateTime(yr, mn, dd);
DateTime d2 = d1.AddDays(183);
System.Console.WriteLine("Initial DateTime = {0:y} {0:dd}", d1);
System.Console.WriteLine("\nNew DateTime (After adding days) = {0:dd} {0:MMMM} {0:yyyy}", d2);
}
}
Input value
Output Value
REFERENCES
- Enum in C#
- DateTime In C#