Skip to main content

Featured

Mastering Hollow Square Patterns in C: Stars, Numbers, Alphabets & Binary

πŸ”’ C Program to Print Hollow Continuous Number Square πŸ“„ Source Code: #include <stdio.h> int main() { int num, k = 0; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { if(i == 1 || i == num || j == 1 || j == num) { // k increments sequentially only along the borders printf("%d ", k++); } else { printf(" "); } } printf("\n"); } return 0; } πŸ“‹ Copy Code πŸ’» Expected Output (Input: 5): Enter the number: 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 πŸ”’ C Program to Print Standard Hollow Binary Row Square πŸ“„ Source Code (Fixed Specifier): #include <stdio.h> int main() { ...

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

πŸŒ™