C Programming Questions: Must-Solve List for Beginners
Must-solve C programming questions for beginners, covering control flow, recursion, strings, arrays, and matrix operations with working code examples.
C ranks in the top three programming languages on the TIOBE Programming Community Index. Campus placement coding portals across India (HirePro-based tests, eLitmus, and most company-specific assessment platforms) accept it as a submission language.
Why C Belongs in Your Placement Toolkit
The language’s stripped-down syntax is both its appeal and its value as an interview tool. There is no garbage collector saving you from memory errors, no class hierarchy abstracting the iteration, no framework handling the input parsing. Every program you write in C requires you to specify exactly what happens at each step.
That transparency is why interviewers choose C-based questions to test how a student thinks. If you can write a correct sorting algorithm in C, you already understand loops, array indexing, conditionals, and state management. Those four concepts appear in nearly every coding-round problem, regardless of the language the company eventually asks you to work in.
This guide covers 12 program categories across five topic groups: control flow and loops, recursion, strings, 1-D arrays, and 2-D matrices. For each program, a working code example and a hand-trace of the output are included. Work through them in order. The goal is to derive the logic on paper before you touch the keyboard, not to have the code memorised.
Control Flow, Loops, and Recursion Programs
These programs are the entry point of every C curriculum. They test whether you understand loop termination conditions, conditional branching, and the difference between a base case and a recursive step.
Even or Odd
The modulo operator is the workhorse here. If n % 2 returns 0, the number is even; otherwise it is odd.
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n % 2 == 0)
printf("%d is even\n", n);
else
printf("%d is odd\n", n);
return 0;
}
Input 7 gives “7 is odd”; input 12 gives “12 is even”. Interviewers sometimes ask for a version without the modulo operator. Use bitwise AND instead: n & 1 returns 1 for odd numbers and 0 for even.
Prime Number Check
A number n is prime if no integer from 2 to its square root divides it evenly. The loop only needs to reach the square root because every factor pair has at least one factor at or below that threshold.
#include <stdio.h>
int main() {
int n, i, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &n);
if (n < 2) isPrime = 0;
for (i = 2; i * i <= n; i++) {
if (n % i == 0) { isPrime = 0; break; }
}
printf("%d is %sprime\n", n, isPrime ? "" : "not ");
return 0;
}
The loop condition i * i <= n avoids a call to sqrt() and keeps the program dependency-free. For n = 17, the loop checks i = 2, 3, 4 (since 5 times 5 = 25 exceeds 17). None divide 17 evenly, so the output is “17 is prime”.
Factorial Using Recursion
The recursive version of factorial is a classic interview problem. The base case is factorial(0) = 1. Every recursive call multiplies n by the result of the call with n minus 1.
#include <stdio.h>
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factorial of %d = %d\n", n, factorial(n));
return 0;
}
Tracing factorial(5): 5 times factorial(4), which is 5 times 4 times factorial(3), continuing down to 5 times 4 times 3 times 2 times 1 times 1 = 120. Verify by hand before compiling. Interviewers check whether you can trace the call stack, not just run the code.
Fibonacci Series
The Fibonacci series starts with 0 and 1. Each subsequent term is the sum of the two before it. This iterative version avoids stack overhead for large inputs.
#include <stdio.h>
int main() {
int n, a = 0, b = 1, c;
printf("Enter number of terms: ");
scanf("%d", &n);
printf("Fibonacci: %d %d", a, b);
for (int i = 2; i < n; i++) {
c = a + b;
printf(" %d", c);
a = b;
b = c;
}
printf("\n");
return 0;
}
For n = 7 terms, the output is: 0 1 1 2 3 5 8. Verify: 0+1=1, 1+1=2, 1+2=3, 2+3=5, 3+5=8. Correct.
GCD Using the Euclidean Algorithm
The Euclidean algorithm replaces the pair (a, b) with (b, a mod b) in each step until b reaches zero. The remaining value of a is the GCD.
#include <stdio.h>
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("GCD = %d\n", gcd(a, b));
return 0;
}
- Step 1: a=48, b=36. New b = 48 mod 36 = 12, new a = 36.
- Step 2: a=36, b=12. New b = 36 mod 12 = 0, new a = 12.
- b is 0. GCD = 12.
String Manipulation Programs in C
Strings in C are null-terminated character arrays. Understanding that representation makes string programs straightforward: most operations reduce to scanning the array with an index variable and stopping at the null character.
Palindrome Check
Compare characters from both ends of the string, moving inward. If any pair does not match, the string is not a palindrome.
#include <stdio.h>
#include <string.h>
int main() {
char s[100];
printf("Enter a string: ");
scanf("%s", s);
int n = strlen(s), isPalin = 1;
for (int i = 0; i < n / 2; i++) {
if (s[i] != s[n - 1 - i]) { isPalin = 0; break; }
}
printf("%s is %sa palindrome\n", s, isPalin ? "" : "not ");
return 0;
}
For “racecar”: r matches r, a matches a, c matches c. The middle character e stands alone. Output: “racecar is a palindrome”.
Count Vowels in a String
Scan through each character and check whether it is one of a, e, i, o, u after converting to lowercase.
#include <stdio.h>
#include <ctype.h>
int main() {
char s[200];
int count = 0;
printf("Enter a string: ");
fgets(s, sizeof(s), stdin);
for (int i = 0; s[i] != '\0'; i++) {
char c = tolower(s[i]);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
count++;
}
printf("Vowel count: %d\n", count);
return 0;
}
For the input “Engineering”: E, i, e, i, i = 5 vowels. The tolower() call from ctype.h makes the check case-insensitive without adding conditional branches.
Reverse a String
Swap characters at index 0 and n-1, then 1 and n-2, and so on until the two indices meet at the midpoint.
#include <stdio.h>
#include <string.h>
int main() {
char s[100];
printf("Enter a string: ");
scanf("%s", s);
int n = strlen(s);
for (int i = 0; i < n / 2; i++) {
char temp = s[i];
s[i] = s[n - 1 - i];
s[n - 1 - i] = temp;
}
printf("Reversed: %s\n", s);
return 0;
}
“Hello” reversed: H swaps with o, e swaps with l, middle character l stays. Output: “olleH”. Interviewers sometimes ask for the pointer-arithmetic version. The swap logic is identical.
Array and Matrix Programs
Arrays are the data structure behind most intermediate coding questions. Start with the 1-D operations below. For 2-D operations, see matrix addition, subtraction, and multiplication in C, which covers the patterns for matrix programs specifically.
For insert, delete, and search operations on arrays, the core insight is element shifting: inserting at position k requires shifting every element from k onward one position right; deleting requires shifting one position left. Both are linear-time operations in the worst case.
Bubble Sort
Bubble sort makes repeated passes over an array, swapping adjacent elements that are out of order. After each pass, the largest unsorted element reaches its correct position at the end.
#include <stdio.h>
int main() {
int a[] = {64, 34, 25, 12, 22};
int n = 5;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
printf("Sorted: ");
for (int i = 0; i < n; i++) printf("%d ", a[i]);
printf("\n");
return 0;
}
Starting array: {64, 34, 25, 12, 22}. After sorting: {12, 22, 25, 34, 64}. Bubble sort performs at most n(n-1)/2 comparisons (quadratic time), which is why it appears in interviews to test loop logic but is not used in production sorting.
Find the Second Largest Element
A single-pass approach tracks the largest and second-largest values without sorting the full array first.
#include <stdio.h>
int main() {
int a[] = {3, 1, 4, 1, 5, 9, 2, 6};
int n = 8, max1 = a[0], max2 = -1;
for (int i = 1; i < n; i++) {
if (a[i] > max1) { max2 = max1; max1 = a[i]; }
else if (a[i] > max2 && a[i] != max1) max2 = a[i];
}
printf("Second largest: %d\n", max2);
return 0;
}
Tracing through {3, 1, 4, 1, 5, 9, 2, 6}: max1 ends at 9, max2 ends at 6. Output: “Second largest: 6”. One pass, linear time.
The Armstrong number check in C uses the same digit-extraction pattern as the number-theoretic programs above: peel off digits using the modulo operator, process each one, then reduce the number by integer division.
A Practice Strategy That Sticks
Code comprehension and code production are different skills. Reading a working solution once and declaring it understood is how students walk into an interview and find they cannot produce anything on a blank screen.
Work in this order for each program:
- Read the problem. Write the algorithm in plain English or pseudocode first.
- Code from the algorithm, not from memory of a solution.
- Run the program. Predict the output by hand before you see the terminal.
- Test one wrong input: a negative number, an empty string, a single-element array. Watch what the program does.
- Try the variation the interviewer is likely to ask — the iterative version if you wrote the recursive one, or vice versa.
Data structures programs in C, C++, Java, and Python extends this foundation into linked lists, stacks, queues, and trees. These are the topics that appear in the second round of most placement coding assessments, once the beginner-level programs are cleared.
Use the GNU GCC compiler from the start. It is free, runs on every platform, and its error messages point to specific lines. Compile with gcc -Wall -Wextra to enable warnings. This catches type mismatches and uninitialized variables that a correct-looking program can hide.
Writing bubble sort in C makes you count every comparison and every swap. That same counting instinct, knowing exactly what your code does at each step, is the discipline behind building AI applications that work reliably. TinkerLLM at ₹299 is a hands-on course that applies that step-by-step discipline to building LLM-backed applications, a deployable project for your GitHub alongside your C practice collection.
Primary sources
Frequently asked questions
Is C a good language to use in placement coding tests?
Yes. Most campus placement portals used in Indian college placements, including HirePro-based assessments, eLitmus, and company-specific online tests, accept C as a submission language. C forces you to manage memory directly and write explicit loop conditions, which builds stronger fundamentals than starting with Java or Python.
How many C programs should I practise before campus placements?
The 12 program categories in this guide cover the patterns most commonly tested at the beginner level. The goal is not to memorise every program but to understand the underlying logic well enough to adapt when the interviewer changes the input or adds a constraint.
What is the difference between iterative and recursive factorial in C?
An iterative factorial multiplies numbers in a for loop from 1 to n. A recursive version calls itself with n minus 1 as the argument until it reaches the base case where n equals 0, returning 1. Both produce the same result. Recursion is shorter to write but uses more stack memory for large values of n.
Why does the prime number check loop only up to the square root of n?
If n has a factor a that is larger than the square root of n, the corresponding paired factor b must be smaller than the square root, because a times b equals n. Checking up to the square root is sufficient to catch all factor pairs, and it reduces the number of iterations compared to checking all the way up to n minus 1.
How do I check if a string is a palindrome in C?
Use two indices starting at opposite ends of the string. Compare the character at position i with the character at position n minus 1 minus i, moving both indices inward until they meet at the midpoint. If any pair of characters does not match, the string is not a palindrome. The program in this guide uses exactly this two-pointer approach.
What is GCD and how does the Euclidean algorithm find it in C?
GCD stands for Greatest Common Divisor, the largest integer that divides two numbers evenly. The Euclidean algorithm replaces the pair a and b with b and a modulo b in each step until b reaches zero. For the GCD of 48 and 36: first step gives b equals 48 mod 36 equals 12, second step gives b equals 36 mod 12 equals 0, so GCD equals 12.
A self-paced playground for building with LLMs.
TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.
Try TinkerLLM (₹299 launch)