# Factorial in C++ - iteratively

 #include using namespace std; int main(int argc, char *argv[]) { unsigned int n; cout<<"n = "; cin>>n; unsigned int i=1, s=1; while(i<=n){ s=s*i; i++; } cout<>c; }

In the first line of the program, we attach the iostream header file (input-output streams). This is necessary because in the further part of the program you will want to be able to write on the screen (cout) and load input data from the keyboard (cin). In the second line, we inform the compiler that we will use names defined in the std namespace (they will be: cin, cout, endl), if we did not, it would be necessary to use longer qualified names (std::cin, std::cout, std::endl). In C ++, the program starts with the main function. In our example, this is the only function of the our program. Line 3 contains the beginning of the main function definition, this is the beginning of the implementation of our algorithm (block START (1)). In the next line, we declare an integer without sign (natural number) n, which we will use in a moment. In C ++, variables can be declared anywhere before they are used, so we do not declare variables i and s immediately.

The lines 5 to 6 perform the input/output operation (2), in which the input data is taken - the argument of the function factorial.

In line 7, we declare variables i and s, giving them also initial values ​​(in both cases equal to 1) (3).

The while statement is equivalent to the selection block (4) from our algorithm. Instructions contained in lines 9 - 10 (block 5) will be executed as long as the condition in the while statement is satisfied, i.e. until and is less than or equal to n. If constantly increasing i becomes greater than n, we pass to the instruction after the while loop, starting in line 12, in which the result is printed (6).

Instructions in lines 13 - 14 have no relation to the implementation of the algorithm. They are used to stop the program until you enter any character and press the enter key to prevent the program window from closing immediately.

The curly brace } from line 15 completes the main function and also the operation of the entire program (end block of the algorithm (7)).