AKS University, Satna (M.P.
)
BCA (AI/ML) — 2nd Semester
Programming Methodology & Data Structure
Complete Lab Notes + 40 MCQ Practice Questions
# Program Unit Status
1 Find Area of a Circle Unit-1 Completed
2 Print Table of Any Number Unit-1 Completed
3 Swap the Contents of Two Variables Unit-1 Completed
4 Factorial of a Number Using Recursion Unit-2 Completed
5 Generate Even & Odd Numbers from 1 to 100 Unit-2 Completed
6 Print Sum of Two Matrices Unit-2 Completed
— 40 MCQ Practice Questions (with answers) Both Included
UNIT - 1 | LAB NOTES
Program 1: Find Area of a Circle
Aim:
To write a C program that calculates and displays the area of a circle when the radius is given by the user.
Theory:
The area of a circle is calculated using the formula: Area = pi * r * r (where pi = 3.14159, r = radius). In C, we use
float data type for decimal values, scanf() to take input and printf() to display the result.
Algorithm:
1. Start
2. Declare: radius (float), area (float)
3. Input: Read radius from user
4. Calculate: area = 3.14159 * radius * radius
5. Output: Print area
6. Stop
Source Code (C Language):
#include <stdio.h>
int main() {
float radius, area;
float pi = 3.14159;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = pi * radius * radius;
printf("Area of the Circle = %.2f\n", area);
return 0;
}
Output:
Enter the radius of the circle: 5 Area of the Circle = 78.54
Result:
The program successfully calculates the area of a circle for the given radius.
Key Concepts: float data type, scanf(), printf(), %.2f format, arithmetic operators
Program 2: Print Table of Any Number
Aim:
To write a C program that prints the multiplication table of any number entered by the user.
Theory:
A multiplication table shows the product of a number with 1 through 10. We use a for loop to iterate from 1 to 10. For
loop syntax: for(init; condition; increment). The loop variable is multiplied with the input number at each step.
Algorithm:
1. Start
2. Declare: n (int), i (int)
3. Input: Read number n
4. Loop: for i = 1 to 10
Print: n x i = n*i
5. Stop
Source Code (C Language):
#include <stdio.h>
int main() {
int n, i;
printf("Enter a number to print its table: ");
scanf("%d", &n);
printf("\nMultiplication Table of %d:\n", n);
printf("---------------------------\n");
for (i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", n, i, n * i);
}
return 0;
}
Output:
Enter a number to print its table: 5 Multiplication Table of 5:
--------------------------- 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 =
30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50
Result:
The program successfully prints the multiplication table using a for loop.
Key Concepts: for loop, %d format specifier, integer arithmetic, scanf/printf
Program 3: Swap the Contents of Two Variables
Aim:
To write a C program that swaps the values of two variables — using a temp variable and without using a temp
variable.
Theory:
Swapping means exchanging values between two variables. Two methods:
• Method 1 (Using temp): temp=a; a=b; b=temp;
• Method 2 (Without temp): a=a+b; b=a-b; a=a-b; (arithmetic trick)
Algorithm (Using Temp):
1. Start
2. Input: Read a and b
3. temp = a
4. a = b
5. b = temp
6. Print a and b
7. Stop
Source Code (C Language):
#include <stdio.h>
int main() {
int a, b, temp;
printf("Enter value of a: ");
scanf("%d", &a);
printf("Enter value of b: ");
scanf("%d", &b);
printf("\nBefore Swapping: a = %d, b = %d\n", a, b);
/* Method 1: Using temporary variable */
temp = a;
a = b;
b = temp;
printf("After Swapping (using temp): a = %d, b = %d\n", a, b);
/* Method 2: Without temporary variable */
printf("\nEnter value of a: ");
scanf("%d", &a);
printf("Enter value of b: ");
scanf("%d", &b);
printf("Before Swapping: a = %d, b = %d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("After Swapping (without temp): a = %d, b = %d\n", a, b);
return 0;
}
Output:
Enter value of a: 10 Enter value of b: 20 Before Swapping: a = 10, b = 20 After Swapping
(using temp): a = 20, b = 10 Enter value of a: 10 Enter value of b: 20 Before Swapping: a =
10, b = 20 After Swapping (without temp): a = 20, b = 10
Result:
The program successfully swaps two variables using both methods.
Key Concepts: temp variable, arithmetic swap (a+b, a-b), assignment operators
UNIT - 2 | LAB NOTES
Program 4: Factorial of a Number Using Recursion
Aim:
To write a C program to find the factorial of a given number using a recursive function.
Theory:
Factorial of n (written n!) = n * (n-1) * (n-2) * ... * 1. Example: 5! = 120. Recursion = a function calling itself. It needs:
(1) Base case — stops recursion, (2) Recursive case — calls itself with smaller value.
• Base case: factorial(0) = 1, factorial(1) = 1
• Recursive case: factorial(n) = n * factorial(n-1)
Recursion Trace (n=5):
factorial(5) = 5 * factorial(4)
= 5 * 4 * factorial(3)
= 5 * 4 * 3 * factorial(2)
= 5 * 4 * 3 * 2 * factorial(1)
= 5 * 4 * 3 * 2 * 1 = 120
Source Code (C Language):
#include <stdio.h>
long int factorial(int n) {
if (n == 0 || n == 1) {
return 1; /* Base case */
} else {
return n * factorial(n - 1); /* Recursive case */
}
}
int main() {
int n;
long int result;
printf("Enter a number: ");
scanf("%d", &n);
if (n < 0) {
printf("Factorial not defined for negative numbers.\n");
} else {
result = factorial(n);
printf("Factorial of %d = %ld\n", n, result);
}
return 0;
}
Output:
Enter a number: 5 Factorial of 5 = 120 Enter a number: 0 Factorial of 0 = 1
Result:
The program successfully calculates factorial using recursion.
Key Concepts: Recursion, base case, recursive call, function, long int, %ld
Program 5: Generate Even and Odd Numbers from 1 to 100
Aim:
To write a C program that generates and displays all even and odd numbers from 1 to 100 separately.
Theory:
A number is Even if divisible by 2 (remainder = 0): num % 2 == 0. A number is Odd if not divisible by 2 (remainder =
1): num % 2 != 0. The modulus operator (%) gives the remainder of division. We use a for loop from 1 to 100 with
if-else to check each number.
Algorithm:
1. Start
2. Loop: for i = 1 to 100
If i % 2 == 0 → print in Even list
Else → print in Odd list
3. Stop
Source Code (C Language):
#include <stdio.h>
int main() {
int i;
printf("Even Numbers from 1 to 100:\n");
printf("----------------------------\n");
for (i = 1; i <= 100; i++) {
if (i % 2 == 0) {
printf("%d ", i);
}
}
printf("\n\nOdd Numbers from 1 to 100:\n");
printf("---------------------------\n");
for (i = 1; i <= 100; i++) {
if (i % 2 != 0) {
printf("%d ", i);
}
}
printf("\n");
return 0;
}
Output:
Even Numbers from 1 to 100: ---------------------------- 2 4 6 8 10 12 14 16 18 20 22 24
26 28 30 32 34 36 38 40 42 44 46 48 50 ... 100 Odd Numbers from 1 to 100:
--------------------------- 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45
47 49 ... 99
Result:
The program successfully generates and displays even and odd numbers from 1 to 100.
Key Concepts: Modulus operator %, for loop, if-else, even/odd logic
Program 6: Print Sum of Two Matrices
Aim:
To write a C program that reads two 3x3 matrices and prints their sum.
Theory:
A Matrix is a 2D array. To add two matrices A and B, both must have the same dimensions. Rule: C[i][j] = A[i][j] +
B[i][j] (add corresponding elements). In C, 2D array: int a[3][3];. Two nested for loops are used — outer for rows,
inner for columns.
Algorithm:
1. Start
2. Declare: a[3][3], b[3][3], c[3][3]
3. Input all elements of Matrix A
4. Input all elements of Matrix B
5. Nested loop: c[i][j] = a[i][j] + b[i][j]
6. Print Matrix A, B and C (result)
7. Stop
Source Code (C Language):
#include <stdio.h>
int main() {
int a[3][3], b[3][3], c[3][3];
int i, j;
printf("Enter elements of Matrix A (3x3):\n");
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++) {
printf("A[%d][%d]: ", i, j);
scanf("%d", &a[i][j]);
}
printf("\nEnter elements of Matrix B (3x3):\n");
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++) {
printf("B[%d][%d]: ", i, j);
scanf("%d", &b[i][j]);
}
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
c[i][j] = a[i][j] + b[i][j];
printf("\nMatrix A:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) printf("%4d", a[i][j]);
printf("\n");
}
printf("\nMatrix B:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) printf("%4d", b[i][j]);
printf("\n");
}
printf("\nSum (A + B):\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) printf("%4d", c[i][j]);
printf("\n");
}
return 0;
}
Output:
Matrix A: Matrix B: Sum (A + B): 1 2 3 9 8 7 10 10 10 4 5 6 6 5 4 10 10 10 7 8 9 3 2 1 10
10 10
Result:
The program successfully reads two matrices and prints their element-wise sum.
Key Concepts: 2D arrays, nested for loops, a[i][j] indexing, %4d formatted output
MCQ PRACTICE — 40 QUESTIONS
Programming Methodology & Data Structure | BCA AI/ML 2nd Semester
Correct answers shown in GREEN | Explanation given for each
UNIT-1 | Q1 to Q20 — Circle, Table, Swap + C Basics
1. Which header file is required for printf() and scanf() in C?
A)
B)
C)
D)
Answer: C)
Explanation: stdio.h = Standard Input Output. Always needed for printf() and scanf().
2. What is the correct formula for area of a circle?
A) Area = 2 * pi * r
B) Area = pi * r * r
C) Area = pi * d
D) Area = 2 * pi * r * r
Answer: B) Area = pi * r * r
Explanation: Area = pi * r^2. pi = 3.14159, r = radius.
3. Which data type stores decimal (floating point) values in C?
A) int
B) char
C) float
D) long
Answer: C) float
Explanation: float (and double) store decimal values. int only stores whole numbers.
4. What does %.2f mean in printf()?
A) Print 2 integers
B) Print float with 2 decimal places
C) Print 2 characters
D) Skip 2 digits
Answer: B) Print float with 2 decimal places
Explanation: %.2f = print a float rounded to exactly 2 decimal places. e.g. 3.14159 becomes 3.14.
5. Which format specifier reads a float with scanf()?
A) %d
B) %c
C) %s
D) %f
Answer: D) %f
Explanation: %f is for float. %d = int, %c = char, %s = string.
6. What does for(i=1; i<=5; i++) printf('%d ',i); print?
A) 1 2 3 4
B) 0 1 2 3 4 5
C) 1 2 3 4 5
D) 2 3 4 5 6
Answer: C) 1 2 3 4 5
Explanation: Starts at 1, runs while i<=5, increments by 1. Prints: 1 2 3 4 5.
7. How many times does a multiplication table loop run?
A) 5
B) 12
C) 10
D) 100
Answer: C) 10
Explanation: Table is printed from 1 to 10, so the loop runs exactly 10 times.
8. What is the output of: printf('%d', 7 * 3);?
A) 73
B) 7*3
C) 10
D) 21
Answer: D) 21
Explanation: 7 * 3 = 21. * is the multiplication operator.
9. To swap a and b using temp, what is the FIRST statement?
A) a = b
B) b = temp
C) temp = a
D) b = a
Answer: C) temp = a
Explanation: First save a in temp: temp = a. Then a = b. Then b = temp.
10. Which arithmetic swap is correct (without temp variable)?
A) a=a-b; b=a+b; a=b-a;
B) a=a+b; b=a-b; a=a-b;
C) a=a*b; b=a/b; a=a/b;
D) a=b-a; b=a+b; a=b-a;
Answer: B) a=a+b; b=a-b; a=a-b;
Explanation: a=a+b; b=a-b; a=a-b; correctly swaps a and b without a temp variable.
11. Which operator checks if a number is even or odd?
A) / (division)
B) * (multiply)
C) % (modulus)
D) + (addition)
Answer: C) % (modulus)
Explanation: % gives remainder. num%2==0 means even. num%2!=0 means odd.
12. What is the value of 7 % 2?
A) 3
B) 3.5
C) 0
D) 1
Answer: D) 1
Explanation: 7 / 2 = 3 remainder 1. So 7 % 2 = 1.
13. Which loop is best when number of iterations is known?
A) while
B) do-while
C) for
D) goto
Answer: C) for
Explanation: for loop is ideal when count is known (like 1 to 10 for multiplication table).
14. What is the standard value of pi used in C programs?
A) 3.14
B) 3.141
C) 3.14159
D) 3.1
Answer: C) 3.14159
Explanation: 3.14159 is the commonly used value of pi in C programs.
15. int a=5, b=10; a=a+b; b=a-b; a=a-b; — what are final values of a and b?
A) a=5, b=10
B) a=10, b=5
C) a=15, b=5
D) a=0, b=0
Answer: B) a=10, b=5
Explanation: Arithmetic swap: a gets b's original value (10), b gets a's original value (5).
16. How do you declare a float variable 'area' in C?
A) int area;
B) float area;
C) double area = float;
D) area float;
Answer: B) float area;
Explanation: Syntax: datatype variablename; So: float area;
17. What does for(i=2; i<=10; i+=2) printf('%d ',i); print?
A) 1 3 5 7 9
B) 2 4 6 8 10
C) 2 4 6 8
D) 0 2 4 6 8 10
Answer: B) 2 4 6 8 10
Explanation: Starts at 2, increments by 2, stops at 10. Prints even numbers: 2 4 6 8 10.
18. What does scanf('%d', &n;); do?
A) Prints value of n
B) Declares n
C) Reads integer from keyboard into n
D) Checks if n is valid
Answer: C) Reads integer from keyboard into n
Explanation: scanf reads input. %d for int, &n; is the memory address where value is stored.
19. What is the return type of main() in C?
A) void
B) float
C) char
D) int
Answer: D) int
Explanation: Standard C: main() returns int. 'return 0;' means program ran successfully.
20. Which printf correctly prints '5 x 3 = 15'?
A) printf('%d x %d = %d', 5, 3, 5*3);
B) printf(5, 3, 15);
C) printf('%d x %d', 5, 3);
D) printf('%f x %f = %f', 5, 3, 15);
Answer: A) printf('%d x %d = %d', 5, 3, 5*3);
Explanation: printf('%d x %d = %d', 5, 3, 5*3) prints: 5 x 3 = 15.
UNIT-2 | Q21 to Q40 — Recursion, Even/Odd, Matrices
21. What is recursion in C?
A) A loop that runs 100 times
B) A function that calls itself
C) A variable that stores multiple values
D) A type of array
Answer: B) A function that calls itself
Explanation: Recursion = a function calling itself. Must have a base case to stop.
22. What is the BASE CASE in a recursive factorial function?
A) return n * factorial(n-1)
B) if (n == 100) return 0
C) if (n == 0 || n == 1) return 1
D) while (n > 0)
Answer: C) if (n == 0 || n == 1) return 1
Explanation: Base case stops recursion. factorial(0) = 1 and factorial(1) = 1.
23. What is the value of factorial(5)?
A) 25
B) 15
C) 100
D) 120
Answer: D) 120
Explanation: 5! = 5 * 4 * 3 * 2 * 1 = 120.
24. What is the value of factorial(0)?
A) 0
B) 1
C) -1
D) undefined
Answer: B) 1
Explanation: By definition 0! = 1. This is the base case in recursive factorial.
25. What happens if a recursive function has NO base case?
A) Returns 0
B) Runs once and stops
C) Infinite recursion / stack overflow
D) Prints error
Answer: C) Infinite recursion / stack overflow
Explanation: Without a base case, the function calls itself forever causing stack overflow.
26. How many recursive calls does factorial(4) make before reaching base case?
A) 2
B) 4
C) 3
D) 5
Answer: C) 3
Explanation: factorial(4)->factorial(3)->factorial(2)->factorial(1)[base]. So 3 recursive calls.
27. What is the correct recursive call inside factorial function?
A) return factorial(n+1)
B) return n + factorial(n)
C) return n * factorial(n-1)
D) return n * n
Answer: C) return n * factorial(n-1)
Explanation: factorial(n) = n * factorial(n-1). Reduces n by 1 each call.
28. A number is EVEN if:
A) number % 2 == 1
B) number / 2 == 0
C) number % 2 == 0
D) number + 2 == 0
Answer: C) number % 2 == 0
Explanation: Even = divisible by 2 with remainder 0. So number % 2 == 0.
29. How many even numbers are there from 1 to 100?
A) 49
B) 51
C) 100
D) 50
Answer: D) 50
Explanation: Even: 2,4,6,...,100. Count = 100/2 = 50.
30. How many odd numbers are there from 1 to 100?
A) 50
B) 49
C) 51
D) 48
Answer: A) 50
Explanation: Odd: 1,3,5,...,99. Count = 50.
31. How is a 3x3 2D array declared in C?
A) int a[3,3];
B) int a[3][3];
C) int a(3)(3);
D) array a[3][3];
Answer: B) int a[3][3];
Explanation: 2D array syntax: int arrayname[rows][cols]; So int a[3][3];
32. How do you access element at row 1, column 2 of matrix a?
A) a[1,2]
B) a(1)(2)
C) a[1][2]
D) a{1}{2}
Answer: C) a[1][2]
Explanation: 2D element: a[row][col]. 0-indexed. a[1][2] = row 1, col 2.
33. Rule for adding two matrices A and B?
A) Multiply corresponding elements
B) Add all A elements to first B element
C) Same dimensions; C[i][j] = A[i][j] + B[i][j]
D) Any size can be added
Answer: C) Same dimensions; C[i][j] = A[i][j] + B[i][j]
Explanation: Matrix addition: same dimensions required. Add corresponding elements.
34. How many loops are needed to add two 3x3 matrices?
A) 1 loop
B) 3 loops
C) 2 nested loops
D) 9 separate loops
Answer: C) 2 nested loops
Explanation: 2 nested loops: outer for rows (0-2), inner for columns (0-2).
35. Sum of A={{1,2,3},{4,5,6},{7,8,9}} and B={{9,8,7},{6,5,4},{3,2,1}}?
A) All = 9
B) All = 10
C) All = 5
D) All = 0
Answer: B) All = 10
Explanation: 1+9=10, 2+8=10, ..., 9+1=10. Every element of result = 10.
36. Why use 'long int' for factorial instead of 'int'?
A) Stores decimals
B) Stores larger integer values
C) Stores negative only
D) Stores text
Answer: B) Stores larger integer values
Explanation: Factorial grows very fast (7!=5040, 10!=3628800). long int holds larger values than int.
37. Correct format specifier to print 'long int'?
A) %d
B) %f
C) %ld
D) %li
Answer: C) %ld
Explanation: %ld is for long int. %d is only for regular int.
38. What does: for(i=1;i<=100;i++) if(i%2!=0) printf('%d ',i); print?
A) Even numbers
B) Odd numbers
C) All numbers
D) Multiples of 2
Answer: B) Odd numbers
Explanation: i%2!=0 means remainder is NOT 0, so the number is ODD. Prints odd numbers.
39. Which concept solves a problem by breaking it into smaller versions of itself?
A) Iteration
B) Recursion
C) Looping
D) Branching
Answer: B) Recursion
Explanation: Recursion = solving by calling itself with a smaller input (e.g. factorial(n) uses factorial(n-1)).
40. int a[2][2]={{1,2},{3,4}}; printf('%d', a[1][1]); — what is output?
A) 1
B) 2
C) 3
D) 4
Answer: D) 4
Explanation: a[1][1]: row 1 = {3,4}, column 1 = 4. Answer is 4. (0-based indexing)
Quick Revision Cheat Sheet
Topic Key Formula / Fact
Area of Circle pi * r * r (pi = 3.14159)
float input scanf("%f", &variable)
2 decimal output printf("%.2f", value)
Swap with temp temp=a; a=b; b=temp;
Swap without temp a=a+b; b=a-b; a=a-b;
Even check number % 2 == 0
Odd check number % 2 != 0
Factorial base case if(n==0 || n==1) return 1;
Factorial recursive return n * factorial(n-1);
5! value 5*4*3*2*1 = 120
0! value 1 (by definition)
2D array declaration int a[3][3];
2D element access a[row][col] — 0 indexed
Matrix addition C[i][j] = A[i][j] + B[i][j]
long int format %ld in printf and scanf
Even count 1-100 50 even numbers
Odd count 1-100 50 odd numbers
for loop syntax for(init; condition; increment)
No base case in recursion Infinite recursion = Stack Overflow!
All the Best for your Exam! | AKS University Satna | BCA AI/ML 2nd Sem