WAP to calculate the factorial of the given number

Write a program to calculate the factorial of the given number

#include<stdio.h>

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

       printf("Enter any number: ");
       scanf("%d",&n);

       if(n>0) {
              for(i=n;i>0;i--) {
                     fact*=i;
              }
              printf("Factorial of %d is %d.",n,fact);
       }else if(n<0) {
              printf("Please enter only positive number.");
       } else {
              printf("Factorial of 0 is 1.");
       }
}

Output

Enter any number: 4
Factorial of 4 is 24.

Behind the scene

4*3*2*1 = 24

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments