Skip to main content

Featured

C Pattern Programs: Square Number and Alphabet Patterns Explained

πŸ”· Square Star Pattern πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { printf("* ");//keep"* " } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Column-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = num; i >= 1; i--) { for(int j = num; j >= 1; j--) { printf("%c ", j + 64);//%c for Character and 64 will be ASIIC VALUE } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Row-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; ...

Factorial Calculation Using Recursion in C

Factorial Using Recursion in C

Factorial Calculation Using Recursion in C

This C program calculates the factorial of a given positive integer using a recursive function. The factorial of a number n (denoted as n!) is the product of all positive integers less than or equal to n.

✅ C Program Code:


#include <stdio.h>

int fact(int n)
{       
    if (n == 0 || n == 1)
    {
        return 1;
    }
    else
    {
        return n * fact(n - 1);
    }
}

int main()
{
    int num;
    printf("Enter the number that you want to find factorial:\n");
    scanf("%d", &num);
    if (num < 0)
    {
        printf("Factorial number contain only positive:\n");
    }
    else
    {
        printf("Factorial of a given number %d is %d\n", num, fact(num));
    }
}
  
  
  

πŸ“Œ How It Works:

  • fact(): This is the recursive function that calculates factorial. It returns 1 if n is 0 or 1 (base case). Otherwise, it multiplies n by the factorial of n-1.
  • main(): Takes user input for the number and checks if it is negative. If negative, it prints an error message. Otherwise, it calls fact() to calculate factorial and prints the result.

πŸ’» Sample Output:

enter the number that you want to find factorial:
5
Factorial of a given number 5 is 120

πŸ” Keywords:

factorial in C, recursive factorial program, factorial using recursion, C programming recursion example, factorial function in C, beginner C programs, recursion in C, factorial calculation

Comments

Popular Posts

πŸŒ™