Write
a program to print the factorial of a number.
As we know that factorial of a number means the sum of
number less then given number but always greater and equal to 1.
Mathematically let’s take a number b then.
Let’s go through program
#include<stdio.h>
void main ()
{
Int a, fact. =1;
Clrscr();
Printf (“\n enter a number you want to calculate=”);
Scanf (“%d”, & a);
while (a! =0)
{
fact. = fact. * a;
fact. = fact. * a;
a--;
}
Printf (“\n fact. Of number = %d”, fact.);
getch();
}
DRY RUN: -
Enter a number you want to calculate = 3 (say)
a! =3 so,
fact. = 1* 3;
a= 3-1 =2;
a! =2 so,
fact. = 3*2;
a= 2-1=1;
a! =1 so,
fact. = 6*1;
a= 1-1=0;
a! =0 which is not possible so operation will stop here
and output
will be 6.
Comments
Post a Comment