💻
NPTEL • Introduction to Programming in C

Study Glance Blog

NPTEL - Introduction to Programming in C - Week 5:Assignment 5 Answers- 2025

C PROGRAMMING • WEEK 5

NPTEL - Introduction to Programming in C - Week 5:Assignment 5 Answers- 2025

NPTEL • Introduction to Programming in C

Week 5 : Assignments - NPTEL >> Introduction to Programming In C - 2025


Week 5 : Assignment 5 - Question 1

Write a recursive program that inputs a line of characters from the user and outputs the line with the characters reversed.

Solution :


#include<stdio.h>
void reverse() 
{
    char c = getchar();
    if (c == EOF) 
    {
        return;
    }
    reverse();
    putchar(c);
}

int main() 
{
    reverse();
    return 0;
}


Week 5 : Assignment 5 - Question 2

C program that generates all binary strings of length N.

Solution :


#include<stdio.h>
void genBinary(char s[], int i, int n) 
{
    if (i == n) 
    {
        s[n] = '\0';
        puts(s);
        return;
    }
    s[i] = '0';
    genBinary(s, i + 1, n);
    s[i] = '1';
    genBinary(s, i + 1, n);
}

int main() 
{
    int n;
    char s[10];
    scanf("%d", &n);
    genBinary(s, 0, n);
    return 0;
}


Week 5 : Assignment 5 - Question 3

Write a recursive C program to find the BlockSum of the array M having size n which is power of 2.

The following is the recursive definition of BlockSum: If size of M is 2, say M = [a, b], where a and b are integers, then
BlockSum(M ) = a − b.
Otherwise (when n > 2), partition M into two subarrays of equal size: M = [A B C D]
The BlockSum of M is defined recursively as : BlockSum(M) = BlockSum(A) − BlockSum(B).

Solution :


#include<stdio.h>

int BlockSum(int *M, int n) 
{
    if (n == 2) 
    {
        return M[0] - M[1];
    } 
    else 
    {
        int half = n / 2;
        int sumA = BlockSum(M, half);
        int sumB = BlockSum(M + half, half);
        return sumA - sumB;
    }
}

int main() 
{
    int n, a[20];
    scanf("%d", &n);
    for (int i = 0; i < n; i++) 
    {
        scanf("%d", &a[i]);
    }
    int result = BlockSum(a, n);
    printf("%d", result);
    return 0;
}