Write a program to calculate the factorial for given number using function.
Factorial for given number without using function
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
int fact=1;
printf("enter the number\n");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
fact=fact*i;
}
printf("%d",fact);
}
Factorial for given number using function
#include<stdio.h>
#include<conio.h>
int fact(int num)
{
int f=1,i;
for(i=1;i<=num;i++)
{
f=f*i;
}
return f;
}
void main()
{
int fa,n;
printf("\n enter the number \n");
scanf("%d",&n);
fa=fact(n);
printf("\n Factorial of number%d is %d",n,fa);
getch();
}
No comments:
Post a Comment