This post explains the HackerRank 30 Days of Code problem Nested Logics step by step: the problem statement, input and output format, constraints, a sample test case and a working solution in Java that you can study and run.

Day 26: Nested Logic
Objective
CodingHumans today's challenge puts your understanding of nested conditional statements to the test. You already have the knowledge to complete this challenge, Lets check your conditiona knowledge
coding humans.
Task
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 x (the number of days late).
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 x (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 1000 hackos.
Input Format
The first line contains 3 space-separated integers denoting the respective day, month , and year on which the book was actually returned.
The second line contains 3 space-separated integers denoting the respective day ,month, and year on which the book was expected to be returned (due date).
Constraints
1<= D <=31
1<= M <=12
1<=Y <=3000
It is guarented that the dates will be valid Gregorian calendar dates.
Output Stylec
Print a single integer denoting the library fine for the book received as input.
Sample Input
9 6 2015
6 6 2015
Sample Output
45
Explanation
Given the following return dates:
Actual: Da= 9, Ma=6, Ya=2015
Expected: De=6,Me=6,Ye=2015
Because Ye == Ya, we know it is less than a year late.
Because Me == Ma, we know it's less than a month late.
Because De < Da, we know that it was returned late (but still within the same month and year).
Per the library's fee structure, we know that our fine will be 15 hackos x (# days late) . We then print the result of 15 x ( Da x De ) =15 x ( 9-6 ) = 45 as our output.
Nested Logics Solution in Java
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int day = sc.nextInt();
int month = sc.nextInt();
int year = sc.nextInt();
int dayExpected = sc.nextInt();
int monthExpected = sc.nextInt();
int yearExpected = sc.nextInt();
int fine = 0;
if(year > yearExpected){
fine = 10000;
}else if(year == yearExpected && month > monthExpected){
fine = (month - monthExpected) * 500;
}else if(year == yearExpected && month == monthExpected && day > dayExpected){
fine = (day - dayExpected) * 15;
}
System.out.println(fine);
}
}
If you have any doubts regarding this problem or need the solution in other programming languages then leave a comment down below .
Tip: try to solve the problem yourself before reading the solution. Use the code as a reference to check your approach, and make sure you understand why it works.
More HackerRank solutions All coding problems MCQ practice
See everything in one place: Coding Problems and Solutions
Comments
Post a Comment