S2-C-Programs

All major conceptual S2 C-Programs with Algorithms

View the Project on GitHub trulyPranav/S2-C-Programs

Code:

#include <stdio.h>
//define recursive function fact
int fact(int n){
    if(n==0||n==1){
        return 1; // returns 1 since 0! and 1! is 1
    } else{
        return n*fact(n-1); // logic > n = 3, 3 * fact(2)=> 3 * 2 * fact(1) = 6
    }
}

void main(){
    int n;
    printf("enter a number");
    scanf("%d",&n);
    printf("Factorial of %d is : %d",n,fact(n));// calling the function in the main
}

Algorithm

Step 1: Define Function fact(n)

Step 2: Cases :

Step 3: Writing main():

Step 4: Stop