C program to calculate factorial using "while loop".

C program to calculate factorial using "while loop".

/*C program to demonstrate the working of while loop*/
#include <stdio.h>
#include <conio.h>
 void main()
 {
 int n,factorial;
     printf("Enter a number.\n");
 scanf("%d",&n);
 factorial=1;
 while (n>0)   /* while loop continues util test condition number>0 is true */
 {
 factorial=factorial*n;
 --n;    /*or n--*/
 }
printf("Factorial=%d",factorial);
getch();
}

Expected Output:
Enter a number.
5
Factorial = 120

Post a Comment

Previous Post Next Post