Iterative (Non-recursive) version of factorial function in C

#include <stdio.h>
 
 int fact(int);
 int main()
 {
    int y,n;
 
    printf("Enter the value of n: ");
    scanf("%d",&n);
    y=fact(n);
    printf("nFactorial of %d is %dn",n,y);
    return 0;
 }
 
 int fact(int n)
 {
    int i,f=1;
 
    for(i=1;i<=n;i++)
        f*=i;
    return f;
 }

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.