Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Coding Interview Questions In C

Top Coding Interview Questions In C

“Coding Interview Questions in C” is available here on this page.

Coding Questions are considered as one of the toughest questions in the Interview. You must be prepared by about these types of questions as they are often asked in the Interview.

Don’t worry PrepInsta is here to help you and to provide you all the updated information about Coding Interview Questions in C.

Along with that we will also suggest you to prepare Top 100 coding questions in. You can click on the button given below to practice Top 100 coding questions in your preferable language.

coding interview questions in c

Learn c : Introduction

C is a common high-level programming language among programmers because it is the most compatible, simple to understand, and powerful programming language. It is the successor to the B programming language, which first appeared in the 1970s and has been the most popular programming language ever since.

Features of C programming:-

  • C is a Middle level language. 
  • It is case sensitive.
  • It is easy to extend.
  • Highly portable.
  • It is fast and efficient.
  • It is more widely used language in operating systems and embedded system.
  • It is the simple language.

Commonly Asked Coding Interview Questions In C

1.Display Prime Numbers Between Two Intervals.

Solution:-

#include <stdio.h>

int main() {
   int low, high, i, flag;
   printf("Enter two numbers(intervals): ");
   scanf("%d %d", &low, &high);
   printf("Prime numbers between %d and %d are: ", low, high);

   // iteration until low is not equal to high
   while (low < high) {
      flag = 0;

      // ignore numbers less than 2
      if (low <= 1) {
         ++low;
         continue;
      }

      // if low is a non-prime number, flag will be 1
      for (i = 2; i <= low / 2; ++i) {

         if (low % i == 0) {
            flag = 1;
            break;
         }
      }

      if (flag == 0)
         printf("%d ", low);

      // to check prime for the next number
      // increase low by 1
      ++low;
   }

OUTPUT

Enter two numbers(intervals): 20
50
Prime numbers between 20 and 50 are: 23 29 31 37 41 43 47

2.Write a C Program to Print Pyramids and Patterns.

*
* *
* * *
* * * *
* * * * *
Solution:-
#include <stdio.h>

int main() {

int i, j, rows;

printf("Enter the number of rows: ");

scanf("%d", &rows);

for (i = 1; i <= rows; ++i) {

for (j = 1; j <= i; ++j) {

printf("* ");

}

printf("\n");

}
return 0;

}





3.Write a C Program to Find the Sum of Natural Numbers using Recursion.

Solution:-
#include
Numbers(int n);

int main() {

int num;

printf("Enter a positive integer: ");

scanf("%d", &num);

printf("Sum = %d", addNumbers(num));

return 0;

}

int addNumbers(int n) {

if (n != 0)

return n + addNumbers(n - 1);

else

return n;

}

OUTPUT

Enter a positive integer: 20
Sum = 210

4.Write a C Program to Find Largest Element in an Array.

Solution:-
#include<stdio.h>
int main() {
int i, n;
float arr[100];
printf("Enter the number of elements (1 to 100): ");
scanf("%d", &n);

for (i = 0; i < n; ++i) {
printf("Enter number%d: ", i + 1);
scanf("%f", &arr[i]);
}

// storing the largest number to arr[0]
for (i = 1; i < n; ++i) {
if (arr[0] < arr[i])
arr[0] = arr[i];
}

printf("Largest element = %.2f", arr[0]);

return 0;
}

OUTPUT

Enter the number of elements (1 to 100): 5
Enter number1: 34.5
Enter number2: 2.4
Enter number3: -35.5
Enter number4: 38.7
Enter number5: 24.5
Largest element = 38.70

5.Write a C Program to Add Two Matrices Using Multi-dimensional Arrays

Solution:-

#include<stdio.h> 
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);

printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}

printf("Enter elements of 2nd matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}

// adding two matrices
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}

// printing the result
printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}
return 0;
}

OUTPUT

Enter the number of rows (between 1 and 100): 2
Enter the number of columns (between 1 and 100): 3

Enter elements of 1st matrix:
Enter element a11 : 2
Enter element a12 : 3
Enter element a13 : 4
Enter element a21 : 5
Enter element a22 : 2
Enter element a23  : 3
Enter elements of 2nd matrix:
Enter element a11 : -4
Enter element a12 : 5
Enter element a13 : 3
Enter element a21 : 5
Enter element a22 : 6
Enter element a23 : 3

Sum of two matrices:
-2    8     7

10    8     6

6.Write a Program Remove Characters in String Except Alphabets.

Solution:-

