Monday, 12 September 2016

How to do factorials in C

#include<stdio.h>

#include<conio.h>

int main()
{
    int i,fact=1,n;

    clrscr();

    printf("Enter the number: ");

    scanf("%d",&n);  // takes input for calculating the factorial 

    for(i=1;i<=n;i++)
     /* here i=1 and n=5 so 1<=5 its true so the loop is started 
    {

    fact=fact*i;  // fact=1 i=1 so fact*i gives 1 and store this into the fact

    /* after that i is increased by 1 and compute the previous line until i<=n                                                       gives  a false value  */

    printf("%d! = %d\n",i,fact);   
}
    getch();

    return 0;

}

2 comments: