Wednesday, November 12, 2014

How To Calculate Denominations in ATM Machine

#include <stdio.h>
#include <conio2.h>

void hitung(int amount, int *denom100, int *denom50, int *denom20, int *denom10)
{
   int restofmoney;
   
   *denom100 = amount / 100000;
   restofmoney = amount - (*denom100 * 100000);
  
  *denom50 = restofmoney / 50000;
  restofmoney = restofmoney - (*denom50 * 50000);
  
  *denom20 = restofmoney / 20000;
   restofmoney = restofmoney - (*denom20 * 20000);
  
  *denom10 = restofmoney / 10000;
}

int periksa(int amount)
{   
   if(amount % 10000 == 0)
   {
          return 1;
     }
     else return 0;
}

int main ()
{
  int amount,
   denom100 = 0,
   denom50 = 0,
   denom20 = 0,
   denom10 = 0;
  
  printf("Jumlah uang: "); scanf("%d", &amount);
  
  if(periksa(amount))
  {         
  hitung(amount, &denom100, &denom50, &denom20, &denom10);
 
   printf("%d lembar Rp100.000,-\n", denom100);
   printf("%d lembar Rp 50.000,-\n", denom50);
   printf("%d lembar Rp 20.000,-\n", denom20);
   printf("%d lembar Rp 10.000,-\n", denom10);
 }
 else printf("Tidak dapat dipecah");
 
  getch();
  return 0;
}

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;
 }
}

How To Read and Write File in C

Database FoodDB.txt must be in the same folder with this program


#include <stdio.h>
#include <conio2.h>
#include <string.h>

// Create data structure 'items'
typedef struct
{
    char ItemsCode[6],
         ItemsName[51];
         
    unsigned int Price,
                 Qty,
                 MinimumQty,
                 MaximumQty;
} items;
 
// DeleteItems function used to delete item from database
void DeleteItems(items data[])
{
    signed int index;
    
    // We assigned the index from each variable to zero
    for(index = 0; index < 1000; index++)
    {
        data[index].Price = 0;
        data[index].Qty = 0;
        data[index].MinimumQty = 0;
        data[index].MaximumQty = 0;
    }
}

// CopyItems function used to copy item to database
int CopyItems(items data[])
{
    signed int index = -1;
    
    // Open and read file FoodDB.txt
    FILE *buffer;
    buffer = fopen("FoodDB.txt","r");
    
    // A condition for program to read the databse. Each data we separate by #
    while(!feof(buffer))
    {
        index++;
        fscanf(buffer,
               "%[^#]#%[^#]#%d#%d#%d#%d\n",
               &data[index].ItemsCode,
               &data[index].ItemsName,
               &data[index].Price,
               &data[index].Qty,
               &data[index].MinimumQty,
               &data[index].MaximumQty);
    }
    fclose(buffer);
    return index+1;
}

// WriteFile function used to write file to database
void WriteFile(items data[], int amount)
{
    unsigned int index = 0;
    
    // Open and write file FoodDB.txt
    FILE *buffer;
    buffer = fopen("FoodDB.txt","w");
    
     // A condition for program to write the databse. Each data we separate by #
    for(index = 0; index < amount; index++)
    fprintf(buffer,
            "%s#%s#%d#%d#%d#%d\n",
            data[index].ItemsCode,
            data[index].ItemsName,
            data[index].Price,
            data[index].Qty,
            data[index].MinimumQty,
            data[index].MaximumQty);
    
    fclose(buffer);
}

// PrintItems function used to show or print items in database
void PrintItems(items data[], int amount)
{
    unsigned int index;
    
    // Print data to the screen
    printf("\nNo. %-8s %-29s %3s %3s %3s %4s %s\n",
           "Code",
           "Items Name",
           "Price",
           "Qty",
           "Min",
           "Max",
           "Status");
    
    // Statement for looping data from the beginning until the end of data       
    for(index = 0; index < amount; index++)
    {
        printf("%2d. %-8s %-29s %5d %3d %3d %4d",
               index+1,
               data[index].ItemsCode,
               data[index].ItemsName,
               data[index].Price,
               data[index].Qty,
               data[index].MinimumQty,
               data[index].MaximumQty);
        
        // Item qty status, if the qty below the minimum qty, the screen displayed "Order" message, if not "Ok" message        
        if(data[index].Qty <= data[index].MinimumQty) printf(" Order\n");
        else printf(" Ok\n");
    }
}

