I know this has been asked before, but none of the threads I found helped me much. I am a beginner at C++ programming and need help figuring out why this doesn't work.

Question: e^x can be approximated by sum
1 + x + x^2/2! + x^3/3 + ... + x^n/n!

Write a program that takes a value x as input and outputs this sum for n taken to be each of the values 1 to 100. The program should also output e^x calculated using the predefined function exp.

Use variables of type double to store the factorials or you are likely to produce integer overflow. You might want to output 10 lines with 10 values on each line.

My teacher prefers us to use Borland, although I also have Dev on my computer.

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

main ()
{

      int x;
      double sum, fact;
      char ans;

      do
      {
            cout << "Please enter the value x: ";
            cin >> x;
            cout << endl;
            sum = fact = 1;
	
               for (int n=1; n<=100; n++)
	           {

            		for (int i=0; i<n; i++)
                 	{
               		   fact = n*fact;
               	         }

            		sum += (pow(x,n)/fact);
                        
         cout <<"n = "<<n<<" and sum = "<< sum << " and
         exp("<<x<<") = "<<exp(x)<<endl;

            	    }


         cout << endl;
      	 cout << "Do you want to calculate this again? (y/n): ";
         cin >> ans;

		} while (ans == 'y' || ans == 'Y');

getch ();
}

Do you know functions? İf not, learn them immediately and divide you program to functions. In this code you should write two function at least, one of them is power and one of them is factorial function. You use pow for power function so you just need factorial ones.

double fact(double x)
{
    //write your code to here
}

int main()
{
    int x;
    char ans;
    
    do {
        cout << "Please enter the value x: ";
        cin >> x;
        double sum = .0;
        for(int i = 0; i < x; ++i) 
            sum += pow(x, i) / fact(i);    
  
        cout << sum << endl;
    } while(ans == 'y' || ans == 'Y'); 
    return 0;
}

And don't forget C++ does't have implicit int type so you have to write the return type of main!

I need help figuring out why it doesn't work haha.

We just learned functions last week actually. Thanks! I'll try it. :)

Do you know functions? İf not, learn them immediately and divide you program to functions. In this code you should write two function at least, one of them is power and one of them is factorial function. You use pow for power function so you just need factorial ones.

double fact(double x)
{
    //write your code to here
}

int main()
{
    int x;
    char ans;
    
    do {
        cout << "Please enter the value x: ";
        cin >> x;
        double sum = .0;
        for(int i = 0; i < x; ++i) 
            sum += pow(x, i) / fact(i);    
  
        cout << sum << endl;
    } while(ans == 'y' || ans == 'Y'); 
    return 0;
}

And don't forget C++ does't have implicit int type so you have to write the return type of main!

I need help figuring out why it doesn't work haha.

then tell us what it does wrong, haha. We aren't psychic, chortle.

This article has been dead for over six months. Start a new discussion instead.