Essential C Programming Exam Questions and Solutions
Store Fibonacci Series in an Array
Exam Source: 2024 Q6(b)
Note: fib[0]=0, fib[1]=1, fib[i] = fib[i-1] + fib[i-2] for i >= 2.
#include <stdio.h>
int main() {
int n, i;
printf("How many terms? ");
scanf("%d", &n);
int fib[n];
fib[0] = 0;
if (n > 1) fib[1] = 1;
for (i = 2; i < n; i++)
fib[i] = fib[i-1] + fib[i-2];
printf("Fibonacci series: ");
for (i = 0; i < n; i++)
printf("%d ", fib[i]);
return 0;
}Dynamic Memory Allocation and Average Calculation
Exam Source: 2024 Q7(c) / 2025 Q6(a) — Repeated both years!
Note: malloc returns void*, cast to int*. Always free() after use. If malloc returns NULL, memory allocation failed.
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i;
printf("Enter size: ");
scanf("%d", &n);
int *arr = (int*) malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
for (i = 0; i < n; i++) {
printf("arr[%d] = ", i);
scanf("%d", &arr[i]);
}
float sum = 0;
for (i = 0; i < n; i++) sum += arr[i];
printf("Average = %.2f\n", sum / n);
free(arr);
return 0;
}Recursive Factorial Function in C
Exam Source: 2025 Q6(c) — Very likely
Note: Base case: factorial(0)=1 and factorial(1)=1. Recursive step: n * factorial(n-1). Recursion uses more memory than iteration but results in cleaner code.
#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;
}Calculate the Sum of Digits of an Integer
Exam Source: 2025 Q4(b)
Note: Use the modulo operator (%) to extract the last digit and division (/ 10) to remove it. Repeat until the number becomes 0.
#include <stdio.h>
int main() {
int n, sum = 0, digit;
printf("Enter a number: ");
scanf("%d", &n);
if (n < 0) n = -n;
while (n != 0) {
digit = n % 10;
sum += digit;
n /= 10;
}
printf("Sum of digits = %d\n", sum);
return 0;
}C Structures: Student Marks and Minimum Score
Exam Source: 2025 Q8(b) — Very likely
Note: Structure members are accessed with a dot (.) for normal variables and an arrow (->) for pointers. Track the index of the minimum value while looping.
#include <stdio.h>
struct Student {
char name[50];
int marks;
};
int main() {
struct Student s[5];
int i, minIdx = 0;
for (i = 0; i < 5; i++) {
printf("Name: ");
scanf("%s", s[i].name);
printf("Marks: ");
scanf("%d", &s[i].marks);
}
for (i = 1; i < 5; i++)
if (s[i].marks < s[minIdx].marks)
minIdx = i;
printf("Minimum marks: %s (%d)\n", s[minIdx].name, s[minIdx].marks);
return 0;
}Copy File Contents Using Command Line Arguments
Exam Source: 2024 Q9(a) / 2025 Q9(a) — Repeated both years!
Note: argc is the argument count. argv[0] is the program name, argv[1] is the source file, and argv[2] is the destination file. Run as: ./program source.txt dest.txt
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: program source dest\n");
return 1;
}
FILE *src = fopen(argv[1], "r");
FILE *dst = fopen(argv[2], "w");
if (!src || !dst) {
printf("File error!\n");
return 1;
}
char ch;
while ((ch = fgetc(src)) != EOF)
fputc(ch, dst);
fclose(src);
fclose(dst);
printf("File copied successfully!\n");
return 0;
}
English with a size of 37.36 KB