# Top 15 C Programs for Interview with Explanations

Are you ready to take your C programming skills to the next level? Look no further! Here we provided the most asked C interview programs with explanations to help you prepare for your next interview.

Whether you’re a beginner or an experienced programmer, our collection of C programs covers everything from basic syntax to complex algorithms. Impress your interviewer with your knowledge and confidence, and show them that you’re the right candidate for the job.

## C Programs for Interview with Explanations

Go through the listed C programs now and get ready to ace your next C interview.

### 1. C code to give Fibonacci series

``````#include <stdio.h>
int fibonacci(int n)
{
if (n <= 1)
return n;
return fibonacci(n-1) + fibonacci(n-2);
}
int main()
{
int n, i;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci series: ");
for (i = 0; i < n; i++)
printf("%d ", fibonacci(i));
return 0;
}``````
``````Input:
Enter the number of terms: 4

Output:
Fibonacci series: 0 1 1 2``````

Explanation:

Fibonacci series is a series in which the nth term is generated by taking the sum of n-1th term and n-2th term here we do that we create a separate recursive function where if n<=1 it returns 1 as Fibonacci of 1 is 1 after that a recursive function is called to get the Fibonacci series.

### 2. C program to check for prime number?

``````#include <stdio.h>
#include <stdbool.h>
bool is_prime(int n)
{
int i;
for (i = 2; i <= n/2; i++)
{
if (n % i == 0)
return false;
}
return true;
}
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (is_prime(n))
printf("%d is a prime number.\n", n);
else
printf("%d is not a prime number.\n", n);
return 0;
}``````
``````Input:
Enter a positive integer: 5

Output:
5 is a prime number.``````

Explanation:

So here to check for a prime number we just need to check if the given number has any factor except 1 and the number itself so in the function is_prime we run a loop from 2 to n/2 and we check if n is divisible by any number in the range if it is we return false else we return true. If the number is 1 or 0 we return false anyway.

### 3. C program to give factorial of a number?

``````#include <stdio.h>
int factorial(int n)
{
if (n == 0)
return 1;
return n * factorial(n-1);
}
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %d\n", n, factorial(n));
return 0;
}``````
``````Input:
Enter a positive integer: 5

Output:
Factorial of 5 = 120``````

Explanation:

The factorial function calculates the factorial of a given number n. The function takes an integer argument n and returns an integer result. The function starts with an if statement that checks if n is equal to 0. If it is, then the function returns 1, which is the factorial of 0.

If n is not equal to 0, the function returns the result of n * factorial(n-1). This is because the factorial of a positive integer n is n * (n-1) * (n-2) * … * 2 * 1. The function makes a recursive call to itself, passing n-1 as the argument.

This recursive call continues until n reaches 0, at which point the function returns 1. The result of each recursive call is then multiplied by n to get the final result.

### 4. C code to check for palindrome?

``````#include <stdio.h>
#include <string.h>
bool is_palindrome(const char *str)
{
int length = strlen(str);
for (int i = 0; i < length/2; i++)
{
if (str[i] != str[length-i-1])
return false;
}
return true;
}
int main()
{
char str;
printf("Enter a string: ");
scanf("%s", str);
if (is_palindrome(str))
printf("%s is a palindrome.\n", str);
else
printf("%s is not a palindrome.\n", str);
return 0;
}``````
``````Input:
Enter a string: abcddcba

Output:
abcddcba is a palindrome``````

Explanation:

The is_palindrome function checks if a given string str is a palindrome or not. It takes a string argument str and returns a boolean result. The function starts by computing the length of the string using the strlen function and storing it in the variable length. Then, the function has a for loop that runs length/2 times.

In each iteration, it checks if the characters at the ith position and length-i-1th position are equal. If they are not equal, the function returns false, indicating that the string is not a palindrome.

If the loop completes without returning false, the function returns true, indicating that the string is a palindrome.

### 5. C program to reverse a string?

``````#include <stdio.h>
#include <string.h>
void reverse_string(char *str)
{
int length = strlen(str);
for (int i = 0; i < length/2; i++)
{
char temp = str[i];
str[i] = str[length-i-1];
str[length-i-1] = temp;
}
}

int main()
{
char str;
printf("Enter a string: ");
scanf("%s", str);
reverse_string(str);
printf("Reversed string: %s\n", str);
return 0;
}``````
``````Input:
Enter a string: abcdbcd

Output:
Reversed string: dcbdcba``````

Explanation:

The reverse_string function takes a string argument str and reverses it in place. The function does not return anything, so it’s declared with the void return type. The function starts by computing the length of the string using the strlen function and storing it in the variable length.

Then, the function has a for loop that runs length/2 times. In each iteration, it swaps the characters at the ith position and length-i-1th position. This effectively reverses the string.

### 6. C program to swap two numbers?