#include 
int main() {
char line[150];

printf("Enter a string: ");
fgets(line, sizeof(line), stdin); // take input


for (int i = 0, j; line[i] != '\0'; ++i) {

// enter the loop if the character is not an alphabet
// and not the null character
while (!(line[i] >= 'a' && line[i] <= 'z') && !(line[i] >= 'A' && line[i] <= 'Z') && !(line[i] == '\0')) {
for (j = i; line[j] != '\0'; ++j) {

// if jth element of line is not an alphabet,
// assign the value of (j+1)th element to the jth element
line[j] = line[j + 1];
}
line[j] = '\0';
}
}
printf("Output String: ");
puts(line);
return 0;
}

OUTPUT

Enter a string: p2’[email protected]/
Output String: programiz

7. Write a C Program to Find the Length of a String.

Solution:-

#include 
int main() {
char s[] = "Programming is fun";
int i;

for (i = 0; s[i] != '\0'; ++i);

printf("Length of the string: %d", i);
return 0;
}

OUTPUT

Length of the string: 18

8. Write a C Program to Calculate Difference Between Two Time Periods

Solution:-

#include 
struct TIME {
int seconds;
int minutes;
int hours;
};

void differenceBetweenTimePeriod(struct TIME t1,
struct TIME t2,
struct TIME *diff);

int main() {
struct TIME startTime, stopTime, diff;

printf("Enter the start time. \n");
printf("Enter hours, minutes and seconds: ");
scanf("%d %d %d", &startTime.hours,
&startTime.minutes,
&startTime.seconds);

printf("Enter the stop time. \n");
printf("Enter hours, minutes and seconds: ");
scanf("%d %d %d", &stopTime.hours,
&stopTime.minutes,
&stopTime.seconds);

// Difference between start and stop time
differenceBetweenTimePeriod(startTime, stopTime, &diff);
printf("\nTime Difference: %d:%d:%d - ", startTime.hours,
startTime.minutes,
startTime.seconds);
printf("%d:%d:%d ", stopTime.hours,
stopTime.minutes,
stopTime.seconds);
printf("= %d:%d:%d\n", diff.hours,
diff.minutes,
diff.seconds);
return 0;
}

// Computes difference between time periods
void differenceBetweenTimePeriod(struct TIME start,
struct TIME stop,
struct TIME *diff) {
while (stop.seconds > start.seconds) {
--start.minutes;
start.seconds += 60;
}
diff->seconds = start.seconds - stop.seconds;
while (stop.minutes > start.minutes) {
--start.hours;
start.minutes += 60;
}
diff->minutes = start.minutes - stop.minutes;
diff->hours = start.hours - stop.hours;
}

OUTPUT

Enter the start time.
Enter hours, minutes and seconds: 12
34
55
Enter the stop time.
Enter hours, minutes and seconds: 8
12
15

Time Difference: 12:34:55 – 8:12:15 = 4:22:40

9. Write a C Program to Find Largest Number Using Dynamic Memory Allocation

Solution:-

#include 
int main() {
int num;
float *data;
printf("Enter the total number of elements: ");
scanf("%d", &num);

// Allocating memory for num elements
data = (float *)calloc(num, sizeof(float));
if (data == NULL) {
printf(“Error!!! memory not allocated.”);
exit(0);
}

// Storing numbers entered by the user.
for (int i = 0; i < num; ++i) {
printf(“Enter Number %d: “, i + 1);
scanf(“%f”, data + i);
}

// Finding the largest number
for (int i = 1; i < num; ++i) {
if (*data < *(data + i))
*data = *(data + i);
}
printf(“Largest number = %.2f”, *data);

return 0;
}

10. Write a C Program to Access Array Elements Using Pointer

Solution:-

#include <stdio.h>
int main() {
int data[5];

printf("Enter elements: ");
for (int i = 0; i < 5; ++i)
scanf("%d", data + i);

printf("You entered: \n");
for (int i = 0; i < 5; ++i)
printf("%d\n", *(data + i));
return 0;
}

OUTPUT

Enter elements: 1
2
3
5
4
You entered:
1
2
3
5
4

11. Write a C Program to Find the Transpose of a Matrix.

#include 
int main() {
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);

