Like Tree1Likes
  • 1 Post By grumpy

help with a c code

This is a discussion on help with a c code within the C Programming forums, part of the General Programming Boards category; the question is Write a program that estimates then prints the value of the mathematical constant e by using the ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    4

    help with a c code

    the question is
    Write a program that estimates then prints the value of the mathematical constant e by using the formula:

    e=1+ 1/1!+ 2/2! + 3/3!+ ...
    (calculate till 5/5!)
    my answer
    Code:
    #include <stdio.h>
    
    int main()
    
    {
        
    int factorial=1;
    
    float formula;
    int counter=0;
    float e=0.0;
    while(counter<=5)
    {
    if(counter==0){factorial=1;
    counter=counter+1;}
    factorial*=counter;
    formula=(float)counter/factorial;
    e=e+formula;
    counter++;  
    }
    printf("%f",e);
    return 0;
    }
    can any one tell what is wrong here?

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by hula2007 View Post
    can any one tell what is wrong here?
    You don't indent your code.
    You need to tell us what's wrong. Do you get an error, a wrong result, ...?

    Bye, Andreas

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,406
    you are incrementing counter twice in the first iteration of the loop but as you are checking <= you get away with that, you dont need to assigin 1 to factorial in the IF statement as it was assigned 1 to start with and the WHILE control is not repeated (which would mean reassignment was neccesary) Your program is a bit clunky and could be cleaner, certainly better formatted, but apart from that it should work ok

    Sorry but i dont really understand maths notation, wish i did..dont know what the ! means so i cant figure out your description of the formula relative to the code and tell you if there is a problem, but it could certainly be expressed with one line using appropriate parentheses to get the order right.
    Last edited by rogster001; 03-17-2013 at 03:16 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,157
    It probably doesn't help that your mathematical description is wrong. I haven't even checked to see if the code matches the formula.

    e = 1 + 1/1! + 1/2! + 1/3! + ..... not 1 + 1/1! + 2/2! + 3/3! + .....
    stahta01 likes this.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 1
    Last Post: 03-10-2010, 10:28 AM
  3. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 06:09 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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