C Programming - Functions - Discussion

Discussion Forum : Functions - Point Out Correct Statements (Q.No. 3)
3.
Which of the following statements are correct about the function?
long fun(int num)
{
    int i;
    long f=1;
    for(i=1; i<=num; i++)
        f = f * i;
    return f;
}
The function calculates the value of 1 raised to power num.
The function calculates the square root of an integer
The function calculates the factorial value of an integer
None of above
Answer: Option
Explanation:

Yes, this function calculates and return the factorial value of an given integer num.

Discussion:
19 comments Page 1 of 2.

Sakshi munya said:   6 years ago
There is no 'main' function. Then, how it's execute?
(1)

Rohan said:   7 years ago
According to me, it is;

long fun(int num)
{
int i;
/* declare variable i as int.... */

long f=1;
/* declare and initialize variable f as long with value 1 */

for(i=1; i<=num; i++)
f = f * i;

/* for loop is used
Let's take an example...
-> 10 is given as arguments in the function fun...
[First Time]
- > i is initialized with 1 , 1<=10 ,condition true
- > f = 1*1
[Second Time]
- > i is incremented and becomes i = 2 , 2<=10 , condition true
- > f = 1*2 = 2

and further till i = 10, 10<=10 , condition true
- > f = 1* 2*3*4*5*6*7*8*9*10 */

return f;
/* because it returns an float variable .... */
}
(1)

Bharu said:   1 decade ago
Can anyone please explain this?

Chandrakala said:   3 years ago
Very nice.

Sanprince said:   6 years ago
You can take any value for num. In for loop, the statement written, it will behave like factorial so answer is C. Take any value you will understand.

Niks Wabale said:   7 years ago
It gives the correct result, even if we put 0 value then also for loop won't work and we return the value of f and previously it is initialized to 1 and 0!=1.

So it works perfectly fine for 0 value also.

Helloworld said:   9 years ago
It does not calculate the factorial of all the numbers. 0! is not calculated so None of the above must be the correct answer.

Praveena said:   9 years ago
Suppose num=4;

In the program given that
>> f=1
>>for(i=1;i<=4;i++)
f=f*i;
let us evaluate the for loop
step 1: f=1*1=1
step 2: f=1*2=2
step 3: f=2*3=6
step 4: f=6*4=24

IN EACH STEP VALUE OF " f " is saved

So factorial of 4 is 24.

Hari said:   10 years ago
I can't understand.

Tripti said:   1 decade ago
I don't understand why to use num?


Post your comments here:

Your comments will be displayed after verification.