// Assigning elements to the matrix
printf("\nEnter matrix elements:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}

// Displaying the matrix a[][]
printf("\nEntered matrix: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}

// Finding the transpose of matrix a
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}

// Displaying the transpose of matrix a
printf("\nTranspose of the matrix:\n");
for (i = 0; i < c; ++i)
for (j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}

OUTPUT

Enter rows and columns: 2
3

Enter matrix elements:
Enter element a11: 1
Enter element a12: 4
Enter element a13: 0
Enter element a21: -5
Enter element a22: 2
Enter element a23: 7

Entered matrix:
1  4  0
-5  2  7

Transpose of the matrix:
1  -5
4  2
0  7

12. Write a Bubble Sort program.

Solution :-  
int main()
{
      int array[100], n, i, j, swap; 
      printf("Enter number of elementsn");
      scanf("%d", &n); 
      printf("Enter %d Numbers:n", n); 
      for(i = 0; i<n; i++)
           scanf("%d", &array[i]); 
           for(i = 0 ; i<n - 1; i++)
           {
                 for(j = 0 ; j < n-i-1; j++) { if(array[j]>array[j+1]) 
                       {
                               swap=array[j];
                               array[j]=array[j+1];
                               array[j+1]=swap;
                       }
                 }
           } 
           printf("Sorted Array:n"); 
           for(i = 0; i < n; i++)
                 printf("%dn", array[i]);
           return 0;
}

13. Write a C Program to Convert Binary Number to Octal and vice-versa.

Solution:-

#include<math.h>
#include<stdio.h>
int convert(long long bin);
int main() {
long long bin;
printf("Enter a binary number: ");
scanf("%lld", &bin);
printf("%lld in binary = %d in octal", bin, convert(bin));
return 0;
}

int convert(long long bin) {
int oct = 0, dec = 0, i = 0;

// converting binary to decimal
while (bin != 0) {
dec += (bin % 10) * pow(2, i);
++i;
bin /= 10;
}
i = 1;

// converting to decimal to octal
while (dec != 0) {
oct += (dec % 8) * i;
dec /= 8;
i *= 10;
}
return oct;
}

OUTPUT

Enter an octal number: 67
67 in octal = 110111 in binary

14.Write a C program to Reverse a Sentence Using Recursion

#include <stdio.h>
void reverseSentence();
int main() {
printf("Enter a sentence: ");
reverseSentence();
return 0;
}

void reverseSentence() {
char c;
scanf("%c", &c);
if (c != '\n') {
reverseSentence();
printf("%c", c);
}
}

15. Write a C Program to Make a Simple Calculator Using switch case.

Solution:-

#include 
int main() {
char operator;
double first, second;
printf("Enter an operator (+, -, *,): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);

switch (operator) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}

return 0;
}

OUTPUT
Enter an operator (+, -, *,): *
Enter two operands: 1.5
4.5
1.5 * 4.5 = 6.8

16. Write a code to generate random numbers in C Language.

Solution:- Random numbers in C Language can be generated as follows:
#include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;
int main()
{
int a,b;
for(a=1;a&lt;=10;a++)
{
b=rand();
printf("%dn",b);
}
return 0;
}
Output :
1987384758
2057844389
3475398489
2247357398
1435983905

17. Write a C program to print hello world without using a semicolon (;).

Solution:-
#include&lt;stdio.h&gt;
void main()
{
if(printf("hello world")){}
}
Output:
hello world

18. Write a program to swap two numbers without using the third variable.

Solution:-
#include&lt;stdio.h&gt;
#include&lt;conio.h&gt;
main()
{
int a=10, b=20;
clrscr();
printf("Before swapping a=%d b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("nAfter swapping a=%d b=%d",a,b);
getch();
}
Output :
Before swapping a=10 b=20
After swapping a=20 b=10

19. Write a code to print the following pattern.

1 12 123 1234 12345

Solution:-
#include<stdio.h>
int main()
{
      for(i=1;i<=5;1++)
      {
           for(j=1;j<=5;j++)
           {
                print("%d",j);
           }
           printf("n");
      }
      return 0;
}
 

20. How can you remove duplicates in an array?

Solution:-
#include &lt;stdio.h&gt;
int main()
{
int n, a[100], b[100], calc = 0, i, j,count;
printf("Enter no. of elements in array.n");
scanf("%d", &amp;n);
printf("Enter %d integersn", n);
for (i = 0; i &lt; n; i++)
scanf("%d", &amp;a[i]);
for (i = 0; i&lt;n; i++)
{
for (j = 0; j&lt;calc; j++)
{
if(a[i] == b[j])
break;
}
if (j== calc)
{
b[count] = a[i];
calc++;
}
}
printf("Array obtained after removing duplicate elementsn");
for (i = 0; i&lt;calc; i++)
{
printf("%dn", b[i]);
}
return 0;
}
Output :
Enter no. of elements in array. 5
Enter 5 integers
12
11
11
10
4
Array obtained after removing duplicate elements
12
11
10
4