HELP NEED EXPLANATION

Feb 24, 2012 at 2:42am
What does fi(b) return when b is positive?
long fi(int a){
int c=1;

while(a>1){
c∗=a;
a−−;
}
return c ;
}

can someone explain why the answer is b! .. and what does b! mean in this problem?
Feb 24, 2012 at 11:41am
b! means factorial of b which is the product of all positive integers less than or equal to b.
Feb 24, 2012 at 11:54am
Also if b == 0 the function will return 1 because by mathematical definition 0! == 1.

In my opinion the function is writen incorrectly. It would be more consistent if the parameter would be defined as unsigned int and the return type as unsigned long long

1
2
3
4
5
6
7
8
inline unsigned long long factorial( unsigned int n )
{ 
   unsigned long long product = 1;

   while( n > 1 ) product ∗= n--;

   return ( product ) ; 
}
Last edited on Feb 24, 2012 at 11:58am
Feb 24, 2012 at 5:44pm
OK, so why isnt it asking for a, b is not even in the function can you explain this?
Feb 24, 2012 at 7:33pm
I don't think your function is calci.ulating factorial at all. It's not calculating anything in fact. It just returns 2.

Imagine you use it with a=3;
You are not calculating any product only assign in eacch loop the value a had in the previous loop. Everytime a is bigger than 1 you have a series of loop like this:
a=3>1->c=3; and a=2
a=2>1->c=2 and a=1
a=1 so it stops. So c =2;

If the condition is not met not even once (a = 1 or less) c is 1.

As for the last thing you ask in your function definition argument int a is replaced by the value (number or variable) you are using as argument when you call the function
Feb 24, 2012 at 7:52pm
so when it says fi(b) I am just to assume a positive number for b lets say 5 and use that value for a?
Feb 25, 2012 at 12:58am
Yes.

@eyepros. In the loop it is not c = a but c *= a (i.e. c = c *a).
Topic archived. No new replies allowed.