Tribhuwan University

Institute of Science and Technology

2077

Bachelor Level / First Year / First Semester / Science

Bachelors in Information Technology (BIT102)

(C Programming)

Full Marks: 60

Pass Marks: 24

Time: 3 Hours

Candidates are required to give their answers in their own words as for as practicable.

The figures in the margin indicate full marks.

Section A

Long Answers Questions

Attempt any TWO questions.
[2*10=20]
1.
Explain the role of function in programming. How function is declared, defined and called in C?Write a grogram to find factorial of 'n' using recursive function.[5+5]

Role of Function in Programming

A function is a self-contained block of code that performs a specific task and can be reused multiple times in a program.

Role of Function in Programming

  • Modularity — Breaks a large program into smaller, manageable parts
  • Reusability — Same function can be called multiple times without rewriting code
  • Readability — Makes program easier to understand and maintain
  • Debugging — Errors can be isolated and fixed within individual functions
  • Abstraction — Hides complex logic and provides a simple interface

Function Declaration (Prototype)

Tells the compiler about the function's name, return type, and parameters before it is used.

return_type function_name(parameter_list);

Example: int add(int a, int b);

Function Definition

Contains the actual body of the function with the code to be executed.

int add(int a, int b) {
    return a + b;
}

Function Call

Invokes the function to execute its code by passing actual arguments.

int result = add(5, 3);

Conclusion: Functions make programs organized, reusable, and easier to debug by dividing complex problems into smaller sub-problems.


Program to Find Factorial of 'n' Using Recursive Function [5 marks]

A recursive function is a function that calls itself repeatedly until a base condition is met.

#include <stdio.h>

int factorial(int n) {
    if (n == 0 || n == 1)
        return 1;           // Base condition
    else
        return n * factorial(n - 1);  // Recursive call
}

int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    printf("Factorial of %d = %d", n, factorial(n));
    return 0;
}

How It Works (for n = 5):

  • factorial(5) = 5 × factorial(4)
  • factorial(4) = 4 × factorial(3)
  • factorial(3) = 3 × factorial(2)
  • factorial(2) = 2 × factorial(1)
  • factorial(1) = 1 (Base condition reached)

Final Result: 5 × 4 × 3 × 2 × 1 = 120

Conclusion: Recursion simplifies problems like factorial by breaking them into smaller identical sub-problems until the base case is reached.

2.
What is an Identifier and keyword? Explain the rules for naming valid identifiers in C with example.Write a C program to find sum of digits of a 'n' digit number.[5+5]
3.
How structure is different from union? Write a program to store and display basic information (roll, name, address, email, and phone) of students of using a structure.[10]
Section B

Short Answers Questions

Attempt any Eight questions.
[8*5=40]
4.
What is the basic structure of a C program? Explain each part. [5]
5.
What do you mean by multi-dimensional array? Write program logic to add and display the sum of two m x n matrices. [5]
6.
Find errors (If any) in the following program and rewrite it.
#define (studio,h)\# \text{define (studio,h)}
float main < >\text{float main < >}
int m [ ] [ ] =  1,2,3,4;\text{int m [ ] [ ] = {{ 1,2,3,4}};}
for(i=4,I>=0;i)for (i = 4, I>=0 ; i--)
for(j=0,j<4;j++);for (j=0, j <4; j++);
m[i] [j] = a[i][j] + b[i][j];\text{m[i] [j] = a[i][j] + b[i][j];}
return 0. 0;\text{return 0. 0;}
}\}
[5]
7.
Write a program to generate the following pattern of Integers.
11
121 2
1231 2 3
12341 2 3 4
123451 2 3 4 5
[5]
8.
Write a C program to check whether a given string is palindrome or not. [5]
9.
What is pointer? Illustrate the use of a double pointer with suitable example. [5]
10.
Why file is used in programming? Explain different file opening modes. [5]
11.
Write short notes on: a) Macros Write short notes on: b) Null Pointers [2.5+2.5]
12.
How break statement is different from continue statement? Explain with suitable example. [5]