Talvira wrote: |
---|
1) Write a program that asks the user to enter an integer, then calculates and prints the factorial of the number. |
This one is pretty easy. I'm sure you know how to read in an int and store it in a variable. The only part that's left is creating a factorial function. Factorial is based around recursion (or, if you really want to, for loops) and returns the evaluated expression, typically as an int.
To help you get started, my Factorial function prototype is
int Factorial(int n);
You should start at n = 1, then move up to n = 2, n =3, n = 4, etc. If by the time you get to 10 you don't see the pattern or don't understand how to use recursion (my function only consists of 4 lines I believe), you should probably read up on recursion again.
Talvira wrote: |
---|
2) Write a program to calculate ex using the formula:
ex = 1 + x/1! + x2/2! + x3/3! + .......
Allow the user to enter the integer number x , then do the calculation in a loop (for or while), taking it out ten iterations.
For example, if the user entered 2 , the program would calculate: e2 = 1 + 2/1! + 22/2! + 23/3! + ....... |
Once you have the factorial function from 1, this one will be much easier. The equation, if I understand it correctly, is pretty easy to implement. You should just make a function called e that returns a double. Possible prototype
double e(int x);
. Your teacher says 10 iterations, so that would more than likely be a for loop, and at least one call to factorial in each loop.
Good luck.