// InputItems function used to input item from user that will be written in database when user call WriteFile function
void InputItems(items data[], int *amount)
{
    char choice ='y';
    
    // Condition while user input item data, if user want to input data again, then type y or Y and the program will displayed list below again
    while(choice == 'y')
    {
        clrscr();
        printf("Input New Item Into Database\n");
        fflush(stdin);
        printf("Item Code     : "); scanf("%s", data[*amount].ItemsCode);
        printf("Item Name     : "); scanf("%s", data[*amount].ItemsName);
        printf("Price         : "); scanf("%d", &data[*amount].Price);
        printf("Qty           : "); scanf("%d", &data[*amount].Qty);
        printf("Minimum Stock : "); scanf("%d", &data[*amount].MinimumQty);
        printf("Maximum tock  : "); scanf("%d", &data[*amount].MaximumQty);
        printf("\n\nWant to enter data again [Y/T] ?"); scanf("%c", &choice); getche();
        
        if (choice == 'y') (*amount)++;
    }
    (*amount)++;
}

// ChangeItems function used to change old item data in database to new data
void ChangeItems(items data[], int amount)
{
    unsigned int index,
                 Price,
                 Qty,
                 MinimumQty,
                 MaximumQty;

    char choice = 'y',
         ItemsCode[6],
         ItemsName[31];
    
    clrscr();
    printf("Change Item In Database\n");
    printf("OLD DATA\n"); fflush(stdin);
    printf("Item Code : ");  // User input the item code that will be changed
    scanf("%s", ItemsCode);
     
    for(index = 0; index < amount; index++)
    {
        if(strcmp(data[index].ItemsCode, ItemsCode) == 0)
        break;
    }
    
    // Program shown data from item code that user inputted before
    printf("Item Code     : %s\n", data[index].ItemsName);
    printf("Price         : %d\n", data[index].Price);
    printf("Qty           : %d\n", data[index].Qty);
    printf("Minimum Stock : %d\n", data[index].MinimumQty);
    printf("Maximum Stock : %d\n", data[index].MaximumQty);
    
    printf("\nNEW DATA\n");
    fflush(stdin);
    // User input new data from item code that user inputted before
    printf("Item Code     : "); scanf("%s", ItemsCode);
    printf("Item Name     : "); scanf("%s", ItemsName);
    printf("Price         : "); scanf("%d", &Price);
    printf("Qty           : "); scanf("%d", &Qty);
    printf("Minimum Stock : "); scanf("%d", &MinimumQty);
    printf("Maximum Stock : "); scanf("%d", &MaximumQty);
    printf("\n\nIs the new data correct [Y/T] ?"); choice = getche();
    
    // When user press Y, then the program will replaced old data by new data
    if(choice=='y')
    {
        strcpy(data[index].ItemsCode, ItemsCode);
        strcpy(data[index].ItemsName, ItemsName);
        data[index].Price = Price;
        data[index].Qty = Qty;
        data[index].MinimumQty = MinimumQty;
        data[index].MaximumQty = MaximumQty;
    }
}

// Title function displayed program title
void Title()
{
     // Program title
     printf("======================================\n");
     printf("            KANTIN KENYANG            \n");
     printf(" Jl. Maju Terus Pantang Mundur No. 45 \n");
     printf("         Jakarta Pusat 12000          \n");
     printf("======================================  ");
     printf("\n\n");
}
 
int main()
{
    items data[1000];
    
    int stat = 1,
        amount,
        choice;
    
    DeleteItems(data);
    amount = CopyItems(data);
    
    while(stat)
    {
        clrscr();
        Title(); // call the Title function
        printf("MENU\n");       

        printf(" 1. Show Items\n");
        printf(" 2. Input New Item\n");
        printf(" 3. Change Item Data\n");
        printf(" 0. Exit\n\n");
        
        printf("Choice : "); scanf("%d", &choice);
        
        // Statement that will be selected by user
        switch(choice)
        {
            case 1: PrintItems(data, amount);
                    stat = 1;
                    printf("\nPress any key to continue");
                    getch();
                    break;
            case 2: InputItems(data, &amount);
                    stat = 1;
                    getch();
                    break;
            case 3: ChangeItems(data, amount);
                    stat = 1;
                    getch();
                    break;
            case 0: WriteFile(data, amount);
                    stat = 0;
                    return 0;
            default : stat = 1;
                      break;
        }
    }
    
    getch();
    return 0;
}