We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
static void Main(string[] args)
{
// Read and assign the returned and due dates
DateTime returnedDate = AssignDate(Console.ReadLine());
DateTime dueDate = AssignDate(Console.ReadLine());
// Calculate and display the fine
if (returnedDate <= dueDate)
{
Console.WriteLine(0); // No fine if returned on or before the due date
}
else
{
int fine = 0;
if (returnedDate.Year > dueDate.Year)
{
// Fine is fixed at 10000 if the return is in a different year
fine = 10000;
}
else if (returnedDate.Month > dueDate.Month)
{
// Fine for being late by months within the same year
fine = 500 * (returnedDate.Month - dueDate.Month);
}
else if (returnedDate.Day > dueDate.Day)
{
// Fine for being late by days within the same month and year
fine = 15 * (returnedDate.Day - dueDate.Day);
}
Console.WriteLine(fine);
}
}
// Method to parse input and return a DateTime object
public static DateTime AssignDate(string dateInput)
{
string[] dateSplit = dateInput.Split(' ');
int day = Convert.ToInt32(dateSplit[0]);
int month = Convert.ToInt32(dateSplit[1]);
int year = Convert.ToInt32(dateSplit[2]);
return new DateTime(year, month, day); // Construct and return the DateTime
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 26: Nested Logic
You are viewing a single comment's thread. Return to all comments →
` C# CSharp