Thread: Error while calculating sum of n terms of a exponential series to n terms:

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    69

    Error while calculating sum of n terms of a exponential series to n terms:

    Following is my program for calculating the sum of exponential series upto n terms:

    e = 1/0! + 1/1! + 1/2! + 1/3! + 1/4! + 1/5!+....1/n!

    Code:
    #include<stdio.h>
    int main()
    {
      int n,i,p=1;
      float sum=1.0,fact=1.0;
      printf("enter the number of terms");
      scanf("the number of terms are %d",&n);
      for(i=1;i<=n;i++)
      {
                       while(p<=i)
                       {
                       fact*=p;
                       p++;
                       }
      sum+=(1.0/fact);
      }
      printf("the sum is %f",sum);
      system("pause");
      return 0;
    }
    While executing the code in dev-C++, the program stops executing and goes into an infinite loop after entering 'n'. Kindly help.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You never reset the value of p, did you mean to?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    It is not needed, is it? Everythime the next factorial is computed the variable fact alreaddy has the value of factorial of the earlier number. I am still getting the same problem, my program, even though it is running fine, doesn't proceed after entering the value of n.


    Quote Originally Posted by quzah View Post
    You never reset the value of p, did you mean to?


    Quzah.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > printf("enter the number of terms");
    > scanf("the number of terms are %d",&n);
    Perhaps you should just have
    scanf("%d",&n);

    Otherwise, you have to type in
    the number of terms are 7
    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.

  5. #5
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Otherwise, you have to type in
    the number of terms are 7
    Salem, would that even be legal thing to do in C?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Salem, would that even be legal thing to do in C?
    Try it

    I mean of course, RTFM
    A directive is one of the following:

    · A sequence of white-space characters (space, tab, newline, etc.; see isspace(3)). This directive matches any amount of white space, including
    none, in the input.

    · An ordinary character (i.e., one other than white space or '%'). This character must exactly match the next character of input.

    · A conversion specification, which commences with a '%' (percent) character. A sequence of characters from the input is converted according to
    this specification, and the result is placed in the corresponding pointer argument. If the next item of input does not match the conversion
    specification, the conversion fails — this is a matching failure.
    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.

  7. #7
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Thank youu so much guys! Couldn't spot the obvious

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Calculate the factorial iteratively; you only need one loop.
    If the last factorial calculated was 4! then just multiply that previous value by 5 and behold you now have 5!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Jan 2012
    Posts
    16
    Code:
    float sum=1.0,fact=1.0;
    Your sum should be equal to 0 not 1, cause you are not multiplying sum, you are adding.

  10. #10
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Thanks everyone. @Stevan - actually the first term of my series is 1 which is not calculated while coding, so I am writing sum as 1 initially.

    Quote Originally Posted by Stevan View Post
    Code:
    float sum=1.0,fact=1.0;
    Your sum should be equal to 0 not 1, cause you are not multiplying sum, you are adding.

  11. #11
    Registered User
    Join Date
    Jan 2012
    Posts
    16
    @abhishekcoder, which term, 1/0! ?

  12. #12
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Yes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. std namespace terms
    By Aisthesis in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2009, 05:46 PM
  2. Terms used in C
    By lido in forum C Programming
    Replies: 12
    Last Post: 07-25-2003, 02:24 PM
  3. how many terms
    By rams in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2002, 03:10 AM
  4. C++ terms Questions
    By DramaKing in forum C++ Programming
    Replies: 8
    Last Post: 11-12-2001, 11:13 PM
  5. Terms
    By Drakon in forum Game Programming
    Replies: 7
    Last Post: 09-27-2001, 06:24 PM

Tags for this Thread