HACKER RANK Algorithms - Implementation - Library Fine - Solution.'C'

                             Algorithms - Implementation - Library Fine - Solution
Problem Statement
The Head Librarian at a library wants you to create a program that calculates the fine for returning a book after the return date. You are given the actual and the expected return dates. Calculate the fine as follows:
  1. If the book is returned on or before the expected return date, no fine will be charged. In other words, the fine is 0.
  2. If the book is returned in the same calendar month as the expected return date, the fine = 15 Hackos × the number of late days.
  3. If the book is not returned in the same calendar month but in the same calendar year as the expected return date, the fine = 500 Hackos × the number of late months.
  4. If the book is not returned in the same calendar year, the fine is fixed at 10000 Hackos.
Input 
You are given the actual and the expected return dates in D M Y format on two separate lines. The first line contains the D M Y values for the actual return date and the next line contains the D M Y values for the expected return date. Here's a sample:
9 6 2015
6 6 2015
Constraints: 
1D31 
1M12 
1Y3000 
The given date is a valid date on a Gregorian calendar.
Output 
Print a single value representing the fine. 
The sample output for the above input would be 45
Since the actual return date is 3 days later than expected, the fine is calculated as 15×3=45 Hackos.

Solution:

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

int main() {

      int d,d1,m,m1,y,y1,n1=0,n2=0,n3=0,n=0;
     scanf("%d%d%d",&d,&m,&y);
    scanf("%d%d%d",&d1,&m1,&y1);
   if(y1>y)
        printf("%d",n);
   else if(d1>=d && m1>=m && y1>=y) printf("%d",n);
    
    else if(d1<d || m1<m || y1<y)
   {
        if(y1!=y)
        {
          n1=10000;
        }
   
        else if(m1<m && y1==y)
        {
          m=m-m1;
          n2=500*m;
        }
   
        else if(d1<d && m1==m && y1==y)
        {
        n3=(d-d1)*15;
        }
        n=n1+n2+n3;
    printf("%d",n);
    }
       return 0;
}

Thanks for Visiting, Hope this helps you....

Comments

Popular Posts