help me below algorithm.
it gives wrong output

e2 was wrong. 7.38.....

#include <cstdlib>
#include <iostream>
#include <math.h>

using namespace std;

int main(int argc, char *argv[])
{
    float x, err, term_1,result=1.0;
    int i=1,burcin;
    float factorial;
    cout << "x=";
    cin >> x;
    cout <<"error tolerance=";
    cin >> err;
    term_1=err+1;
    while (term_1 > err)
    {
          factorial=1.0;
          for (burcin=2;burcin <= i;++burcin)
          factorial=factorial*burcin;
          term_1=pow(x,i)/factorial;
          result=result+term_1;
          i++;
          }
          cout << "result is "<<result<<endl;
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Recommended Answers

All 3 Replies

What is the equation you used in this computation? You initial 'i' but never assign a new value from user input; as a result, your factorial value is always 1.0. Again... I don't know what the equation is supposed to be...

e2 was wrong. 7.38.....

Not really; it would be something like that. 7.389056...

In 1 + x/1! + x^2/2! + X^3/3! + ..... + x^n/n! + x^(n+1)/(n+1)! + ..... ,
just as n! == (n-1)! * n , x^n == x^(n-1) * n .
We can eliminate the computation of pow() for each term.

double x ; std::cout << "x? " && std::cin >> x ;

    double err ; std::cout << "error tolerence? " && std::cin >> err ;

    double ex = 0.0 ;
    double term = 1.0 ;

    for( int n = 1 ; term > err ; ++n )
    {
        ex += term ;
        term *= x / n ;
    }

Dear friend,

your program is not correct. I could not catch the correct answer cout ex...
I found my mistake in my program... burcin++ is correct. ++ burcin was wrong
my assistant found it. thanks anyway.

Not really; it would be something like that. 7.389056...

In 1 + x/1! + x^2/2! + X^3/3! + ..... + x^n/n! + x^(n+1)/(n+1)! + ..... ,
just as n! == (n-1)! * n , x^n == x^(n-1) * n .
We can eliminate the computation of pow() for each term.

double x ; std::cout << "x? " && std::cin >> x ;

    double err ; std::cout << "error tolerence? " && std::cin >> err ;

    double ex = 0.0 ;
    double term = 1.0 ;

    for( int n = 1 ; term > err ; ++n )
    {
        ex += term ;
        term *= x / n ;
    }
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.