Day 26: Nested Logic - 30 Days Code - IndianTechnoEra
Latest update Android YouTube

Day 26: Nested Logic - 30 Days Code

Introduction:

Welcome to Day 26 of the 30 Days of Code challenge! Today, we'll test your understanding of nested conditional statements. If you haven't already, check out the Tutorial tab for insights on testing.


The Challenge:

Your local library needs your assistance! Given the expected and actual return dates for a library book, your task is to 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.

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 is 15 * 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 is 500 * 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 10,000.


Constraints:

1 ≤ day, month, year ≤ 31 (for both actual and expected return dates)


Format & Sample:

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).


Output Format:

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


Input Format:

9 6 2015

6 6 2015


Output Format:

45


Explanation:

Given the following return dates:

Returned: 9th June 2015

Due: 6th June 2015


Because 2015 is the same year, and 6th June is the same month, the book is returned late. The fine is 15 * the number of days late, i.e., 15 * (9 - 6) = 45. We then print the result of 45 as our output.


Code Breakdown:

Reading Input:

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */

    return 0;

}


Calculating Fine:

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */

    int returnedDay, returnedMonth, returnedYear;

    int dueDay, dueMonth, dueYear;


    cin >> returnedDay >> returnedMonth >> returnedYear;

    cin >> dueDay >> dueMonth >> dueYear;


    // Implement fine calculation here


    return 0;

}


Printing Output:

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */

    int returnedDay, returnedMonth, returnedYear;

    int dueDay, dueMonth, dueYear;


    cin >> returnedDay >> returnedMonth >> returnedYear;

    cin >> dueDay >> dueMonth >> dueYear;


    // Implement fine calculation here


    return 0;

}


Final Code:

#include <iostream>


using namespace std;


int main() {

    int returnedDay, returnedMonth, returnedYear;

    int dueDay, dueMonth, dueYear;


    cin >> returnedDay >> returnedMonth >> returnedYear;

    cin >> dueDay >> dueMonth >> dueYear;


    int fine = 0;


    if (returnedYear > dueYear ||

        (returnedYear == dueYear && returnedMonth > dueMonth) ||

        (returnedYear == dueYear && returnedMonth == dueMonth && returnedDay > dueDay)) {

        if (returnedYear == dueYear) {

            if (returnedMonth == dueMonth) {

                fine = 15 * (returnedDay - dueDay);

            } else {

                fine = 500 * (returnedMonth - dueMonth);

            }

        } else {

            fine = 10000; // Fixed fine for returning after the expected calendar year

        }

    }


    cout << fine << endl;


    return 0;

}

This concludes Day 26 of the 30 Days of Code challenge. You've successfully implemented a program to calculate library fines based on the return dates. Keep up the great work!

إرسال تعليق

Feel free to ask your query...
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.