Simple expression

This is a discussion on Simple expression within the C Programming forums, part of the General Programming Boards category; Why is the following evaluated to 8.625000? firstly the expression within brackets are evaluated (3.5 * q) = 9.8 secondly ...

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    Simple expression

    Why is the following evaluated to 8.625000?

    firstly the expression within brackets are evaluated (3.5 * q) = 9.8
    secondly 2.5 * c = 4.5
    lastly, the division operator (3.5 * q) / 1.6) = 6.125

    i can't match the final answer that is printed

    Code:
    #include <stdio.h>
    
    int main()
    {
        int c = 1.8;
        float q = 2.8;
    
       float total = (2.5 * c + (3.5 * q) / 1.6);
       printf("%f", total);
    }

  2. #2
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Quote Originally Posted by Axel
    Code:
    #include <stdio.h>
    
    int main()
    {
        int c = 1.8;//problem is here
        float q = 2.8;
    
       float total = (2.5 * c + (3.5 * q) / 1.6);
       printf("%f", total);
    }
    const double is converted to integer(loss of data).....
    use
    Code:
    float c=1.8f

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Axel
    Why is the following evaluated to 8.625000?
    In general, if the program's calculations give the "wrong" answer, make sure the program is working on the values that you think it is:

    Code:
    #include <stdio.h>
    
    int main()
    {
       int c = 1.8;
       float q = 2.8;
    
       float total = (2.5 * c + (3.5 * q) / 1.6);
    
       printf("c = %d, q = %f\n", c, q); /* what's going on here? */
    
       printf("%f", total);
       return 0;
    }
    c = 1, q = 2.800000
    8.625000
    D

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  2. Expression Evaluator Contest
    By Stack Overflow in forum Contests Board
    Replies: 20
    Last Post: 03-29-2005, 09:34 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Binary Expression Trees
    By Kanji in forum C++ Programming
    Replies: 0
    Last Post: 03-12-2003, 09:01 PM
  5. Help! Program to evaluate expression...
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 02-19-2002, 05:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21