I need help to find if a given number is a factorial or not. Example : if 120 is given, it is a factorial as 120 = 5! Whereas 121 is not. Any help will be appreciated.
divide by natural numbers, incrementing them by 1 each time in a while loop.
if there's a non zero remainder, its not a factorial.
if finally the divisisor becomes one, it is a factorial.
as in
int i=0;
while(...)
n=n/i;
i++
int findFact(int test){
for(int i=1, int sum=1; sum <= test; i++){
if(sum == test)
return i; //Factorial of i
sum *= i; //Increment factorial number
}
return 0; //No factorial found
}