Library Fine HackerRank Solution in C, C++, Java, Python

Your local library needs your help! Given the expected and actual return dates for a library book, create a program that calculates the fine (if any). The fee structure is as follows:

  1. If the book is returned on or before the expected return date, no fine will be charged (i.e.:fine=0) .
  2. If the book is returned after the expected return day but still within the same calendar month and year as the expected return date, fine = 15 Hackos * (the number of days later) .
  3. If the book is returned after the expected return month but still within the same calendar year as the expected return date, the fine = 500 Hackos * (the number of months late).
  4. If the book is returned after the calendar year in which it was expected, there is a fixed fine of 10000 Hackos.

Charges are based only on the least precise measure of lateness. For example, whether a book is due January 1, 2017 or December 31, 2017, if it is returned January 1, 2018, that is a year late and the fine would be 10000 Hackos.

Example

The first values are the return date and the second are the due date. The years are the same and the months are the same. The book is  days late. Return .

Function Description

Complete the libraryFine function in the editor below.

libraryFine has the following parameter(s):

  • d1, m1, y1: returned date day, month and year, each an integer
  • d2, m2, y2: due date day, month and year, each an integer

Returns

  • int: the amount of the fine or 0 if there is none

Input Format

The first line contains 3 space-separated integers,d1,m1,y1 , denoting the respective day,month , and year on which the book was returned.

The second line contains 3 space-separated integers,d1,m2,y2 denoting the respective day,month, and year on which the book was due to be returned.

Sample Input

9 6 2015

6 6 2015

 

Sample Output

45

Library Fine HackerRank Solution in C

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    
        var actual = DateTime.ParseExact(
            Console.ReadLine().Replace(' ', '/'), "d/M/yyyy", CultureInfo.InvariantCulture);
        var expected = DateTime.ParseExact(
            Console.ReadLine().Replace(' ', '/'), "d/M/yyyy", CultureInfo.InvariantCulture);
        
        int fine;
        
        if (actual <= expected)
        {
            Console.WriteLine(0);
        }
        else if (actual.Year != expected.Year)
        {
            Console.WriteLine(10000);
        }
        else if (actual.Month != expected.Month)
        {
            Console.WriteLine((actual.Month - expected.Month) * 500);
        }
        else if (actual.Day != expected.Day)
        {
            Console.WriteLine((actual.Day - expected.Day) * 15);
        }
    }
}

 

 

Attempt Library Fine HackerRank Challenge 

Link – https://www.hackerrank.com/challenges/library-fine/

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/cut-the-sticks-hackerrank-solution/

 

Leave a Comment