Like Tree1Likes
  • 1 Post By qny

Need help with calculating a sum :/

This is a discussion on Need help with calculating a sum :/ within the C Programming forums, part of the General Programming Boards category; As I'm informed about the rules, I won't ask you to write all of the code.. The problem i am ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    17

    Need help with calculating a sum :/

    As I'm informed about the rules, I won't ask you to write all of the code.. The problem i am trying to overcome is that:
    Write a program that approximates e by computing the value of :
    e=1+1/1!+1/2!+1/3!....+1/n! (user will enter the value "n")

    So, my code is as it's written below;
    But results are not satisfying when i execute the program.. I ask you to find my mistake and inform me about it. Thanx.


    Code:
    #include <stdio.h>
    int main ()
    {
        int k, n, x;
        float total, denom;
        scanf("%d", &n);
        k=1;
        denom=1;
        total=1;
        while(n>=k){
            x=n;
            for(;x>0; x--){
                denom=denom*x;
            }
    
    
            total+=1.0f/denom;
            n--;
        }
        printf("Result:%.4f\n", total);
        return 0;
    }

  2. #2
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    You need to set denom to 1 inside the loop.
    blackolick_ likes this.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    Try using a debugger, or at a push, some debug print statements.
    Code:
        while(n>=k){
            x=n;
            for(;x>0; x--){
                denom=denom*x;
            }
            printf("Fact(%d)=%f\n",n,denom);
    
            total+=1.0f/denom;
            n--;
        }
    Are these factorials?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    17
    OMG ! I spent my 2 days just for this I'm so grateful sir. Thank you very much "qny" !

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,287
    No need for nested loops. If you do insist on nesting the loops, reset denom to 1 at the beginning of the while loop.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    17
    Quote Originally Posted by Salem View Post
    Try using a debugger, or at a push, some debug print statements.
    Code:
        while(n>=k){
            x=n;
            for(;x>0; x--){
                denom=denom*x;
            }
            printf("Fact(%d)=%f\n",n,denom);
    
            total+=1.0f/denom;
            n--;
        }
    Are these factorials?
    Yes Salem. My first loop (while) for the sum of (1/denom)s, and the second one (for) for factorials.. And thanx for your reply.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    17
    Yes Grumpy, maybe it is as you said sir. But consider that i am a beginner in c programming approach as you can understand from my coding. And i've learned until loops, that's why I insist on using nested loops. Thanks for your reply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calculating e
    By roelof in forum C Programming
    Replies: 11
    Last Post: 06-11-2011, 01:49 AM
  2. Calculating GCD
    By roelof in forum C Programming
    Replies: 15
    Last Post: 05-27-2011, 10:40 AM
  3. Calculating Pi with GMP
    By blkhockeypro19 in forum C Programming
    Replies: 0
    Last Post: 06-10-2010, 04:30 PM
  4. calculating cos
    By lilhawk2892 in forum C++ Programming
    Replies: 5
    Last Post: 09-28-2005, 02:39 PM
  5. Help with calculating
    By Moffia in forum Windows Programming
    Replies: 3
    Last Post: 08-05-2005, 01:21 AM

Tags for this Thread


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