Friday, 4 May 2012

Calculating Depreciation in C


#include<stdio.h>
#include<conio.h>
#include<ctype.h>

void ddb(float val,int n);
void syd(float val,int n);
void writeout(int year,float depreciation,float value);

main()
{
 int n,year,choice=1;
 float val,deprec;
 char answer1 = 'Y', answer2 = 'Y';
 while(toupper(answer1)!='N')
 {
  if(toupper(answer2)!='N')
  {
   printf("\nOriginal Value");
   scanf("%f",&val);
   printf("Number of years:");
   scanf("%d",&n);
  }
  printf("\nMethod: (1-SL 2-DDB 3-SYD)");
  scanf("&d",choice);
  
  switch(choice)
  {
  case 1:
    printf("Straight Line Method\n\n");
    break;
  case 2:
    printf("Double Declining Balance Method\n\n");
    ddb(val,n);
    break;
  case 3: 
    printf("Sum-of-the-years Digits Method\n\n");
    syd(val,n);
  }
  printf("\n\nAnother Calculation? (Y/N)\n");
  scanf("%s",&answer1);
  if(toupper(answer1)!='N')
  {
   printf("\nEnter new set of data:\n");
   scanf("%s",&answer2);
  }
 }
 for(year=1;year<=n;++n)
 {
  val -= deprec;
  writeout(year,deprec,val);
 }
 deprec=val/n;
 for(year=1;year<=n;++year)
 {
  val-=deprec;
  writeout(year,deprec,val);
 }
 return;
}

void ddb(float val,int n)
{
 float deprec;
 int year;
 for(year=1;year<=n;++year)
 {
  deprec=2*val/n;
  val-=deprec;
  writeout(year,deprec,val);
 }
 return;
}

void syd(float val,int n)
{
 float tag,deprec;
 int year;
 tag=val;
 for(year=1;year<=n;++year)
 {
  deprec=(n-year+1)*tag/(n*(n+1)/2);
  val-=deprec;
  writeout(year,deprec,val);
 }
 return;
}

void writeout(int year,float depreciation,float value)
{
 printf("End of the year %d",year);
 printf("  Depreciation:%f",depreciation);
 printf("  Current Value:%f",value);
 return;
}


There are bugs in this code (or code missing)

;)

No comments:

Post a Comment