Write a program that prompts the user for a value for i and displays the e value, and this is all i have so far. I was told it's suppose to have two for loops but i'm not sure where and how to do it and i know my output is wrong as well, also the use of a for loop is important.

#include <iostream>
using namespace std;
int main (){
    double count;
    double i=1;
    double e;

    cout<<"What is the value for i?";
    cin>>i;

    for(count=1;count<=i;count++){
        e!=1/count;
        cout<<"The value of e is equal to :"<<e<<endl;
    }

    system ("pause");
    return 0;

}

Edited 2 Months Ago by laurel.jackson.12

You're close.

Using ! as factorial as is used in mathematics and assuming i is the number of iterations here, if i is 5,

e is approximated to 1 + 1/1! + 1/2! + 1/3! + 1/4! + 1/5! (so the ith term is 1/i!). e is the sum of the terms, so initialize e to 1 outside of the loop and initialize term to 1 outside the loop and adjust those two values INSIDE the loop just like the math formula. You should get closer and closer to e the higher the value of i is.

Keep in mind that ! in C++ does NOT mean factorial. It means "not", so don't use ! in your code. Fortunately 1/5! is (1/4!) divided by 5, so it's pretty easy to calculate using your count variable and the LAST term.

e - math constant

double e = 1;
double term = 1;

cout<<"What is the value for i?";
cin>>i;

for(count=1;count<=i;count++){
    term = term / count;
    e = e + term;
    cout<<"The value of e is equal to :"<<e<<endl;
}

Edited 2 Months Ago by AssertNull