21/03/2023

Day 26 Nested Logic Hackerrank Solution in C++ | Hackerrank

Day 26 Nested Logic Hackerrank Solution in C++. Today's challenge puts your understanding of nested conditional statements to the test. You already have the knowledge to complete this challenge, but check out the Tutorial tab for a video on testing.

Day 26 Nested Logic Hackerrank Solution in C++

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:

  • If the book is returned on or before the expected return date, no fine will be charged (i.e.: fine = 0.
  • If the book is returned after the expected return day but still within the same calendar month and year as the expected return date, the fine = 15 Hackos * (the number of days late).
  • 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).
  • If the book is returned after the calendar year in which it was expected, there is a fixed fine of 10000 Hackos.

Example

d1, m1, y1 = 12312014 returned date
d2, m2, y2 = 112015 due date

The book is returned on time, so no fine is applied.

d1, m1, y1 = 112015 returned date
d2, m2, y2 = 12312014 due date

The book is returned in the following year, so the fine is a fixed 10000.

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 guaranteed that the dates will be valid Gregorian calendar dates. 

Output Format

Print a single integer denoting the library fine for the book received as input.

Sample Input

STDIN       Function
-----       --------
9 6 2015    day = 9, month = 6, year = 2015 (date returned)
6 6 2015    day = 6, month = 6, year = 2015 (date due)

Sample Output

45

Submit Your Solution Here: Click Here

Day 26 Nested Logic Hackerrank Solution in C++


#include <bits/stdc++.h>
using namespace std;

int libraryFine(int d1, int d2, int m1, int m2, int y1, int y2)
{
	if (y2 > y1)
		return 0;

	if (y1 > y2)
		return 10000;
	else if (y2 == y1 && m1 > m2)
		return abs(m2 - m1) *500;
	else if (y2 == y1 && m2 == m1 && d2 < d1)
		return abs(d2 - d1) *15;
	else
		return 0;
}

int main()
{
	int d1, d2, m1, m2, y1, y2;
	cin >> d1 >> m1 >> y1;
	cin >> d2 >> m2 >> y2;
	cout << libraryFine(d1, d2, m1, m2, y1, y2);
	return 0;
}

Nested Logic Explanation


We have to solve the problem according to a given task, the task is given below. There are 4 cases given in the task our solution should pass all 4 cases according to the condition and print the Hackos or fine. Let's take an example and try to find out the solution to this problem.

Suppose we have given the below input. The input format is first DD: MM: YYYY

Actual =====>15 12 2015
Expected ===>15 6 2015

Now according to our logic in the program.

diff = 15 - 15 = 0.
mdiff = 12 - 6 = 6.
ydiff = 2015 - 2015 = 0.

Now the first condition is failed actual year - expected year so according to the ternary operator second part will be executed, so according to the second part (Month-Month1>0&&ydiff==0) this part is passed cause 12 - 6 = 6 and the year difference(actual year - expected ) is 0 so Month difference ((actual year - expected) * 500). The month difference is 6 * 500 = 3000. So this is a Hackos we have to pay.

The Output of Nested Logic Hackerrank Solution


The Output of Nested Logic Hackerrank Solution

Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: