0
#include<stdio.h>
int factorial (int);
void main()
{
    int a,fact;
    printf("\nEnter a value:");
    scanf("%d", &a);
    fact= factorial(a);
    printf("Factorial value=%d", fact);
}
int factorial (int x)
{
    int f=1,i;
    for(i=x;i>=1;i--)
    f=f*i;
    return(f);
}

Enter any number: 3
Factorial value = 6

But i did not undartand this program. Mostly in the loop condition checking in the function part. Can any one describe me the program step by step. Advanced Thanks.

3
Contributors
2
Replies
17
Views
2 Years
Discussion Span
Last Post by Shark_1
0

Play computer!
With a pencil and paper!
Go through the factorial function.
What value is x? Write down x = 3
What value is f? Write down f = 1
What value is i? Write down i = x = 3
In the for loop the condition i >=1 is true so you enter the loop.
What value will f be now? Cross out the previous value and change it with fXi = 1X3=3.
Hope you got the picture, continue until finished.

0

In mathematics
Factorial of x=x * (x-1) * (x-2) * (x-3)................(x-(x-2)) * (x-(x-1))
If x=3 the
Factorial of 3=3 * (3-1) * (3-2)=3 * 2 * 1=6
You can represent it by any one Symbols of L or !.
So, 3!=3 * 2 * 1=6

Edited by Shark_1

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.