Skip to content

IP-14.c

Problem Statement

Write a program to calculate the factorial of a number (i) using recursion (ii) using iteration

Metadata

Property Detail
Author Amit Dutta amitdutta4255@gmail.com
Date 03 Jan 2026
License MIT License (See the LICENSE file for details)
Difficulty Beginner (index: 2 / 10)

Concepts

Beta Feature

This concept detection system is still in beta and may occasionally show incorrect or incomplete results.

  • Recursion
  • Pointers
  • Iteration

Actions

Raw View on GitHub

You can print or save this file by opening Raw and using your browser.

Source Code

#include <stdio.h>

long long int fact_tail_rec(int, long long int);
long long int fact_rec(int);
long long int fact_ite(int);

int main()
{
    int n;
    printf("Enter the number: ");
    scanf("%d", &n);
    if (n < 0)
    {
        printf("\nFactorial of negetive number is not possible.");
        return 1;
    }
    printf("\nFactorial of %d (Tail-Recursion) =  %lld", n, fact_tail_rec(n, 1));
    printf("\nFactorial of %d (Recursion)      =  %lld", n, fact_rec(n));
    printf("\nFactorial of %d (Iteration)      =  %lld", n, fact_ite(n));
    return 0;
}

long long int fact_tail_rec(int n, long long int result)
{
    if (n == 0 || n == 1)
    {
        return result;
    }
    else
    {
        return fact_tail_rec(n - 1, n * result);
    }
}

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

long long int fact_ite(int n)
{
    int i;
    long long int result = 1;
    if (n == 0 || n == 1)
    {
        return 1;
    }
    for (i = 2; i <= n; i++)
    {
        result *= i;
    }
    return result;
}

Explanation

Explain with AI

Copy the prompt below and paste it into any AI assistant.

    You are explaining a C programming code to a beginner.

    STRICT RULES:

    - Only use the given code. Do NOT assume anything not present.

    - Do NOT add extra examples.

    - Keep explanation clear and short.

    - If something is unclear, say "Not clear from code".

    - Follow the exact format below. Do NOT change headings.

    FORMAT:

    [START]

    ## What it does

    (Explain the overall purpose in 1-2 sentences)

    ## Step-by-step

    (Explain how the code works in steps, simple language)

    ## Key Concepts

    (List concepts like loop, condition, function, etc.)

    ## Notes

    (Mention any limitations, errors, or assumptions)

    [END]

    CODE (IP-14.c):

    #include <stdio.h>

    long long int fact_tail_rec(int, long long int);
    long long int fact_rec(int);
    long long int fact_ite(int);

    int main()
    {
        int n;
        printf("Enter the number: ");
        scanf("%d", &n);
        if (n < 0)
        {
            printf("\nFactorial of negetive number is not possible.");
            return 1;
        }
        printf("\nFactorial of %d (Tail-Recursion) =  %lld", n, fact_tail_rec(n, 1));
        printf("\nFactorial of %d (Recursion)      =  %lld", n, fact_rec(n));
        printf("\nFactorial of %d (Iteration)      =  %lld", n, fact_ite(n));
        return 0;
    }

    long long int fact_tail_rec(int n, long long int result)
    {
        if (n == 0 || n == 1)
        {
            return result;
        }
        else
        {
            return fact_tail_rec(n - 1, n * result);
        }
    }

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

    long long int fact_ite(int n)
    {
        int i;
        long long int result = 1;
        if (n == 0 || n == 1)
        {
            return 1;
        }
        for (i = 2; i <= n; i++)
        {
            result *= i;
        }
        return result;
    }