BSc CSIT C Programming Important Programs & Viva Questions (TU 1st Semester) | PDF Resource

Introduction
Two things determine your C Programming grade at TU: how well you write programs in the practical exam, and how confidently you answer viva questions about what your programs do and why. Theory knowledge helps, but the marks difference between students usually comes down to these two practical skills.
This page is a dedicated programs and viva resource — not another general notes page. The Complete C Programming (CSC115) Guide has full explanations of every concept. The C Programming Exam Preparation Guide has exam strategy, topic priority ratings, and study plans. This page gives you something both of those don't: complete, compilable programs organized by exam frequency, and viva questions with full answers so you know not just what to say but why it's correct.
Official Course Information
| Particular | Details |
|---|---|
| Course Title | C Programming |
| Course Code | CSC115 |
| Semester | First Semester |
| Nature | Theory + Lab |
| Full Marks | 60 + 20 + 20 |
| Pass Marks | 24 + 8 + 8 |
Most Frequently Asked Programs (Ranked by Exam Frequency)
These are organized from most to least likely to appear. The top eight are considered near-certain exam fixtures.
1. Prime Number Check
★★★★★ (Appears almost every year)
#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a number: ");
scanf("%d", &n);
if (n <= 1) {
printf("%d is not prime.\n", n);
return 0;
}
for (i = 2; i <= n / 2; i++) {
if (n % i == 0) { flag = 1; break; }
}
printf("%d is %s\n", n, flag == 0 ? "Prime" : "Not Prime");
return 0;
}
Key concept to explain in viva: We check divisors only up to n/2 (not n) because if n has a factor larger than n/2, the corresponding factor pair would be less than 2, which is not a valid divisor.
2. Fibonacci Series
★★★★★ (Iterative and recursive versions both asked)
Iterative version:
#include <stdio.h>
int main() {
int n, a = 0, b = 1, c, i;
printf("Enter terms: ");
scanf("%d", &n);
printf("%d %d ", a, b);
for (i = 3; i <= n; i++) {
c = a + b;
printf("%d ", c);
a = b;
b = c;
}
return 0;
}
Recursive version:
int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
Key concept: The recursive version has O(2ⁿ) time complexity — exponential. The iterative version is O(n). Know this for viva.
3. Factorial (Recursive)
★★★★★
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1) return 1;
return n * factorial(n - 1);
}
int main() {
int n;
printf("Enter n: ");
scanf("%d", &n);
printf("%d! = %d\n", n, factorial(n));
return 0;
}
Key concept: Base case is critical. Without if (n == 0) return 1, the function would recurse infinitely (stack overflow).
4. Palindrome Check (Number)
★★★★★
#include <stdio.h>
int main() {
int n, rev = 0, orig, rem;
printf("Enter number: ");
scanf("%d", &n);
orig = n;
while (n != 0) {
rem = n % 10;
rev = rev * 10 + rem;
n /= 10;
}
printf("%d is %s\n", orig, orig == rev ? "Palindrome" : "Not Palindrome");
return 0;
}
5. Swap Two Numbers Using Pointers
★★★★★ (The standard call-by-reference demonstration)
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 5, y = 10;
printf("Before: x=%d, y=%d\n", x, y);
swap(&x, &y);
printf("After: x=%d, y=%d\n", x, y);
return 0;
}
Key concept: &x passes the address. *a inside the function dereferences it — reads or writes the value at that address. Without pointers, the swap would only affect local copies.
6. Matrix Addition
★★★★★
#include <stdio.h>
#define R 3
#define C 3
int main() {
int a[R][C], b[R][C], sum[R][C], i, j;
printf("Enter matrix A:\n");
for (i = 0; i < R; i++)
for (j = 0; j < C; j++)
scanf("%d", &a[i][j]);
printf("Enter matrix B:\n");
for (i = 0; i < R; i++)
for (j = 0; j < C; j++)
scanf("%d", &b[i][j]);
for (i = 0; i < R; i++)
for (j = 0; j < C; j++)
sum[i][j] = a[i][j] + b[i][j];
printf("Sum matrix:\n");
for (i = 0; i < R; i++) {
for (j = 0; j < C; j++)
printf("%d\t", sum[i][j]);
printf("\n");
}
return 0;
}
7. Matrix Transpose
★★★★★
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
transpose[j][i] = matrix[i][j];
Key concept: The indices swap — row i, col j becomes row j, col i. For an m×n matrix, the transpose is n×m.
8. Bubble Sort
★★★★★
#include <stdio.h>
int main() {
int arr[] = {64, 34, 25, 12, 22};
int n = 5, i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Key concept: Each pass bubbles the largest unsorted element to its correct position. After k passes, the last k elements are sorted. Time complexity: O(n²).
9. Binary Search
★★★★☆
#include <stdio.h>
int binarySearch(int arr[], int n, int target) {
int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == target) return mid;
else if (arr[mid] < target) low = mid + 1;
else high = mid - 1;
}
return -1;
}
int main() {
int arr[] = {10, 20, 30, 40, 50};
int result = binarySearch(arr, 5, 30);
printf(result != -1 ? "Found at index %d\n" : "Not found\n", result);
return 0;
}
Key concept: Array MUST be sorted. Each comparison eliminates half the remaining elements. Time complexity: O(log n).
10. Armstrong Number Check
★★★★☆
#include <stdio.h>
#include <math.h>
int main() {
int n, orig, rem, result = 0, digits = 0, temp;
printf("Enter number: ");
scanf("%d", &n);
temp = n;
while (temp != 0) { digits++; temp /= 10; }
orig = n;
temp = n;
while (temp != 0) {
rem = temp % 10;
result += (int)pow(rem, digits);
temp /= 10;
}
printf("%d is %s\n", orig, orig == result ? "Armstrong" : "Not Armstrong");
return 0;
}
Key concept: An Armstrong number equals the sum of its digits each raised to the power of the number of digits. 153 = 1³+5³+3³ = 1+125+27 = 153.
11. String Reverse (Without Library Function)
★★★★☆
#include <stdio.h>
int main() {
char str[100], rev[100];
int len = 0, i;
printf("Enter string: ");
scanf("%s", str);
while (str[len] != '\0') len++;
for (i = 0; i < len; i++)
rev[i] = str[len - 1 - i];
rev[len] = '\0';
printf("Reversed: %s\n", rev);
return 0;
}
12. String Palindrome Check
★★★★☆
#include <stdio.h>
#include <string.h>
int main() {
char str[100], rev[100];
printf("Enter string: ");
scanf("%s", str);
strcpy(rev, str);
strrev(rev); /* Use manual reverse if strrev not available */
printf("%s is %s\n", str, strcmp(str, rev) == 0 ? "Palindrome" : "Not Palindrome");
return 0;
}
13. Student Record Using Structure
★★★★☆ (Very common practical lab task)
#include <stdio.h>
struct Student {
int roll;
char name[50];
float marks;
char grade;
};
char getGrade(float m) {
if (m >= 90) return 'A';
if (m >= 75) return 'B';
if (m >= 60) return 'C';
if (m >= 45) return 'D';
return 'F';
}
int main() {
struct Student s;
printf("Roll: "); scanf("%d", &s.roll);
printf("Name: "); scanf("%s", s.name);
printf("Marks: "); scanf("%f", &s.marks);
s.grade = getGrade(s.marks);
printf("\nRoll: %d | Name: %s | Marks: %.1f | Grade: %c\n",
s.roll, s.name, s.marks, s.grade);
return 0;
}
14. File Handling — Write and Read Records
★★★★☆ (Students skip this; it's easy marks)
#include <stdio.h>
int main() {
FILE *fp;
char name[50];
int marks;
/* Write to file */
fp = fopen("students.txt", "w");
if (fp == NULL) { printf("Error opening file\n"); return 1; }
fprintf(fp, "Alice 85\nBob 72\nCarol 91\n");
fclose(fp);
/* Read from file */
fp = fopen("students.txt", "r");
if (fp == NULL) { printf("Error opening file\n"); return 1; }
printf("Name\tMarks\n");
while (fscanf(fp, "%s %d", name, &marks) != EOF) {
printf("%s\t%d\n", name, marks);
}
fclose(fp);
return 0;
}
Key concept: Always check if fopen returned NULL. Always call fclose. Mode "w" creates or overwrites; "r" requires the file to already exist.
15. Dynamic Memory Allocation
★★★☆☆
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, *arr;
printf("How many numbers? ");
scanf("%d", &n);
arr = (int*) malloc(n * sizeof(int));
if (arr == NULL) { printf("Memory allocation failed\n"); return 1; }
for (i = 0; i < n; i++) scanf("%d", &arr[i]);
for (i = 0; i < n; i++) printf("%d ", arr[i]);
free(arr); /* Always free allocated memory */
return 0;
}
Viva Questions with Complete Answers
These are the questions lab examiners ask most consistently. Short, precise answers score better than long rambling ones.
Q: Why does C need & in scanf but not in printf?
A: scanf needs to store a value at a memory address — it needs to know where to write the input. &variable gives it that address. printf only needs to read the value — it reads the value directly, no address needed.
Q: What is a pointer? What does *p mean versus &p?
A: A pointer is a variable that stores a memory address. &p gives the address of variable p itself. *p (when p is a pointer) dereferences p — it reads or writes the value at the address stored in p.
Q: What is the difference between call by value and call by reference?
A: In call by value, a copy of the variable is passed — changes inside the function don't affect the original. In call by reference, the address of the variable is passed — changes inside the function through the pointer modify the original variable.
Q: What is recursion? What happens without a base case?
A: Recursion is when a function calls itself to solve a smaller version of the same problem. Without a base case, the function calls itself indefinitely until the stack overflows and the program crashes with a segmentation fault.
Q: What is the difference between malloc and calloc?
A: Both allocate dynamic memory. malloc(n) allocates n bytes with uninitialized (garbage) content. calloc(n, size) allocates n×size bytes and initializes all bytes to zero.
Q: What is a dangling pointer?
A: A dangling pointer points to memory that has already been freed or is no longer valid. Dereferencing it causes undefined behavior — it may crash the program or produce incorrect results silently, which is worse. Fix: set the pointer to NULL after freeing — free(ptr); ptr = NULL;
Q: What is a NULL pointer? When should you use it?
A: A NULL pointer explicitly points to nothing (address 0). It's used to initialize pointers when no valid address is assigned yet, and to signal that a function returned no valid result (e.g., malloc returns NULL on failure). Always check if (ptr != NULL) before dereferencing.
Q: What is the difference between while and do-while?
A: A while loop checks the condition before the first iteration — it may never execute. A do-while loop checks after the first iteration — it always executes at least once.
Q: What is ++i versus i++?
A: Both add 1 to i, but at different moments. ++i increments before the expression is evaluated (pre-increment). i++ increments after (post-increment). In a simple loop for(i=0; i<n; i++), the difference doesn't matter. In a = ++i versus a = i++, a receives different values.
Q: What causes a segmentation fault in C?
A: A segmentation fault means the program tried to access memory it doesn't own. Common causes: dereferencing a NULL or uninitialized pointer, accessing an array out of bounds, or using a dangling pointer after free.
Q: Why is free() important after malloc?
A: Dynamic memory allocated with malloc is not automatically released when the function returns — it persists until explicitly freed or the program terminates. Without free, the program gradually consumes more and more RAM (memory leak), which can cause slowdowns or crashes in long-running programs.
Q: What is the difference between a structure and a union?
A: In a structure, each member has its own memory location — all members can hold values simultaneously. In a union, all members share the same memory — only one member can hold a valid value at a time. A union's size equals the size of its largest member; a structure's size is the sum of all members' sizes.
Q: What does fopen return if the file doesn't exist in read mode?
A: It returns NULL. You must always check for NULL before using the file pointer, or the program will crash when it tries to read from an invalid pointer.
Recommended Books
| Book | Author | Best For |
|---|---|---|
| The C Programming Language | Kernighan & Ritchie (K&R) | Deep understanding, industry standard |
| Programming with C | Byron Gottfried | TU textbook, exam-aligned |
| Let Us C | Yashavant Kanetkar | Beginner friendly, lots of examples |
| Programming in ANSI C | E. Balagurusamy | Good for theory questions |
Quick Programs Checklist (Before Exam)
Use this checklist to verify you can write each of these from memory without notes:
- [ ] Prime number check
- [ ] Fibonacci series (both iterative and recursive)
- [ ] Factorial using recursion
- [ ] Palindrome check (number)
- [ ] Swap using pointers
- [ ] Matrix addition
- [ ] Matrix transpose
- [ ] Bubble sort
- [ ] Binary search
- [ ] Armstrong number
- [ ] String reverse without library function
- [ ] Student record using structure
- [ ] Write to file and read from file
- [ ] Dynamic array using malloc/free
If you can write all 14 from memory, you're ready for the practical exam.
Frequently Asked Questions
Which programs are most important for the TU practical exam? Pointer swap, recursive factorial, Fibonacci, prime number, matrix addition, bubble sort, and student record using structures are the most consistently tested. File handling appears regularly and is often skipped by students, making it a reliable source of marks for those who prepare it.
Should I memorize programs or understand them? Understand them — exams vary the specific task (sort different array sizes, check different conditions). A student who understands bubble sort can adapt it; a student who memorized it can't. Understanding also makes viva questions much easier to answer.
What if I make a small syntax error in the practical exam? Show the logic clearly with correct structure. TU lab examiners generally award marks for correct logic even with minor syntax errors. Explain your approach during the viva — correct explanation of correct logic that has a small syntax slip often still earns most marks.
How are viva marks awarded? Viva marks reward clear, concise, correct answers. The examiner typically asks 3–5 questions about your program: what it does, why you used a specific approach, what would happen if you changed something. Answer specifically and don't bluff — saying "I'm not sure but I think..." is better than confidently stating something wrong.
Conclusion
The practical exam and viva together account for 40 of the 100 total marks in CSC115. Students who practice programs until they can write them from memory, and who understand what each line does well enough to explain it, consistently outperform those who only studied the theory.
Use the programs above as your practice set — not to copy, but to understand and then reproduce independently. Use the viva Q&A as both a study tool and a self-test: if you can answer each question in one clear paragraph without looking at the answer, you're ready.
For the complete chapter-by-chapter concept explanations, see the Complete C Programming (CSC115) Guide. For exam strategy, topic priority ratings, and theory questions, see the C Programming Exam Preparation Guide.
Also read Bsc.CSIT Physics notes
C programming is a powerful skill for programmers. By practicing these important questions, you can strengthen your concepts and perform well in exams and interviews. For more resources, keep exploring on utsabojha.com.np.
0 Comments