Top 30 C Language Coding Questions and Answers
Top 30 C Language Coding Questions and Answers with Logical Explanation
Whether you’re a student, a beginner programmer, or preparing for interviews, understanding how to solve C programming problems logically is crucial. This blog provides 30 common and important C coding questions with detailed, beginner-friendly explanations and sample output.
🔢 Q1: Write a C program to check if a number is even or odd.
Explanation:
If a number is divisible by 2 (i.e., remainder is 0), it is even; else, it’s odd.
🧮 Q2: Find the factorial of a number using recursion.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <stdio.h> int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } int main() { int num; printf("Enter a number: "); scanf("%d", &num); printf("Factorial of %d is %d\n", num, factorial(num)); return 0; } |
Example:factorial(5)
returns 120
.
Logic:
This uses recursion. The base case is when n == 0
. The function keeps calling itself with smaller values until it reaches the base case.
🔄 Q3: Reverse a number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <stdio.h> int main() { int num, reversed = 0; printf("Enter a number: "); scanf("%d", &num); while (num != 0) { reversed = reversed * 10 + num % 10; num /= 10; } printf("Reversed number: %d\n", reversed); return 0; } |
🧠 Explanation:
This logic repeatedly extracts the last digit of the number and builds the reversed number digit by digit. It’s a great example of loop and modulus usage.
Example:Input: 1234 → Output: 4321
🔢 Q4: Print Fibonacci series up to n terms.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <stdio.h> int main() { int n, first = 0, second = 1, next, i; printf("Enter the number of terms: "); scanf("%d", &n); printf("Fibonacci Series: "); for (i = 0; i < n; i++) { if (i <= 1) next = i; else { next = first + second; first = second; second = next; } printf("%d ", next); } return 0; } |
🧠 Explanation:
This program generates the Fibonacci sequence using iteration. It starts with 0 and 1, and then each new number is the sum of the previous two.
Example:
Fibonacci(5) = 0 1 1 2 3
🔁 Q5: Check if a number is a palindrome.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <stdio.h> int main() { int num, reversed = 0, remainder, original; printf("Enter a number: "); scanf("%d", &num); original = num; while (num != 0) { remainder = num % 10; reversed = reversed * 10 + remainder; num = num / 10; } if (original == reversed) printf("%d is a palindrome.\n", original); else printf("%d is not a palindrome.\n", original); return 0; } |
🧠 Explanation:
This program checks whether a given number is a palindrome or not. A palindrome number reads the same backward as forward (e.g., 121, 1331). The logic involves:
-
Storing the original number,
-
Reversing the number using modulo and division,
-
Then comparing the reversed number with the original.
If both match, it’s a palindrome.
🔣 Q6: Write a C program to reverse a string using a loop.
Explanation:
This program swaps characters from the beginning and end of the string until it reaches the middle, thus reversing the string. We use strlen()
to get the length and gets()
to read input (note: use fgets()
in real applications for safety).
🔡 Q7: Convert a string from lowercase to uppercase without using strupr()
.
Explanation:
In the ASCII table, the difference between lowercase and uppercase characters is 32. By subtracting 32 from a lowercase character, we convert it to uppercase.
📝 Q8: Write a program to count the number of vowels in a string.
Explanation:
We loop through each character and check if it’s a vowel (both lowercase and uppercase). We increment the counter whenever we find one.
🔄 Q9: Check if a string is a palindrome.
Explanation:
A palindrome is a string that reads the same forward and backwards. We reverse the string and compare it with the original using strcmp()
.
🔠 Q10: Remove all white spaces from a string.
Explanation:
We loop through the original string and copy only non-space characters into a new string. This effectively removes all white spaces.
🔢 Q11: Write a C program to find the largest element in an array.
Explanation:
We initialise the max
variable with the first element and then iterate through the array to compare and update the maximum value found.
🔢 Q12: Find the sum of all elements in an array.
Explanation:
As we take input, we add each element directly to the sum
variable to calculate the total in a single loop.
🔁 Q13: Write a C program to reverse the elements of an array.
Explanation:
We use the two-pointer approach to swap elements from both ends toward the center, effectively reversing the array.
➗ Q14: Find the average of elements in an array.
Explanation:
We accumulate the sum and divide it by the total number of elements to find the average. %.2f
is used to display the result up to two decimal places.
Also Read,
50 most common Python Interview Questions and Answers |
🔢 Q15: Add two matrices and print the result.
Explanation:
This program takes input for two matrices and calculates the sum of corresponding elements. The result is stored and displayed in a third matrix.
🧮 Q16: Multiply two matrices and display the result
Explanation:
Matrix multiplication is valid only when the number of columns in the first matrix equals the number of rows in the second. We use three nested loops for the multiplication and store the result in a new matrix.
📉 Q17: Sort an array using Bubble Sort
Explanation:
Bubble Sort repeatedly swaps adjacent elements if they are in the wrong order. It’s a simple algorithm and good for understanding sorting basics.
🔍 Q18: Search for an element in an array using Linear Search
Explanation:
Linear Search checks each element of the array sequentially. It’s simple but inefficient for large datasets.
🔎 Q19: Search for an element using Binary Search
Explanation:
Binary Search is efficient but requires a sorted array. It repeatedly divides the search space in half to find the element.
🚀 Q20: Merge two arrays into a third array
Explanation:
This code merges two arrays by copying elements of both into a third array sequentially. This is useful for data combination tasks.
🔤 Q21: Reverse a string without using library functions
Explanation:
We reverse the string by swapping the first character with the last, second with the second last, and so on, until the midpoint is reached. This avoids using strrev()
which is non-standard in some compilers.
🧮 Q22: Count the number of vowels, consonants, digits, and spaces in a string
Explanation:
We check each character using character handling functions from <ctype.h>
. This teaches students how to classify characters and understand input parsing.
🔁 Q23: Check if a number is a palindrome
Explanation:
A number is a palindrome if it reads the same forward and backwards. This logic builds confidence in working with digits and conditionals.
🎯 Q24: Find the LCM (Least Common Multiple) of two numbers
Explanation:
This brute-force approach finds the smallest number divisible by both inputs. It’s simple and intuitive for beginners.
📉 Q25: Find the GCD (Greatest Common Divisor) using the Euclidean Algorithm
Explanation:
This is a classic use of recursion and helps students understand divide-and-conquer techniques. It’s efficient and widely used.
🔄 Q26: Swap two numbers using pointers
Explanation:
This demonstrates how pointers allow us to directly modify values in memory. Students learn call-by-reference, which is crucial for working with functions in C.
📌 Q27: Find the length of a string using pointers
Explanation:
Instead of using strlen
This code counts characters using a pointer. It teaches students how to navigate arrays through pointer arithmetic.
📂 Q28: Write a program to copy the contents of one file to another
Explanation:
Students get hands-on experience with file handling. This task strengthens their understanding of file pointers and the importance of EOF
.
🗃️ Q29: Define a structure for student data and print it
Explanation:
Structures allow the grouping of related data. This is a great way to introduce basic data modelling and memory layout in C.
🧮 Q30: Calculate the sum of the digits of a number using recursion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <stdio.h> int sumOfDigits(int num) { if (num == 0) return 0; return (num % 10) + sumOfDigits(num / 10); } int main() { int n; printf("Enter a number: "); scanf("%d", &n); printf("Sum of digits = %d\n", sumOfDigits(n)); return 0; } |
Explanation:
This program uses recursion to repeatedly extract and sum the digits of a number. It strengthens logic-building and understanding of recursive functions.
This blog will teach you from basic-level coding questions and answers to advanced-level.
📤 Stay Updated with NextGen Careers Hub
📱 Follow us on Instagram
📺 Subscribe us on YouTube
Please share our website with others: NextGenCareersHub.in
Comments are closed.