Write a program to find the factorial of any number.
using function
Recursion:
using function
- #include <stdio.h>
- int fact(int);
- void main()
- {
- int num, factorial;
- printf("Please enter any number to find factorial : ");
- scanf("%d",&num);
- factorial=fact(num);
- printf("The number %d factorial is = %d\n",num,factorial);
- //printf("Factorial of the number (%d) = %d\n",num,fact(num));
- }
- int fact(int n)
- {
- int i,f=1;
- for(i=1;i<=n;i++) {
- f=f*i;
- }
- return f;
- }
Recursion:
- #include <stdio.h>
- int fact(int n)
- {
- if(n==0)
- return 1;
- else
- return (n*fact(n-1));
- }
- void main()
- {
- int num;
- printf("Please enter any number to find factorial : ");
- scanf("%d", &num);
- printf("The number %d factorial is = %d\n",num,fact(num));
- }
একটি মন্তব্য পোস্ট করুন