Wednesday, November 12, 2014

How To Calculate Interest Rate in C

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

int main()
{
    int balance,
        date_in,
        month_in,
        year_in,
        date_out,
        month_out,
        year_out,
        duration;
        
    float interest_rate,
          final_balance;
          
    char input;
    
    balance       = 0;
    interest_rate = 0;
    final_balance = 0;
 int days_per_month[] = {0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // Not a leap year 
    int days_per_month_leap[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // Leap year
    
    while (1)
    {   
        printf("Input customer balance : "); scanf("%d", &balance); 
  
  if(balance <= 100) 
        {
             interest_rate = 5;
             printf("your balance is <= $100, your interest rate is : %.f%%/30 days\n", interest_rate);
        }
        else if(balance > 100 && balance <= 1000)
        {
            interest_rate = 7.5;
          printf("your balance is <= $1.000, your interest rate is : %.1f%%/30 days\n", interest_rate);
  }
        else if(balance > 1000)
        {
             interest_rate = 10;
            printf("your balance is > $1.000, your interest rate is : %.f%%/30 days\n", interest_rate);
        }
  
  printf("Date\n");
  printf("  In     : "); scanf("%d-%d-%d", &date_in, &month_in, &year_in); 
        printf("  Out    : "); scanf("%d-%d-%d", &date_out, &month_out, &year_out);
        
        duration = 0;
        
        if(year_in == year_out)
        {
         int i;
                   
   if(month_in == month_out) duration += (date_out - date_in);
         
   else if(year_in % 4 == 0 && year_in % 100 != 0 || year_in % 400 == 0)
                {
           duration += days_per_month_leap[month_in] - date_in;
          }
          else duration += days_per_month[month_in] - date_in;
          
          for(i = month_in + 1; i < month_out; i++)
                {
                 if(year_in % 4 == 0 && year_in % 100 != 0 || year_in % 400 == 0) duration += days_per_month_leap[i];
                    else duration += days_per_month[i];
                }

             duration += date_out;
        }
        
        else                                   
        {
            int i, century_in, falseleap = 0;
            century_in = ((year_in / 100) + 1) * 100;

            if(year_in % 4 == 0 && year_in % 100 != 0 || year_in % 400 == 0) 
                duration = days_per_month_leap[month_in] - date_in;
            else
                duration = days_per_month[month_in] - date_in;

            for(i = month_in + 1; i <= 12; i++)              
            {
                if(year_in % 4 == 0 && year_in % 100 != 0 || year_in % 400 == 0)
                    duration += days_per_month_leap[i];
                else
                    duration += days_per_month[i];
            }

            for(i = 1; i < month_out; i++)                     
            {
                if(year_out % 4 == 0 && year_out % 100 != 0 || year_out % 400 == 0)
                    duration += days_per_month_leap[i];
                else
                    duration += days_per_month[i];
            }

            int leapcount_in = year_in / 4;                     
            int leapcount_out = year_out / 4;                     
           
      if(year_out % 4 == 0) leapcount_out -= 1;

            int leaptotal = leapcount_out - leapcount_in;        

            for(i = century_in; i < year_out; i += 100)          
            {
                    if((i % 400) != 0)
                            falseleap += 1;
            }

            duration += 365 * (year_out - year_in - 1) + date_out + leaptotal - falseleap;
     } 
    
     if(duration <= 0) return(-1); //Input error
  printf("You had saved your money for %d days\n", duration);
  
  final_balance = balance;
  while(1)
        {
   if(duration >= 30)
            {
    duration -= 30;
    final_balance += (interest_rate * (final_balance/100));
    
    if(final_balance > 100 && final_balance <= 1000) interest_rate = 7.5;
          else if(final_balance > 1000) interest_rate = 10;
   }
   
   else if(duration < 30 && duration > 0)
            {
    duration = 0;
    final_balance += ((duration/30) * interest_rate * (final_balance/100));
   }
   else if(duration == 0) break;
  }
  
        printf("\nYour final balance is : %.6f", final_balance);
        
        fflush(stdin);
        printf("\nDo you want to restart the program? (y/n) "); scanf("%c", &input);
        system("cls");
        
  if(input == 'n') return 0;
 }
}

0 komentar:

Post a Comment