#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.

Recommended Answers

All 2 Replies

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.