Nepal's open education portal

EduNepalEduNepal

C Programming Laboratory Manual — 20 Practical Programs with Output

Official practical laboratory manual containing 20 complete C programming experiments, source codes, flowcharts, and verified outputs for TU BCA.

C Programming · CACS101 · Semester 1

BCA Curriculum 2079

Unit 3•Unit 3 of 3

Unit 3: Input and Output Operations

```c #include <stdio.h>

int main() { int r1, c1, r2, c2; int a[10][10], b[10][10], c[10][10];

printf("Enter rows and columns for First Matrix: "); scanf("%d %d", &r1, &c1); printf("Enter rows and columns for Second Matrix: "); scanf("%d %d", &r2, &c2);

if (c1 != r2) { printf("Error: Matrix multiplication not possible! Columns of A (%d) != Rows of B (%d)\n", c1, r2); return 1; }

printf("\nEnter elements of Matrix A:\n"); for (int i = 0; i < r1; i++) { for (int j = 0; j < c1; j++) { scanf("%d", &a[i][j]); } }

printf("\nEnter elements of Matrix B:\n"); for (int i = 0; i < r2; i++) { for (int j = 0; j < c2; j++) { scanf("%d", &b[i][j]); } }

// Multiply matrices: c[i][j] = sum(a[i][k] b[k][j]) for (int i = 0; i < r1; i++) { for (int j = 0; j < c2; j++) { c[i][j] = 0; for (int k = 0; k < c1; k++) { c[i][j] += a[i][k] b[k][j]; } } }

printf("\nProduct Matrix (Resultant %d x %d):\n", r1, c2); for (int i = 0; i < r1; i++) { for (int j = 0; j < c2; j++) { printf("%4d ", c[i][j]); } printf("\n"); }

return 0; } ```

Sample Output:

```text Enter rows and columns for First Matrix: 2 3 Enter rows and columns for Second Matrix: 3 2

Enter elements of Matrix A: 1 2 3 4 5 6

Enter elements of Matrix B: 7 8 9 1 2 3

Product Matrix (Resultant 2 x 2): 31 19 85 55 ```

Related notes

View all →

Related video lessons

View all →

Practice questions

View all →
  • Which of the following is an invalid variable name in C? (a) _salary (b) 1st_rank (c) total_sum (d) age2objective · 1 marks
  • What is the return type of the `malloc()` function in C? (a) `int*` (b) `char*` (c) `void*` (d) `float*`objective · 1 marks
  • Explain the difference between call by value and call by reference in C with suitable code snippets.short · 5 marks
  • What is recursion? Write a recursive function in C to calculate the factorial of a positive integer.short · 5 marks
  • Explain dynamic memory allocation in C. Differentiate between `malloc()` and `calloc()`. Write a C program to dynamically allocate memory for N integers, sort them in ascending order, and free the memory.long · 10 marks

View all questions for this subject →

← Back to all notes