C Program to find Factorial of a Number
Factorial program in C : This program calculates or find the factorial of a number using function, for loop and recursion.
C Program to Find Factorial of Number
Here is the source code of C program find the factorial of Number./* Aim: Write a C program to find or calculate factorial of a number. */
#include<stdio.h>
int main()
{
int i,fact=1,num;
printf("\n Enter the any number:- ");
scanf("%d",&num);
for(i=num;i>=1;i--)
{
fact*=i;
}
printf("\n The factorial of %d is %d \n \n",num,fact);
return 0;
}
Output:
Enter the any number:- 5 The factorial of 5 is 120
Flowchart to Find Factorial of a Number
Algorithm (wap) to find factorial of a number in C
- Start
- Accept any number to find its factorial.
- Declare and initialize an integer variable to save the factorial of given number.
- Use for loop to loop through 1 to given number. Here we can loop forward as well as backward.
- Finally print the factorial of given number.
- Stop.
C Program to Find Factorial of Number using Function
/* Aim: Write a function for the following function prototype
void Factorial(int a) */
#include<stdio.h>
void Factorial(int a); // Factorial Function Prototype
int main()
{
int a;
printf("\n Enter any value:- ");
scanf("%d",&a);
Factorial(a);
return 0;
}
// Factorial Function
void Factorial(int a)
{
int i,fact=1;
for(i=a;i>=1;i--)
{
fact*=i;
}
printf("\n Factorial of %d is %d \n \n",a,fact);
}/
Output:
Enter any value:- 6 Factorial of 6 is 720
Factorial Program Using Recursion in C
/* Aim: Write a recursive program to factorial of given number */
#include<stdio.h>
long int Factorial(int num); // Factorial Function Prototype
int main()
{
int num;
printf("\n Enter any number:- ");
scanf("%d",&num);
printf("\n Factorial of %d is %d \n \n",num,Factorial(num));
return 0;
}
// Factorial Function
long int Factorial(int num)
{
if(num==0 || num==1)
return 1;
else
return (num*Factorial(num-1));
}
Enter the any number:- 9 The factorial of 9 is 362880