PlEASE HELP!

Hello! I cannot Understand why do I need "i" here and what does "for" do in this situation? can you explain this code for me ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include<iostream>    //Calling the Header File.

int main()    //Declaring the main function
{
    using namespace std;    //Tells the compiler that we'll be using all the standard C++ library functions
    
    int a,factorial=1;
    cout<<"Enter the number: ";  //Ask for the number.
    cin>>a;
    
    for(int i=1;i<=a;i++)
    {
        factorial=factorial*i;
    }
    
    cout<<"The factorial of the given number is: "<<factorial<<endl;
    
    
    return 0;
}
hi, i am doing a similar project, you could change it to this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// to calculate the factorial of a number (n!)
#include<iostream>

using namespace std;

int main()

{

int num,factorial=1;

// user factorial input
cout<<"Enter a number to find its factorial:  ";
cin>>num;
// a = 1, number less then 1 equals number, increase the number by 1 
for(int a=1;a<=num;a++)

{
// factorial = 1 * 
factorial=factorial*a;

}
//Factorial number
cout<<"\n\nFactorial number is ="<<factorial<<endl;

// Pause for user
system("pause");

return 0;

}
Thank you!! <3
Topic archived. No new replies allowed.