C Online Compiler
Example: Factorial Calculation in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Factorial Calculation #include <stdio.h> long long factorial(int n) { // Step 1: Define the base case // The factorial of 0 or 1 is 1. if (n == 0 || n == 1) { return 1; } // Step 2: Define the recursive step // n! = n * (n-1)! return n * factorial(n - 1); } int main() { // Step 1: Declare a variable for the number int num = 5; // Step 2: Call the recursive factorial function long long result = factorial(num); // Step 3: Print the result printf("Factorial of %d is %lld\n", num, result); return 0; }
Output
Clear
ADVERTISEMENTS