Thread: Problem with math in an if/else code in C.

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    32

    Problem with math in an if/else code in C.

    Hi, I'm trying to write a code to ask for and display a person's salary under these conditions:

    "If the hours worked are less than or equal to 40, the person receives $8.00 per hour; otherwise, the person receives $320.00 plus $12.00 for each hour worked over 40 hours."

    I have the first part down, the only thing I'm not sure how to do is the whole "$12.00 for each hour worked OVER 40 hours." Any ideas?

    Here's what I have so far,


    Code:
    #include <stdio.h>
    void main ()
    {
            double hour;
            double salary;
    
    
            printf("Please enter the number of hours you work each week\n");
            scanf("%lf", &hour);
    
    
            if(hour <= 40)
            {
                    salary = 8.00 * hour;
                    printf("Your weekly salary is $%5.2lf.\n", salary);
            }
    
    
            else
            {
                    salary = 320.00 + (12.00 * hour);
                    printf("Your weekly salary is $%5.2lf.\n", salary);
            }
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    "Each hour worked over 40 hours" is computed by subtracting 40 from the number of hours.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    32
    Wowwwwwwwwwwww. Good call man. I was over thinking this way too much. Thanks a lot, I appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math equations help....with C code
    By lilbo4231 in forum C Programming
    Replies: 1
    Last Post: 03-08-2011, 04:49 PM
  2. Need help with simple math code please!
    By MetaMorphicX in forum C Programming
    Replies: 9
    Last Post: 02-18-2010, 07:28 PM
  3. Math + Code
    By turck3 in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 01:44 PM
  4. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM
  5. Math function code - couple probs, tips?
    By Captain Penguin in forum C++ Programming
    Replies: 4
    Last Post: 06-18-2002, 09:54 AM