#include <stdio.h>
#include <math.h>

int main()

    float i, x;
    double fact=0, sum=0;

    printf("Enter value of x as the power of e: ");
    scanf("%f", &x);

    printf("Enter n as the number of terms:  ");
    scanf("%d",&i);    

    for (i = 0; i < 99999; i++)

    {   
        sum = 1 + x;
        fact = fact * i;
        sum = sum + pow(x, i)/(fact);   
    }
    if (sum == sum)

    printf("The value of e^%f is %lf",x,sum);

    return 0;

Dani AI

Generated

The symptom described here is a very common set of implementation bugs when trying to compute e^x by summing a Maclaurin series. The quick causes to check are: wrong variable types or format specifiers for the loop limit, incorrect initialization or update of the running factorial/term, and accidentally reinitializing the sum inside the loop. already spotted the input/loop-variable mismatch, and correctly mentioned that alternative formulations exist (and that some are faster or more stable).

Practical fixes and checks:

  • Use a floating type with enough precision (double) for the accumulator and term.
  • Initialize the running term/factorial so the first term equals 1, then update it each iteration rather than recomputing factorials or calling pow repeatedly.
  • Read the loop limit into an integer variable and use that integer to control the loop; do not ignore the user-supplied n.
  • Stop on convergence: break when the absolute value of the newest term is below a small epsilon instead of iterating to an arbitrary large count.
  • Avoid comparing floating values with ==; validate results against the standard library exp() when available.
  • Add diagnostics while debugging: print the first few terms and the index where terms become zero or underflow.

Cautions: for large |x| the series may require many terms or overflow; library implementations are optimized and numerically safer. When correctness and performance matter, prefer the standard exp or use scaling/identity techniques rather than blindly increasing term count.

Recommended Answers

All 2 Replies

Why are you asking for n then reading it into i? And why are you then just ignoring it in your loop control?

Here is the series solution and an alternative solution. In you can opt for any of both, solution #2 is more accurate, more concise and faster. I hope you can understand and tranlate the VB.Net code:

        Dim sum As Double = 0
        Dim fact As BigInteger = 1
        Dim esx As String = InputBox("x = ?")
        Dim x As Double = Double.Parse(esx)
        Dim x0 As Double = x
        Dim es As String = InputBox("n = ?")
        Dim n As Int32 = Int32.Parse(es)
        es = InputBox("solution 1/2")
        If es = "1" Then
            ' Solution #1 '
            sum = 1 + x
            For i As Int32 = 1 To n
                x *= x
                fact *= i + 1
                sum += x / CType(fact, Double)
            Next
        Else
            ' Solution #2 '
            ' exp(x) = lim(1+x/n)^n when n->Infinity  '
            sum = Math.Pow(1 + x / n, n)
        End If
        TextBox1.Text = sum.ToString + " " + Math.Exp(x0).ToString
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.