C Programming - Functions - Discussion

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;
}

[A]. The function calculates the value of 1 raised to power num.
[B]. The function calculates the square root of an integer
[C]. The function calculates the factorial value of an integer
[D]. None of above

Answer: Option C

Explanation:

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


Dheeraj said: (Jul 13, 2011)  
Why return is used here?

Sreedhar said: (Jul 14, 2011)  
In for loop, what is the value of num

Usha said: (Oct 15, 2011)  
How it calculates fraction value?

Rohit said: (Apr 27, 2012)  
@usha

If num =3 \* 3! = 6 *|
i=1 , f=1
Executes the for loop now i=1, 1<=3,post increment so i= 2 for next time

f= f*i =1*1 =1 --------returns f as 1
Next time after for loop
1*2 = 3-------------returns f as 2
After for loop
2*3 = 6 ---------------returns f as 6
Now for loops get failed in condition
So f=====6 \* ans *\

Parth said: (May 16, 2012)  
How it calculates factorial values?

Srikanth said: (Dec 26, 2012)  
What is the value of num variable?

Hari said: (Feb 14, 2014)  
How it calculate the factorial values here please explain step by step?

Nikky said: (Apr 17, 2014)  
What is the value of num?

Aman said: (Jan 27, 2015)  
But there is no value for number?

Bharu said: (Mar 4, 2015)  
Can anyone please explain this?

Tripti said: (Mar 25, 2015)  
I don't understand why to use num?

Hari said: (Sep 5, 2015)  
I can't understand.

Praveena said: (Jun 27, 2016)  
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.

Helloworld said: (Oct 24, 2016)  
It does not calculate the factorial of all the numbers. 0! is not calculated so None of the above must be the correct answer.

Rohan said: (Jun 9, 2018)  
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 .... */
}

Niks Wabale said: (Dec 13, 2018)  
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.

Sakshi Munya said: (May 5, 2019)  
There is no 'main' function. Then, how it's execute?

Sanprince said: (May 5, 2019)  
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.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.