Skip to content

PROG SKILL (204) Set F - Practical Solutions

Paper Information

Attribute Value
Subject Programming Skills Using C
Subject Code 204 (PROG SKILL)
Set F
Exam Type Practical
Max Marks 25
Month/Year April 2024

Question 1: Recursive Factorial Function

Question 1: User Defined Recursive Function

Q: Write a C program to create user define recursive function to find factorial of inputted number.

Requirements: - Create a recursive function for factorial - Accept number as parameter - Return calculated factorial - Handle base case (0! = 1) - Call from main() and display result

Solution - C Language
#include <stdio.h>

// Recursive function to calculate factorial
long long factorial(int n) {
    // Base case
    if (n == 0 || n == 1) {
        return 1;
    }
    // Recursive case
    else {
        return n * factorial(n - 1);
    }
}

int main() {
    int num;
    long long result;

    // Input from user
    printf("Enter a number: ");
    scanf("%d", &num);

    // Validate input
    if (num < 0) {
        printf("Factorial is not defined for negative numbers.\n");
    } else {
        // Call recursive function
        result = factorial(num);

        // Display result
        printf("Factorial of %d = %lld\n", num, result);
    }

    return 0;
}
Expected Output - C
Enter a number: 5
Factorial of 5 = 120

Enter a number: 0
Factorial of 0 = 1

Enter a number: 7
Factorial of 7 = 5040
Solution - Python
# Recursive function to calculate factorial
def factorial(n):
    # Base case
    if n == 0 or n == 1:
        return 1
    # Recursive case
    else:
        return n * factorial(n - 1)

# Main program
num = int(input("Enter a number: "))

# Validate input
if num < 0:
    print("Factorial is not defined for negative numbers.")
else:
    # Call recursive function
    result = factorial(num)
    print(f"Factorial of {num} = {result}")
Expected Output - Python
Enter a number: 5
Factorial of 5 = 120

Enter a number: 0
Factorial of 0 = 1

Enter a number: 7
Factorial of 7 = 5040
Explanation

Recursion Concepts: - Base Case: When n=0 or n=1, return 1 (stops recursion) - Recursive Case: n × factorial(n-1) (calls itself with smaller value) - Call Stack: Each call waits for next to complete

Execution for factorial(5):

factorial(5) = 5 × factorial(4)
             = 5 × 4 × factorial(3)
             = 5 × 4 × 3 × factorial(2)
             = 5 × 4 × 3 × 2 × factorial(1)
             = 5 × 4 × 3 × 2 × 1
             = 120

Concept Deep Dive: Recursion

How Recursion Works: 1. Function calls itself with modified parameter 2. Each call adds to call stack 3. Base case stops the chain 4. Returns unwind the stack 5. Final result propagates back

Pros: Elegant, readable for mathematical problems Cons: Stack overflow for large n, memory overhead


Question 2: Viva Preparation

Question 2: Viva + Journal

Q: Viva + Journal (5 Marks)

Potential Viva Questions

Q1: What is recursion? - A: A function calling itself to solve smaller instances of the same problem until reaching a base case.

Q2: What is base case and why is it important? - A: Base case is the condition that stops recursion. Without it, function calls infinitely causing stack overflow.

Q3: How is factorial(0) = 1 justified? - A: Mathematically, empty product equals 1. It also serves as base case for recursion.

Q4: What is the maximum value for which we can calculate factorial? - A: Depends on data type. long long handles up to 20! (2,432,902,008,176,640,000).

Q5: Can we write this program without recursion? - A: Yes, using iterative approach with for/while loop.

Common Pitfalls

  • Missing base case: Causes infinite recursion/stack overflow
  • Integer overflow: Use long long for larger values
  • Negative input: Factorial undefined for negatives
  • Stack overflow: Recursion depth limited (~1000 calls)
  • Return type: Match return type with expected result size

Set Link
Set A Solutions
Set B Solutions
Set C Solutions
Set D Solutions
Set E Solutions
Set F Current Page

Quick Navigation