C program to calculate factorial of number N using function.

#include<stdio.h>
#include<conio.h>
void main(){
int n;
long int ans;
long int factorial(int x);
printf("Enter an integer number\n");
scanf("%d",&n);
if(n>0) {
ans=factorial(n);
printf("factorial of %d=%ld\n",n,ans);
}
else printf("wrong input\n");
getch();
}
long int factorial(int x){
int i; long int fact=1;
for (i=1;i<=x;i++) fact=fact*i;
return (fact);


}

No comments:

Post a Comment