C Programming - Pointers - Discussion
Discussion Forum : Pointers - Point Out Correct Statements (Q.No. 8)
8.
In the following program add a statement in the function fact() such that the factorial gets stored in j.
#include<stdio.h>
void fact(int*);
int main()
{
int i=5;
fact(&i);
printf("%d\n", i);
return 0;
}
void fact(int *j)
{
static int s=1;
if(*j!=0)
{
s = s**j;
*j = *j-1;
fact(j);
/* Add a statement here */
}
}
Discussion:
28 comments Page 3 of 3.
Udam said:
1 decade ago
Why j=&s is not workings?
Akshay said:
1 decade ago
@Jigar is correct. Store result in address of i i.e. at *j.
Therefore *j=s;
Therefore *j=s;
Akshay k said:
1 decade ago
@Rahul - s**j means multiply s with *j. You can see this as s*(*j).
Rahul said:
1 decade ago
What does s**j mean?
Jigar Patel said:
1 decade ago
Here fact(j) is a recursive function so when fact(j) is called last time i=0 .
Then function returns
s=120 (factorial value)
So it we have to store in j mean we need to us *j=s.
Then function returns
s=120 (factorial value)
So it we have to store in j mean we need to us *j=s.
Sk rashid ali said:
1 decade ago
Recursive function store the value at the stack.
So when when the condition is satisfied and the values that are store are executed in reverse order. So whatever the value of s at last will have to be stored in *j and that becomes the first value to be displayed.
So when when the condition is satisfied and the values that are store are executed in reverse order. So whatever the value of s at last will have to be stored in *j and that becomes the first value to be displayed.
Gowthami said:
1 decade ago
Can you give a brief explanation for this question sir.
Arjun Prasad said:
1 decade ago
After all this recursive calls 's' will finally have the factorial of 5. So it must be copied to j location so that it can be printed in main function. Hence B is right answer.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers