A calculation problem in c

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Citrons
    New Member
    • Aug 2012
    • 1

    #1

    A calculation problem in c

    Contributions wont calculate
    Code:
    #include <stdio.h> 
    #include <conio.h> 
    int main() 
    { float tax;
     float salary;
     float taxable;
     float taxmo;
     float contributions;
     float pension;
     float ni;
     float pensionmo;
     float nimo;
     printf("Please Enter Gross Salary\n");
     scanf("%f", &salary);
     contributions = 6/100*salary + 2/100*salary;
     taxable = salary - contributions;
     if (taxable <= 4000) { tax = 0 ; } else { tax = 0;
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:
    Code:
    contributions = 6/100*salary + 2/100*salary;
    6/100 is 0. 2/100 is 0. 0 times anything is 0.

    Remember, with ints, 100 goes into 6 zero times.

    You might try to ue 6.0f/100.0f and 2.0f/100.0f.
    The f tells the compiler to create the constants as float because 2.0 by default is a double. A double is larger than a float so you starting getting "possible loss of data" warnings from the compiler if you try to put a double into a float.

    Personally, would use 2.0/100.0 and chnage all thse floats to double.

    Comment

    Working...