Semester
Subject
Year
Tribhuwan University
2078
Bachelor Level / First Year / First Semester / Science
(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.
Long Answers Questions
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.
An expression in C is any valid combination of operators, constants, and variables that produces a result.
Examples:
a + b → Arithmetic expressionx > y → Relational expressiona = 10 → Assignment expressionAn operator is a special symbol that tells the compiler to perform a mathematical or logical operation.
Types: Arithmetic (+, -, *, /, %), Relational (<, >, ==), Logical (&&, ||, !), Assignment (=), etc.
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.
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
Short Answers Questions