
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&
...
Continue reading "Essential C Programming Exam Questions and Solutions" »