- #1
erok81
- 464
- 0
Homework Statement
Write a C++ code factorial.cc that calculates the factorial of an integer by direct multiplication. Start with your code in exercise (3), and modify it so that it gets "n" from the user and prints out n! = 1*2*3*...*(n-1)*n. To do this, use a "for" loop:
Code:
int nfac;
nfac = 1;
for (int i = 0; i < n; i++)
{
...
}
and within the curly braces set the variable nfac appropriately
Homework Equations
None.
The Attempt at a Solution
I am 100% lost on this problem. We don't have a text and we didn't learn about nfac or how to do these types of calculations in class so I don't really have anything to go off of.
It says to start with my code from exercise 3 which is this. It works fine and I can mostly understand which lines do what.
Code:
#include <iostream>
using namespace std;
int main(void)
{
int n;
cout << "enter a number:";
cin >> n;
cout << "your number was " << n << "\n";
return 0;
}
I don't have much past this. I've tried different combinations of things for last hour and a half but haven't gotten anything. Mainly because I zero idea where to put things. My latest guess was something like this.
Code:
#include <iostream>
using namespace std;
int nfac;
nfac = 1;
for (int i = 0; i < n; i++)
{
cout << "enter a number:";
cin >> n;
cout << "your number was " << n << "\n";
return 0;
}
I know this is completely incorrect and am almost embarrassed to even post it, but I really have nothing at all. I think the cout line needs something that actually points it to what the factorial is?
As you can see it looks like I just slapped his hint in there and called it good, but I've tried everything and this is the closest/cleanest attempt I've gotten so far - even though it is wrong.