#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
system("cls");
int i, num, fact = 1;
cout<<"\nEnter integer:";
cin>>num;
i=num;
while(num)
{
fact *= num;
--num;
}
cout<<"the factorial of"<<i
<<"is"<<fact<<"\n";
getch();
}
The above program inputs an integer num. Then as long as num
is nonzero (according to while (num)) the loop body iterates i.e., fact is
multiplied with num and the result is stored back in fact, followed by the
decrement of num. Again the test-expression (num) is evaluated : if it is true,
the loop is repeated otherwise terminated.
The for and while
loops are entry-controlled loops i.e., there is control over entry in them.
Then test-expression is evaluated before entering into the loop, if it is
evaluates to true then loop is executed otherwise is loop is terminated.
by luv yadav
No comments:
Post a Comment