I want to make a programme that finds the factorial of a number that i input: eg 6!=720.
The following is my source code, it can compile but the logic is probably wrong as it does not do what its suppose to do
#include<iostream>
using namespace std;
int factorial(int m){
int y,z;
for(z=1;z<=y;z++){
y=y*z;
return (y);
}
}
int main(){
int g,k;
cout<<"Enter a number";
cin>>g;
k=factorial(g);
cout<<"The answer is" << k;
system("PAUSE");
return 0;
}
sorry for my noobness i have no idea why i did it. My main aim is to create a factorial function and main function.I will then use the main function to call the factorial function and get the answer. Can someone show me the correct method to do it, thanks. I know i am half way there but i stilll cant solve it
factorial is calculated like this :-
for e.g 5.
5*4*3*2*1 = 120
initalize y with the number of which you want the factorial.
inside for loop,
initalize z equals to 1 less than y. decrement z and set the condition greater than or equal (>=) to 1.
the rest is correct.