0

I have been asked why a program written in C does not work properly. The program calculates costs of two plans(A and B) and it shows which one is expensive and how much different their costs. It keeps sending its flow to the 2nd if block. I've tested it with Dev-C++. The following is a screenshot that I've got:

#include <stdio.h>

int main(void) {
  
  double dt=162.0; //daytime
  double et=61.0; //evening time
  double wt=66.0; //weekend time
  
  double an=0.0; //last result A
  double bn=0.0; //last result B
  double di=0.0; //margin
  double a=0.0;
  double b=0.0;

  a = dt-100;
  b = dt-250;

  if(a<=0)
  {
    a=0;
  }
  if(b<=0)
  {
    b=0;
  }

  an=(a*0.25)+(et*0.15)+(wt*0.20);
  bn=(b*0.45)+(et*0.35)+(wt*0.25);  

  printf("Plan A costs  %.4f\n", an);
  printf("Plan B costs  %.4f\n", bn);

  di=an-bn;

  if(di == 0)
  {
    printf("Different $%.4f\n", di);
    printf("Plan A and B are the same price.");
  }
  else if(di > 0) /* this block keeps running */
  {
    printf("Different $%.4f\n", di);   
    printf("Plan B is cheapest.");
  }
  else if(di < 0)
  {
    printf("Different $%.4f\n", di);   
    printf("Plan A is cheapest.");
  }
  return 0;
}
Jim Sjoo
  • 326
  • 2
  • 5

0 Answers0