``````#include <stdio.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int a, b;
printf("Enter two numbers: ");
scanf("%d%d", &a, &b);

swap(&a, &b);
printf("Swapped numbers: %d %d\n", a, b);
return 0;
}``````
``````Input:
Enter two numbers: 2 4

Output:
Swapped numbers: 4 2``````

Explanation:

The swap function takes two pointers to int, a and b, and swaps the values they point to. The function does not return anything, so it’s declared with the void return type.

The function starts by storing the value pointed to by a temporary variable temp. Then it changes the value pointed to by a to be the value pointed to by b, and finally changes the value pointed to by b to be the original value stored in temp. This effectively swaps the values of a and b.

### 7. C program for linear search?

``````#include <stdio.h>
int linear_search(int arr[], int n, int x)
{
int i;
for (i = 0; i < n; i++)
{
if (arr[i] == x)
{
return i;
}
}
return -1;
}

int main()
{
int arr[] = {10, 20, 80, 30, 60, 50, 110, 100, 130, 170};
int n = sizeof(arr) / sizeof(arr);
int x = 110;
int index = linear_search(arr, n, x);
if (index == -1)
{
}
else
{
printf("Element found at index %d\n", index);
}
return 0;
}``````
``````Output:
Element found at index 6``````

Explanation:

The linear_search function takes an array arr, the size of the array n, and the value to be searched x as arguments. The function returns the index of the first occurrence of x in the array, or -1 if x is not found in the array.

The function starts by initializing a loop variable i to 0. Then it iterates over the elements of the array using a for loop, and checks if the current element arr[i] is equal to x. If it is, the function returns i (the index of the first occurrence of x). If the end of the array is reached without finding x, the function returns -1.

### 8. C program for binary search?

``````#include <stdio.h>
int binary_search(int arr[], int l, int r, int x)
{
while (l <= r)
{
int m = l + (r - l) / 2;
if (arr[m] == x)
{
return m;
}
if (arr[m] < x)
{
l = m + 1;
}
else
{
r = m - 1;
}
}
return -1;
}

int main()
{
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr);
int x = 30;
int result = binary_search(arr, 0, n - 1, x);
if (result == -1)
{
}
else
{
printf("Element found at index %d\n", result);
}
return 0;
}``````
``````Output:
Element found at index 2``````

Explanation:

The binary_search function takes an array arr, the left index l, the right index r, and the value to be searched x as arguments. The function returns the index of the first occurrence of x in the array, or -1 if x is not found in the array.

The function starts by using a while loop to iterate until l is greater than r. Within the loop, it calculates the middle index m by adding l and (r – l) / 2. Then it checks if the middle element arr[m] is equal to x. If it is, the function returns m (the index of the first occurrence of x).

If arr[m] is less than x, it means that x could only exist in the right half of the array, so l is updated to m + 1. If arr[m] is greater than x, it means that x could only exist in the left half of the array, so r is updated to m – 1.

### 9. C program for gcd calculation?

``````#include <stdio.h>
int gcd(int a, int b)
{
if (b == 0)
{
return a;
}
return gcd(b, a % b);
}
int main()
{
int a = 60;
int b = 48;
int result = gcd(a, b);
printf("The GCD of %d and %d is %d\n", a, b, result);
return 0;
}``````
``````Output:
The GCD of 60 and 48 is 12``````

Explanation:

The gcd function calculates the greatest common divisor (GCD) of two numbers a and b. The function uses a recursive approach based on the Euclidean algorithm.

The function starts by checking if b is equal to 0. If it is, the function returns a as the GCD. If b is not 0, the function returns a recursive call to gcd with the arguments b and a % b.

The value a % b calculates the remainder when a is divided by b. The idea behind this is that the GCD of a and b is the same as the GCD of b and a % b.

### 10. C program for lcm calculation?

``````#include <stdio.h>
int gcd(int a, int b)
{
if (b == 0)
{
return a;
}
return gcd(b, a % b);
}
int lcm(int a, int b)
{
return (a * b) / gcd(a, b);
}
int main()
{
int a = 15;
int b = 20;
int result = lcm(a, b);
printf("The LCM of %d and %d is %d\n", a, b, result);
return 0;
}``````
``````Output:
The LCM of 15 and 20 is 60``````

Explanation:

This is the code for the gcd function, which calculates the greatest common divisor of two numbers using the Euclidean algorithm (as explained in the previous answer).

The lcm function calculates the least common multiple of two numbers a and b. The LCM is calculated by multiplying a and b and dividing the result by the GCD of a and b. The GCD of a and b is calculated by calling the gcd function.

### 11. C program to check for Armstrong number?

