C Programming Practice Set
Part 1: Coding Exercises
1. Write a function in C to calculate the factorial of a number using recursion.
2. Write a program to reverse a string without using any library function.
3. Write a function that takes an integer array and its size as input and returns the largest element in the
array.
4. Write a program that uses pointers to swap two integers.
5. Write a function that counts the number of vowels in a given string.
Part 2: Debug-the-Code Challenges
1. Pointer Initialization
int main() {
int a = 10;
int *ptr;
*ptr = a;
printf("%d", *ptr);
return 0;
Problem: Program crashes. Why?
2. Array Index Error
int main() {
int arr[5] = {1, 2, 3, 4, 5};
printf("%d", arr[5]);
return 0;
C Programming Practice Set
Problem: Output is undefined or gives garbage value.
3. String Termination Error
int main() {
char str[5] = {'H', 'e', 'l', 'l', 'o'};
printf("%s", str);
return 0;
Problem: String does not print correctly.
4. Memory Allocation
int main() {
int *arr;
arr = malloc(10); // allocate memory
for (int i = 0; i < 10; i++) {
arr[i] = i;
printf("%d", arr[9]);
return 0;
Problem: Sometimes crashes or gives incorrect output.
5. Loop Logic Error
int main() {
C Programming Practice Set
int i = 0;
while (i = 10) {
printf("Loop\n");
i++;
return 0;
Problem: Infinite loop. Why?