You are here:

C/if-else

Advertisement


Question
i did this question.
car              discount
800cc               8%
1000cc              5%

write a prog to calculate the net amount.
input: cc, price, discount
print: cc, price, discount, net amount.

but when i compile,there r no errors or warnings. but the if-else part wasnt executed. y?
please help me.

#include<stdio.h>

int main(void)
{

  int cc;
  float price;
  double discount;



  printf("Enter your car cc:");
  scanf("%d",&cc);

  printf("Enter the price of your car:");
  scanf("%f",&price);

  if(cc=800)
     discount=price-(0.05*price);
  else
     discount=price-(0.08*price);

  return 0;
}

Answer
Change:
>if(cc=800)
To:
if(cc == 800)

Note that you have used '=' which is an assignment operator in the condition. Because of that value of cc is getting changed to 800.
Instead use the '==' operator which tests for equality.

Hope this helps....

-ssnkumar

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.