#include <stdio.h> //recursion // int factorial(int n){ // if(n==0){ // return 1; // } // else{ // return n*factorial(n-1); // } // // printf("invalid"); // return 0; // } //iterative approach int factorial(int n){ int val=1; for(int i=n; i>1; i--){ val=val*i; } return val; } int main(){ int num; printf("enter the number "); scanf("%d",&num); printf("factorial of number is %d",factorial(num)); return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter