Thread: negative exponents (simple math prog)

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    63

    negative exponents (simple math prog)

    Hey all, i'm using an outdated book, but first of all there is no mention of using pow().

    Code:
    /* Write a program that evaluates the following expression and displays the results (remember to use exponential format to display the result):
     *
     * (3.31 x 10^-8 x 2.01 x 10^-7) / (7.16 x 10^6 + 2.01 x 10^-8)
    */
    
    #include <stdio.h>
    
    int main(void)
    {
        double num;
    
        num = (10 / 8 / 8 / 8 / 8 / 8 / 8 / 8 / 8);
    
        printf("%f ", num);
        
        return 0;
    }
    Currently getting program to output (this is just a start on the calculation and not getting correct answer);

    Code:
    0.000000
    Also, the previous question in book, here is my solution:

    Code:
    /* 6. Write a program to evaluate the polynomial shown here:
     *
     * 3x^3 - 5x^2 + 6
     * for x = 2.55
    */
    
    #include <stdio.h>
    
    int main(void)
    {
        float x = 2.55, num;
    
        num = (3 * (x*x*x)) - (5 * (x*x)) + 6;
    
        printf("%f \n", num);
    
        return 0;
    }
    program output:

    Code:
    23.231621
    Thanks...i understand I am learning how to use the parenthesis correctly. These questions are out of "Programming in C" third Edition by Stephen Kochan (2004).
    Last edited by _jamie; 01-28-2019 at 04:49 PM. Reason: fixed source code

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,688
    In C , "10 / 8 / 8" is zero. While "10.0 / 8 / 8" is not zero.

    Look up integer division for why.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    703
    Presumably the answer they are looking for is more like this. I have no idea what you are thinking trying to divide 10 by 8 eight times!
    Code:
    #include <stdio.h>
     
    int main()
    {
        printf("%.2e\n", (3.31e-8 * 2.01e-7) / (7.16e6 + 2.01e-8));
        return 0;
    }
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  4. #4
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    63
    Thanks john.c

    My book states, "the %e characters are used the values of float or double in scientific notation".
    Quote Originally Posted by john.c View Post
    Presumably the answer they are looking for is more like this. I have no idea what you are thinking trying to divide 10 by 8 eight times!
    Code:
    #include <stdio.h>
     
    int main()
    {
        printf("%.2e\n", (3.31e-8 * 2.01e-7) / (7.16e6 + 2.01e-8));
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-04-2016, 08:11 AM
  2. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 06:25 PM
  3. New to C - need help with a simple prog.
    By Milano in forum C Programming
    Replies: 5
    Last Post: 07-06-2006, 12:23 AM
  4. Hi simple prog for all of you
    By gauravkg in forum C Programming
    Replies: 3
    Last Post: 07-27-2005, 04:27 AM
  5. VC++ simple prog HELP
    By estranged in forum Windows Programming
    Replies: 5
    Last Post: 01-12-2003, 06:03 AM

Tags for this Thread