BIT102 ยท 2078 ยท 10 marks
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.Write a program to find factorial of a given integer using recursion.[5+5]
Quick answer
An expression in C is a valid combination of operands (variables, constants) and operators that evaluates to a single value.
Expression, Operator, Operator Associativity in C + Factorial Using Recursion
(a) Expression and Operator
Expression
Examples:
| Expression | Type |
|---|---|
a + b * c | Arithmetic expression |
x > y | Relational expression |
a = 10 | Assignment expression |
a && b | Logical expression |
Operator
An operator is a special symbol that instructs the compiler to perform a specific mathematical, logical, or relational operation on one or more operands.
Types of Operators in C:
| Type | Operators | Example |
|---|---|---|
| Arithmetic | +, -, *, /, % | a + b |
| Relational | <, >, <=, >=, ==, != | a > b |
| Logical | &&, ||, ! | a && b |
| Assignment | =, +=, -=, *= | a = 5 |
| Unary | ++, --, - | a++ |
| Bitwise | &, |, ^, ~ | a & b |
| Conditional | ? : | a>b ? a : b |
Operator Associativity
Associativity defines the direction of evaluation when two or more operators of the same precedence appear in a single expression. It resolves ambiguity in such expressions.
There are two types:
- Left to Right (L to R): Evaluation proceeds from left side to right side
- Right to Left (R to L): Evaluation proceeds from right side to left side
Associativity Table:
| Operator Type | Operators | Associativity |
|---|---|---|
| Unary | ++, --, !, - (unary) | Right to Left |
| Arithmetic (high) | *, /, % | Left to Right |
| Arithmetic (low) | +, - | Left to Right |
| Relational | <, >, <=, >= | Left to Right |
| Equality | ==, != | Left to Right |
| Logical AND | && | Left to Right |
| Logical OR | || | Left to Right |
| Assignment | =, +=, -=, *= | Right to Left |
Example 1 (Left to Right):
10 - 4 - 2
= (10 - 4) - 2 // Left to Right
= 6 - 2
= 4
Example 2 (Right to Left):
a = b = c = 5;
// Evaluated as: a = (b = (c = 5))
// c = 5 first, then b = 5, then a = 5
Conclusion: Associativity ensures that expressions with operators of equal precedence are evaluated in a well-defined, unambiguous order.
(b) Program to Find Factorial Using Recursion
Concept of Recursion
Recursion is a programming technique where a function calls itself repeatedly to solve a smaller sub-problem, until a base condition is reached that stops further calls.
Factorial Definition:
factorial(n) = n ร factorial(n-1), for n > 1
factorial(0) = 1 (base case)
factorial(1) = 1 (base case)
C Program
#include <stdio.h>
// Recursive function to calculate factorial
int factorial(int n) {
if (n == 0 || n == 1) // Base condition
return 1;
else
return n * factorial(n - 1); // Recursive call
}
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
if (num < 0)
printf("Factorial is not defined for negative numbers.");
else
printf("Factorial of %d = %d\n", num, factorial(num));
return 0;
}
Sample Output
Enter a positive integer: 5
Factorial of 5 = 120
Trace of Recursive Calls for n = 5
factorial(5)
= 5 ร factorial(4)
= 4 ร factorial(3)
= 3 ร factorial(2)
= 2 ร factorial(1)
= 1 (base case)
= 2 ร 1 = 2
= 3 ร 2 = 6
= 4 ร 6 = 24
= 5 ร 24 = 120
Final Result: 5! = 120
Key Point: Every recursive function must have a base case to prevent infinite recursion and stack overflow.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.