``````#include <stdio.h>
#include <math.h>
int get_number_of_digits(int n)
{
int count = 0;
while (n != 0)
{
count++;
n /= 10;
}
return count;
}
int is_armstrong_number(int n)
{
int num_digits = get_number_of_digits(n);
int sum = 0;
int temp = n;
while (temp != 0)
{
int digit = temp % 10;
sum += pow(digit, num_digits);
temp /= 10;
}
return sum == n;
}
int main()
{
int n = 153;
if (is_armstrong_number(n))
{
printf("%d is an Armstrong number\n", n);
}
else
{
printf("%d is not an Armstrong number\n", n);
}
return 0;
}``````
``````Output:
153 is an Armstrong number``````

Explanation:

The get_number_of_digits function calculates the number of digits in a given integer n. It uses a loop to repeatedly divide n by 10 until n becomes 0, counting the number of divisions.

The is_armstrong_number function checks if a given integer n is an Armstrong number. It first calculates the number of digits in n by calling the get_number_of_digits function.

Then it uses a loop to sum the cubes of the digits in n. If the sum is equal to n, the function returns 1, indicating that n is an Armstrong number.

### 12. C program for bubble sort ?

``````#include <stdio.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void bubble_sort(int arr[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
swap(&arr[j], &arr[j + 1]);
}
}
}
}

int main()
{
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr);
bubble_sort(arr, n);
printf("Sorted array: \n");
int i;
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}``````
``````Output:
Sorted array:
11 12 22 25 34 64 90``````

Explanation:

The swap function takes two integer pointers as arguments and swaps the values at their respective locations. The bubble_sort function sorts an array of integers in ascending order using the bubble sort algorithm. The outer loop runs n-1 times, where n is the number of elements in the array.

The inner loop compares adjacent elements and swaps them if the element on the left is greater than the element on the right. This loop runs n-i-1 times, where i is the current iteration of the outer loop.

This reduces the number of elements that need to be compared in each iteration, as the largest element “bubbles up” to the end of the array after each iteration.

### 13. C program to print pattern?

``````#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;
}``````
``````Input:
Enter the number of rows: 5
Output:
*
* *
* * *
* * * *
* * * * *``````

Explanation:

This section prompts the user to enter the number of rows for the pattern. The input is stored in the rows variable. This section uses nested loops to print the pattern. The outer loop runs rows times, where rows is the number of rows entered by the user.

The inner loop runs i times, where i is the current iteration of the outer loop. This means that the inner loop will print i asterisks in each row, with each subsequent row having one more asterisk than the previous row. The printf(“\n”) statement is used to print a new line after each row.

### 14. C program for insertion sort?

``````#include <stdio.h>
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int arr[] = { 12, 11, 13, 5, 6 };
int n = sizeof(arr) / sizeof(arr);

printf("Unsorted array: \n");
printArray(arr, n);

insertionSort(arr, n);

printf("Sorted array: \n");
printArray(arr, n);

return 0;
}``````
``````Input:
Unsorted array:
12 11 13 5 6

Output:
Sorted array:
5 6 11 12 13``````

Explanation:

This section implements the insertion sort algorithm. The function takes an array arr and its size n as arguments. The outer loop iterates n-1 times, where n is the number of elements in the array. In each iteration, the current element is stored in the key variable.

The inner loop shifts all elements that are greater than key to the right, until an element smaller than key is found. Finally, the key is inserted into its correct position in the array.

This function takes an array and its size as arguments, and prints all elements of the array.

### 15. C program for matrix multiplication?

``````#include <stdio.h>
void multiply(int a[], int b[], int c[], int m, int n, int p)
{
int i, j, k;
for (i = 0; i < m; i++)
{
for (j = 0; j < p; j++)
{
c[i][j] = 0;
for (k = 0; k < n; k++)
c[i][j] += a[i][k] * b[k][j];
}
}
}

void printMatrix(int c[], int m, int p)
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < p; j++)
printf("%d ", c[i][j]);
printf("\n");
}
}

int main()
{
int a, b, c;
int m, n, p;

printf("Enter the number of rows and columns of first matrix: ");
scanf("%d%d", &m, &n);

printf("Enter the elements of first matrix: \n");
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &a[i][j]);

printf("Enter the number of rows and columns of second matrix: ");
scanf("%d%d", &n, &p);

printf("Enter the elements of second matrix: \n");
for (i = 0; i < n; i++)
for (j = 0; j < p; j++)
scanf("%d", &b[i][j]);

multiply(a, b, c, m, n, p);

printf("Result matrix: \n");
printMatrix(c, m, p);

return 0;
}``````
``````Input:
Enter the number of rows and columns of first matrix: 2
2
Enter the elements of first matrix:
1
2
3
4
Enter the number of rows and columns of second matrix: 1
2
Enter the elements of second matrix:
3
3

Output:
Result matrix:
3 3
9 9``````

Explanation:

This section implements the matrix multiplication algorithm. The function takes three matrices a, b, and c and their dimensions m, n, and p as arguments.

The outer two loops iterate over all rows and columns of the result matrix c. The inner loop multiplies the elements of a row of the first matrix a with the elements.