Tribhuwan University

Institute of Science and Technology

2078

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.
What is an expression and operator? Discuss operator associativity in C.Write a program to find factorial of a given integer using recursion.[5+5]

What is an Expression and Operator? Discuss Operator Associativity in C

Expression is a combination of operands (variables, constants) and operators that evaluates to a single value. Operator is a symbol that performs a specific operation on operands.

Expression

An expression in C is any valid combination of operators, constants, and variables that produces a result.

Examples:

  • a + b → Arithmetic expression
  • x > y → Relational expression
  • a = 10 → Assignment expression

Operator

An operator is a special symbol that tells the compiler to perform a mathematical or logical operation.

Types: Arithmetic (+, -, *, /, %), Relational (<, >, ==), Logical (&&, ||, !), Assignment (=), etc.

Operator Associativity

Associativity defines the direction in which operators of the same precedence are evaluated in an expression — either Left to Right (L→R) or Right to Left (R→L).

Operator Type Operators Associativity
Arithmetic *, /, % then +, - Left to Right
Relational <, >, <=, >= Left to Right
Assignment =, +=, -= Right to Left
Unary ++, --, ! Right to Left

Example: In a = b = c = 5;, assignment is R→L, so c=5 is evaluated first, then b=c, then a=b.

Conclusion: Associativity resolves ambiguity when multiple operators of equal precedence appear in a single expression.


Program to Find Factorial Using Recursion

Recursion is a technique where a function calls itself to solve a smaller sub-problem until a base condition is met.

Logic: factorial(n) = n × factorial(n-1), with base case factorial(0) = 1

#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 num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("Factorial of %d = %d", num, factorial(num));
    return 0;
}

Sample Output:

Enter a number: 5
Factorial of 5 = 120

Explanation of Recursive Calls 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) → returns 1 (base case)

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

2.
What is an Identifier and Keyword? Explain the rules for naming valid Identifiers in C with example.Write a program to convert a given string to lowercase without using any library functions.[5+5]
3.
What different data types available in C along with their respective range?Write a program to check whether a given integer is palindrome or not.[5+5]
Section B

Short Answers Questions

Attempt any Eight questions.
[8*5=40]
4.
How break statement is different from continue statement. Explain with examples? [5]
5.
What is the basic structure of a C program? Explain each part. [5]
6.
What do you mean by multi-dimensional array? Write a program to display the sum of two mxn matrices. [5]
7.
Write a program to create a file "duplicate" that contains the exact copy of file "original". [5]
8.
Explain any four input/output functions used in C language with suitable example [5]
9.
Write a program to generate the following pattern of integers.
11
1211 2 1
123211 2 3 2 1
12343211 2 3 4 3 2 1
[5]
10.
Write a program to find smallest element of an array using a pointer. [5]
11.
Write a program to store and display basic information (Roll, name, address, email. and phone) of students of using a structure. [5]
12.
Write Short Notes on: a) Escape Sequence Write Short Notes on: b) Null Pointer [2.5+2.5]