Skip to content
Programmingoneonone
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

LEARN EVERYTHING ABOUT PROGRAMMING

HackerRank Library Fine problem solution

YASH PAL, 31 July 2024

In this HackerRank Library Fine problem you have Given the expected and actual return dates for a library book, create a program that calculates the fine.

HackerRank Library Fine problem solution

Topics we are covering

Toggle
  • Problem solution in Python programming.
  • Problem solution in Java Programming.
    • Problem solution in C++ programming.
    • Problem solution in C programming.
    • Problem solution in JavaScript programming.

Problem solution in Python programming.

res = list(reversed([int(x) for x in input().split()]))
due = list(reversed([int(x) for x in input().split()]))

def calc(res, due):
    if res[0] < due[0]:
        return 0
    if res[0] > due[0]:
        return 10000
    if res[1] < due[1]:
        return 0
    if res[1] > due[1]:
        return 500 * (res[1] - due[1])
    if res[2] < due[2]:
        return 0
    if res[2] > due[2]:
        return 15 * (res[2] - due[2])
    return 0
print(calc(res, due))

Problem solution in Java Programming.

import static java.lang.System.out;
import java.util.Scanner;

public class WarmLibraryFine {

    public static void main(String x[]) {
        WarmLibraryFine o = new WarmLibraryFine();
        o.run();
    }

    void run() {
        try (final Scanner in = new Scanner(System.in, "ascii")) {

            final int fine;

            final int aday = in.nextInt();
            final int amon = in.nextInt();
            final int ayr = in.nextInt();

            final int eday = in.nextInt();
            final int emon = in.nextInt();
            final int eyr = in.nextInt();

            if (ayr < eyr) {
                fine = 0;
            } else if (ayr > eyr) {
                fine = 10_000;
            } else if (amon < emon) {
                fine = 0;
            } else if (amon > emon) {
                fine = 500 * (amon - emon);
            } else if (aday > eday) {
                fine = 15 * (aday - eday);
            } else {
                fine = 0;
            }
            out.println(fine);
        }
    }
}

Problem solution in C++ programming.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    int ed,em,ey,ad,am,ay;
    cin>>ad>>am>>ay;
    cin>>ed>>em>>ey;
    if(ay>ey){
        cout<<10000<<endl;
    }else if((ey==ay)&&(am>em)){
        cout<<500*(am-em)<<endl;
    }else if((ad<=em)&&(am<=em)&&(ay<=ey)||(ay<ey)||(am<em)&&(ay<=ey)){
        cout<<0<<endl;
    }else{
        cout<<15*(ad-ed)<<endl;
    }
    return 0;
}

Problem solution in C programming.

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

int main() {
    int d1, m1, y1, d2, m2, y2;
    scanf("%d %d %d %d %d %d", &d1, &m1, &y1, &d2, &m2, &y2);
    if (y1 > y2) 
        printf("10000");
    else
        if (y1 < y2)
            printf("0");
        else
            if (m1 > m2)
                printf("%d", (m1-m2)*500);
            else
                if (m1 < m2)
                    printf("0");
                else
                    if (d1 > d2)
                        printf("%d", (d1-d2)*15);
                    else
                        printf("0");
    return 0;
}

Problem solution in JavaScript programming.

function processData(input) {
    //Enter your code here
    var dates = input.split('n').map(function(date) {
        return date.split(' ').map(function(datePart) {
            return parseInt(datePart, 10);
        });
    });
    if (dates[0][2] > dates[1][2]) {
        console.log(10000);
    } else if (dates[0][2] < dates[1][2]) {
        console.log(0);
    } else if (dates[0][1] > dates[1][1]) {
        console.log((dates[0][1] - dates[1][1]) * 500);
    } else if (dates[0][1] < dates[1][1]) {
        console.log(0);
    } else {
        console.log(Math.max(dates[0][0] - dates[1][0], 0) * 15);
    }
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

Algorithms coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes