C Programming Interview Questions with Examples and Outputs (Part 3 for Freshers)
If you’re preparing for C language interviews, knowing theory is not enough. Interviewers love to test how you think in code — by asking small programs or tricky output-based questions.
This post will help you practice the most commonly asked C programs and their outputs, all explained in simple and clear words.
1. Program to check if a number is even or odd
Code:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("Even number");
else
printf("Odd number");
return 0;
}
Explanation:
If the remainder after dividing a number by 2 is zero, the number is even; otherwise, it’s odd.
Output:
Enter a number: 5
Odd number
2. Program to find the largest number among three numbers
Code:
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if(a > b && a > c)
printf("%d is largest", a);
else if(b > a && b > c)
printf("%d is largest", b);
else
printf("%d is largest", c);
return 0;
}
Output Example:
Enter three numbers: 10 25 5
25 is largest
3. Program to calculate factorial of a number
Code:
#include <stdio.h>
int main() {
int n, i;
long fact = 1;
printf("Enter a number: ");
scanf("%d", &n);
for(i = 1; i <= n; i++) {
fact = fact * i;
}
printf("Factorial = %ld", fact);
return 0;
}
Output:
Enter a number: 5
Factorial = 120
Explanation:
The factorial of 5 is 5×4×3×2×1 = 120.
4. Program to check if a number is prime
Code:
#include <stdio.h>
int main() {
int n, i, count = 0;
printf("Enter a number: ");
scanf("%d", &n);
for(i = 2; i <= n/2; i++) {
if(n % i == 0) {
count = 1;
break;
}
}
if(n == 1)
printf("1 is neither prime nor composite");
else if(count == 0)
printf("%d is a prime number", n);
else
printf("%d is not a prime number", n);
return 0;
}
Output:
Enter a number: 7
7 is a prime number
5. Program to reverse a number
Code:
#include <stdio.h>
int main() {
int n, rev = 0, rem;
printf("Enter a number: ");
scanf("%d", &n);
while(n != 0) {
rem = n % 10;
rev = rev * 10 + rem;
n = n / 10;
}
printf("Reversed number: %d", rev);
return 0;
}
Example Output:
Enter a number: 1234
Reversed number: 4321
6. Program to check palindrome number
Code:
#include <stdio.h>
int main() {
int n, original, rev = 0, rem;
printf("Enter a number: ");
scanf("%d", &n);
original = n;
while(n != 0) {
rem = n % 10;
rev = rev * 10 + rem;
n /= 10;
}
if(original == rev)
printf("Palindrome number");
else
printf("Not a palindrome");
return 0;
}
Output Example:
Enter a number: 121
Palindrome number
7. Program to print Fibonacci series
Code:
#include <stdio.h>
int main() {
int n, a = 0, b = 1, c, i;
printf("Enter number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: %d %d ", a, b);
for(i = 2; i < n; i++) {
c = a + b;
printf("%d ", c);
a = b;
b = c;
}
return 0;
}
Output Example:
Enter number of terms: 6
Fibonacci Series: 0 1 1 2 3 5
8. Output-based question (Tricky)
Code:
#include <stdio.h>
int main() {
int x = 5;
printf("%d %d %d", x, x++, ++x);
return 0;
}
Output:
⚠️ Output is compiler-dependent (undefined behavior).
It may print different values in different compilers because modifying a variable multiple times in the same expression is undefined in C.
Interview Tip:
Never modify and use the same variable in one statement like this. It’s tricky and unsafe.
9. Program to swap two numbers without using a third variable
Code:
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
a = a + b;
b = a - b;
a = a - b;
printf("After swapping: a = %d, b = %d", a, b);
return 0;
}
Output Example:
Enter two numbers: 5 10
After swapping: a = 10, b = 5
10. Program to count digits in a number
Code:
#include <stdio.h>
int main() {
int n, count = 0;
printf("Enter a number: ");
scanf("%d", &n);
while(n != 0) {
n = n / 10;
count++;
}
printf("Total digits: %d", count);
return 0;
}
Output:
Enter a number: 98765
Total digits: 5
11. Program to check Armstrong number
Code:
#include <stdio.h>
#include <math.h>
int main() {
int n, original, rem, result = 0, digits = 0, temp;
printf("Enter a number: ");
scanf("%d", &n);
original = n;
temp = n;
while(temp != 0) {
temp /= 10;
digits++;
}
temp = n;
while(temp != 0) {
rem = temp % 10;
result += pow(rem, digits);
temp /= 10;
}
if(result == original)
printf("Armstrong number");
else
printf("Not Armstrong");
return 0;
}
Example Output:
Enter a number: 153
Armstrong number
12. Output-based tricky question
Code:
#include <stdio.h>
int main() {
int a = 10;
printf("%d %d %d", a, ++a, a++);
return 0;
}
Output:
⚠️ Undefined behavior — could print 12 12 10, 11 11 10, or something else depending on compiler.
Always avoid modifying a multiple times in the same statement.
13. Program to find sum of digits
Code:
#include <stdio.h>
int main() {
int n, sum = 0, rem;
printf("Enter a number: ");
scanf("%d", &n);
while(n != 0) {
rem = n % 10;
sum += rem;
n /= 10;
}
printf("Sum of digits: %d", sum);
return 0;
}
Output Example:
Enter a number: 123
Sum of digits: 6
14. Program to check leap year
Code:
#include <stdio.h>
int main() {
int year;
printf("Enter year: ");
scanf("%d", &year);
if((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
printf("%d is a leap year", year);
else
printf("%d is not a leap year", year);
return 0;
}
Example Output:
Enter year: 2024
2024 is a leap year
15. Output-based Question (Strings)
Code:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
strcat(str1, str2);
printf("%s", str1);
return 0;
}
Output:
HelloWorld
Explanation:strcat() joins two strings — here “Hello” and “World”.
💡 Pro Tip for Interviews
When you get a C programming question:
- Think logically first, don’t jump to code.
- Use loops and conditionals smartly.
- Always initialize variables properly.
- Try to predict the output before running your program — interviewers love that skill.
Conclusion
By practicing these C programs and understanding their logic, you can answer both coding and output-based interview questions confidently.
This post covered everything from basic programs to tricky questions that test your attention to detail.
If you want to get better, type and run each code in your compiler — that’s the best way to learn.
Stay tuned for Part 4, where we’ll cover Advanced C Programming Concepts and Real Coding Challenges that are often asked in developer job interviews.