C Programming Coding Questions
Practice most asked C programming coding interview questions with solutions.
1. Write a program to find factorial of a number
EasyInput: 5 Output: 120
#include <stdio.h>
int main() {
int n, fact = 1;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
fact *= i;
printf("%d", fact);
return 0;
}
2. Check whether a number is prime
EasyInput: 3 Output: Prime
#include <stdio.h>
int main() {
int n, flag = 1;
scanf("%d", &n);
if(n <= 1) flag = 0;
for(int i = 2; i <= n/2; i++) {
if(n % i == 0) {
flag = 0;
break;
}
}
printf(flag ? "Prime" : "Not Prime");
return 0;
}
3. Find Fibonacci series
EasyInput: 7 Output: 0 1 1 2 3 5 8
#include <stdio.h>
int main() {
int n, a = 0, b = 1, c;
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
printf("%d ", a);
c = a + b;
a = b;
b = c;
}
return 0;
}
4. Find largest of three numbers
EasyInput: 10 20 30 Output: 30
#include <stdio.h>
int main() {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if(a > b && a > c)
printf("%d", a);
else if(b > c)
printf("%d", b);
else
printf("%d", c);
return 0;
}
5. Reverse a string
EasyInput: HelloWorld Output: dlroWolleH
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
gets(str);
for(int i = strlen(str) - 1; i >= 0; i--)
printf("%c", str[i]);
return 0;
}
6. Count digits of a number
EasyInput: 1451 Output: 4
#include <stdio.h>
int main() {
int n, count = 0;
scanf("%d", &n);
while(n != 0) {
count++;
n /= 10;
}
printf("%d", count);
return 0;
}
7. Check Armstrong number
MediumInput: 153 Output: Armstrong
#include <stdio.h>
#include <math.h>
int main() {
int n, temp, sum = 0, digits = 0;
scanf("%d", &n);
temp = n;
while(temp) {
digits++;
temp /= 10;
}
temp = n;
while(temp) {
sum += pow(temp % 10, digits);
temp /= 10;
}
printf(sum == n ? "Armstrong" : "Not Armstrong");
return 0;
}
8. Find sum of array elements
Easy
Input: 5
10 20 30 40 50
Output: 150
#include <stdio.h>
int main() {
int n, sum = 0;
scanf("%d", &n);
int arr[n];
for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("%d", sum);
return 0;
}
9. Linear search
Easy
Input: 5
10 20 30 40 50
30
Output: Found
#include <stdio.h>
int main() {
int n, key, found = 0;
scanf("%d", &n);
int arr[n];
for(int i = 0; i < n; i++)
scanf("%d", &arr[i]);
scanf("%d", &key);
for(int i = 0; i < n; i++) {
if(arr[i] == key) {
found = 1;
break;
}
}
printf(found ? "Found" : "Not Found");
return 0;
}
10. Swap two numbers without third variable
MediumInput: 10 20 Output: 20 10
#include <stdio.h>
int main() {
int a, b;
scanf("%d%d", &a, &b);
a = a + b;
b = a - b;
a = a - b;
printf("%d %d", a, b);
return 0;
}
11. Find second largest element in an array
Medium
Input: 6
10 45 20 8 45 30
Output: 30
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int a[n], max, second;
for(int i=0;i max){
second = max;
max = a[i];
} else if(a[i] > second && a[i] != max){
second = a[i];
}
}
printf("%d", second);
return 0;
}
12. Check if a string is palindrome
MediumInput: madam Output: Palindrome
#include <stdio.h>
#include <string.h>
int main() {
char s[100];
gets(s);
int l=0, r=strlen(s)-1;
while(l
13. Remove duplicate elements from array
Medium
Input: 8
1 2 3 2 4 1 5 3
Output: 1 2 3 4 5
#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i
14. Count frequency of characters in string
Medium
Input: hello
Output: h = 1
e = 1
l = 2
o = 1
#include <stdio.h>
int main(){
char s[100];
gets(s);
int freq[256]={0};
for(int i=0;s[i];i++)
freq[s[i]]++;
for(int i=0;i<256;i++)
if(freq[i])
printf("%c = %d\n", i, freq[i]);
return 0;
}
15. Binary search
Medium
Input: 7
2 5 8 12 16 23 38
16
Output: Found
#include <stdio.h>
int main(){
int n,key;
scanf("%d",&n);
int a[n];
for(int i=0;i
16. Implement stack using array
HardOutput: 10
#include <stdio.h>
#define SIZE 5
int stack[SIZE], top=-1;
void push(int x){
if(top==SIZE-1) return;
stack[++top]=x;
}
void pop(){
if(top==-1) return;
top--;
}
int main(){
push(10); push(20);
pop();
for(int i=0;i<=top;i++)
printf("%d ",stack[i]);
}
17. Implement queue using array
HardOutput: 2
#include <stdio.h>
#define SIZE 5
int q[SIZE], front=0, rear=-1;
void enqueue(int x){
if(rear==SIZE-1) return;
q[++rear]=x;
}
void dequeue(){
if(front>rear) return;
front++;
}
int main(){
enqueue(1); enqueue(2);
dequeue();
for(int i=front;i<=rear;i++)
printf("%d ",q[i]);
}
18. Reverse linked list
HardInput: 10 20 30 Output: 30 20 10
struct Node{
int data;
struct Node* next;
};
struct Node* reverse(struct Node* head){
struct Node *prev=NULL,*curr=head,*next=NULL;
while(curr){
next=curr->next;
curr->next=prev;
prev=curr;
curr=next;
}
return prev;
}
19. Check balanced parentheses
HardInput: ((a+b)*(c+d)) Output: Balanced
#include <stdio.h>
#include <string.h>
int main(){
char s[100];
int count=0;
gets(s);
for(int i=0;s[i];i++){
if(s[i]=='(') count++;
if(s[i]==')') count--;
if(count<0){
printf("Unbalanced");
return 0;
}
}
printf(count==0 ? "Balanced" : "Unbalanced");
}
20. Find missing number in array
Hard
Input: 5
1 2 3 5
Output: 4
#include <stdio.h>
int main(){
int n,sum=0;
scanf("%d",&n);
int a[n];
for(int i=0;i
21. Write a program to reverse a number
EasyProblem: Reverse a given integer number.
Example:
Input: 1234 Output: 4321
#include <stdio.h>
int main() {
int n, rev = 0;
scanf("%d", &n);
while(n != 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
printf("%d", rev);
return 0;
}
22. Check whether a number is palindrome
EasyInput: 121 Output: Palindrome
#include <stdio.h>
int main() {
int n, temp, rev = 0;
scanf("%d", &n);
temp = n;
while(n != 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
if(temp == rev)
printf("Palindrome");
else
printf("Not Palindrome");
return 0;
}