Factorial in C
Q. Write a C program for Factorial Number.
#include <iostream>
void main(){
int n, i;
unsigned long factorial = 1;
printf("Enter the factorial number : ");
scanf("%d",&n);
factorial= fact(n);
printf("Factorial of %d = %d", n, factorial);
}
int fact(int a){
if (a==0 || a==1){
return 1;
}
else{
return a*fact(a-1);
}
}
Q. Write an algorithm to Factorial Number in C.
Step 1: Start the program.
Step 2: Declare variables:
- `n` (integer) to store the input number.
- `factorial` (unsigned long) to store the factorial result.
Step 3: Prompt the user to enter an integer number `n`.
Step 4: Read the input number from the user.
Step 5: Define a recursive function `fact(int a)` that returns factorial of `a`:
- If `a` is 0 or 1, return 1. (Base case)
- Otherwise, return `a * fact(a - 1)` (Recursive call).
Step 6: Call the function `fact(n)` and assign the returned value to `factorial`.
Step 7: Print the result: "Factorial of n = factorial".
Step 8: End the program.
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com