FAQs in C: Find Factorial using Recursion
FAQs in C: Find Factorial using Recursion

FAQs in C: Find Factorial using Recursion

C Program

Here’s a C program that calculates the factorial of a number using recursion:

C
#include <stdio.h>

// Step 2: Function to calculate factorial using recursion
int factorial(int n) {
    if (n == 1) // Step 3: Base case
        return 1;
    else // Step 4: Recursive case
        return n * factorial(n - 1);
}

int main() {
    int n;
    
    // Step 5: Read values from the user
    printf("Enter an integer: ");
    scanf("%d", &n);

    // Step 5: Call the factorial function and store the result
    if (n < 0) {
        printf("Invalid input! Factorial of a negative number doesn't exist.\n");
    } else {
        int result = factorial(n);
        // Step 5: Print the result
        printf("Factorial of %d = %d\n", n, result);
    }

    return 0;
}

Explanation:

Include the Standard Input-Output header file: #include <stdio.h> is used to include the standard input-output library which allows us to use functions like printf and scanf.

Declare the factorial function:

  • int factorial(int n) is the function declaration. It takes an integer n as an argument and returns an integer.
  • Inside the function, we check if n is 0 or 1. If it is, we return 1 (the base case).
  • Otherwise, we return n multiplied by the result of calling factorial(n – 1) (the recursive case).

Declare the main function: int main() is the main function where the execution of the program begins.

Declare integer variables:

  • int n; declares an integer variable n to store the input number.

Read input from the user:

  • printf(“Enter an integer: “); prompts the user to enter an integer.
  • scanf(“%d”, &n); reads the integer input from the user and stores it in n.

Check if the input is valid:

  • if (n < 0) { printf(“Invalid input! Factorial of a negative number doesn’t exist.\n”); } checks if the input number is negative. If it is, it prints an error message.

Call the factorial function and store the result:

  • int result = factorial(n); calls the factorial function with n and stores the result in the variable result.

Print the result:

  • printf(“Factorial of %d = %d\n”, n, result); prints the calculated factorial.

Return statement: return 0; indicates that the program ended successfully.

Algorithm

Plaintext
Step 1: Start
Step 2: Declare a function factorial that takes an integer n as an argument and returns an integer.

Step 3: If n is equal to 0 or 1, return 1.

Step 4: Otherwise, return n multiplied by the result of factorial(n-1).
Step 5: In the main function:
          Declare an integer variable n to store the input number.
          Read the value of n from the user.
          Call the factorial function with n and store the result.
          Print the result.
Step 6: Stop

Detailed Explanation of Each Step

Start: This is the beginning of the algorithm.

Declare the factorial function:

  • The factorial function is declared to calculate the factorial of a number using recursion.
  • Base Case: If n is 0 or 1, return 1 because the factorial of 0 or 1 is 1.
  • Recursive Case: Otherwise, return n * factorial(n – 1). This breaks down the problem into smaller subproblems, eventually reaching the base case.

Declare the main function:

  • The main function is the entry point of the program.

Declare an integer variable n:

  • Before performing any operations, we need a variable to store the number for which we will calculate the factorial.

Read the value of n from the user:

  • Using input functions like scanf in C, we read the integer value from the user and store it in n.

Check if n is less than 0:

  • If the user inputs a negative number, we print an error message since the factorial of a negative number doesn’t exist.

Call the factorial function and store the result:

  • We call the factorial function with the input number n and store the returned result.

Print the result:

  • Output the calculated factorial to the user.

Stop: The end of the algorithm signifies that the program has completed its task.


Discover more from lounge coder

Subscribe to get the latest posts sent to your email.

Leave a Reply

Your email address will not be published. Required fields are marked *

Discover more from lounge coder

Subscribe now to keep reading and get access to the full archive.

Continue reading