×

Top 20 C Programming | Coding Interview Questions for 2024

Most Frequently asked C programming | Coding Interview Questions and answer for Fresher

C is a powerful high-level programming language.  It is a fast, portable and available for all platforms. C is called as mother language of all programming language. It is used for creating a wide range of system applications for different OS like Windows, iOS. It is highly efficient, that's why it is popular for decades. It's a good programming language for those who are new to programming. C clears your entire concept of how a computer works as well as the program. Drivers and kernels of an OS are written in c. It was developed during 1972 by Dennis Ritchie at Bell Labs. It was created for UNIX utilities. In this tutorial, I bring you some most important and asked interview questions and answers. These questions are also important for academic exams as well as competitive exams. Try to make your logic for the programs. Question 1: write a c program to print the leap year from 1 to a given number. Answer-
intleap(int y)
{
if( (y % 400==0)||(y%4==0 && y%100!=0) )
return 1;
else
return  0;
}
intmain()
{#include<stdio.h>
inti,n;
printf("Enter the value of N: ");
scanf("%d",&n);
printf("Leap years from 1 to %d:\n",n);
for(i=1;i<=n;i++)
    {
if(leap(i))
printf("%d\t",i);
    }
return 0;
}
Output-
Enter the value of N:
50

Leap years from 1 to 50:
4          8          12       16       20       24       28       32       36       40       44       48
Question 2: write a c program to read two integers A and B and swap their values. Answer-
#include <stdio.h>
void swap(float *ptr1, float  *ptr2);
void main()
{
float a, b;
printf("Enter the values of A and B \n");
scanf("%f %f", &m, &n);
printf("Before Swapping A = %5.2ftN = %5.2f\n", a, b);
swap(&m, &n);
printf("After Swapping B  = %5.2ftN = %5.2f\n", a, b);
}
void swap(float *ptr1, float *ptr2)
{
float temp;
temp = *ptr1;
    *ptr1 = *ptr2;
    *ptr2 = temp;
}
 Output-
Enter the values of A and B5 6Before Swapping= 5.00    N = 6.00After Swapping = 6.00    N = 5.00
Question 3: write a c program to compute the sum of the digit in a given number. Answer-
#include <stdio.h>
void main()
{
long n, temp, d, sum = 0;
printf("Enter the number \n");
scanf("%ld", &n);
temp = n;
while (n > 0)
    {
digit = n % 10;
sum  = sum + d;
n /= 10;
    }
printf("Given number = %ld\n", temp);
printf("Sum of the digits %ld = %ld\n", temp, sum);
}
Output-
Enter the number
755
Given number= 755
Sum of the digits 755=17
Question 4: write a c program to check whether a number is even or add. Answer- For this program, we will take the input by the user in integer format. And divide that number by 2 with modulus operator (%) if n%2=0 then the number is even otherwise the number is odd.
#include <stdio.h>
int main()
{
int n;
printf("Enter the number: ");
scanf("%d", &n);
if(n % 2 == 0)
printf("%d is even.", n);
else
printf("%d is odd.", n);
return 0;
}
Output-
Enter the number:
25
25 is odd
Question 5- write a c program to check whether a number is a palindrome or not. Answer- For this program, we will get a number from user and reverse it. Compare it with the number taken by the user. If both numbers are equal, then the number is palindrome otherwise not.
#include <stdio.h>
int main()
{
int n, reverse = 0, a;
printf("Enter a number to check for palindrome\n");
scanf("%d", &n);
  a = n;
while (a!= 0)
   {
reverse = reverse * 10;
reverse = reverse + a%10;
      a = a/10;
   }
if (n == reverse)
printf(number is palindrome: %d.\n", n);
else
printf("%d isn't a palindrome number.\n", n);
return 0;
}
Output-
Enter a number to check for palindrome
12121
Number is palindrome: 12121
Question 6: write a c program to calculate Sum & Average of an Array. Answer- For this program, we will create a fixed size array, takes the array elements from the user say n. Iterate the array element by for loop (0 to n).  Perform the sum operation in the loop block. Print the sum. For average print sum/n. This program will take the array element from the user as input and give the sum and average of the array element as output.
#include <stdio.h>
int main()
{
int A[100], n, i, sum = 0;
printf("Enter the number of array element : ");
scanf("%d", &n);
for (i = 0; i< n; i++)
    {
printf("Enter element %d : ", i + 1);
scanf("%d", &A[i]);
sum += A[i];
    }
printf("\n sum of the array elements : %d", sum);
printf("\n average of the array element: %0.2f", (float)sum / n);
return 0;
Output-
Enter the number of array element: 5
Enter element 1 : 10
Enter element 2 : 5
Enter element 3 : 2
Enter element 4 : 4
Enter element 5 : 6
Sum of the array elements: 27
Average of the array elements: 5.40
Question 7:  write a c program to calculate the sum of the array elements using the pointer. Answer- For this program we will create a fixed size array, takes the array elements from the user say n. Iterate the array element by for loop (0 to n).  And assign the array into pointer. Perform the sum operation in the loop block. Print the sum.
#include<stdio.h>
#include<conio.h>
void main() {
            int a[10];
            int n;
            inti, sum = 0;
            int *ptr;
            printf("\n Enter the size of array : ");
            scanf("%d",&n);
            printf(" enter the array element");
            for (i = 0; i< n; i++)
            {
                        scanf("%d", &a[i]);
            }
            ptr = a;
            for (i = 0; i< n; i++)
            {
                        sum = sum + *ptr;
                        ptr++;
            }
            printf("The sum of array elements : %d", sum);
            getch();
}
Output-
Enter the size of array: 5
Enter the array element
1
2
3
4
5
The sum of array elements: 15
Question 8: write a c program for selecting the largest two numbers from an array. Answer- For this program, we will declare a fixed size array let's take it 4, Define elements of an array. We will consider the first and second elements as top two higher number of the array respectively. We will interchange both l1 and I2, if I1 is smaller than I2. We will run ‘for loop' for the remaining element of array, i.e. 3 to 4. Now it will compare with all the elements of the array. We will run the loop till the end, and it will give the actual first and second largest number. See the program for better understanding.
#include <stdio.h>
    #define MAX 4
void main()
    {
int a[MAX], i, l1, l2, temp;
printf("Enter numbers:\n", MAX);
for (i = 0; i< MAX; i++)
        {
scanf("%d", &a[i]);
        }
printf("Input integer are \n");
for (i = 0; i< MAX; i++)
        {
printf("%5d", a[i]);
        }
printf("\n");
        l1 = a[0];
        l2 = a[1];
if (l1 < l2)
        {
temp = l1;
            l1 = l2;
            l2 = temp;
        }
for (i = 2; i< 4; i++)
        {
if (a[i] >= l1)
            {
                l2 = l1;
                l1 = a[i];
            }
else if (a[i] > l2)
            {
                l2 = a[i];
            }
        }
printf("first larger number is %d \n", l1);
printf("second largest number is %d \n", l2);
printf("\n Average of %d and %d = %d \n", l1, l2,
        (l1 + l2) / 2);
    }
Output-
Enter numbers:
5
15
10
12
Input integers are
5 15 10 12
First larger number is 15
Second larger number is 12
Average of 15 and 12 =13
Question 9: write a c program to delete a specific integer from an array. Answer- This program will delete an element from the array list. In this program, we will take the position from the user where he wants to delete an element. Deletion does not affect the size of the array. We will check whether deletion is possible or not. The required program is-
#include <stdio.h>
int main()
{
int a[100], p, i, n;
printf("enter the total number of array element\n");
scanf("%d", &n);
printf("Enter array elements\n");
for (i = 0; i< n; i++)
scanf("%d", &a[i]);
printf("Enter the position where you want to delete\n");
scanf("%d", &p);
if (p >= n+1)
printf("Deletion is not possible.\n");
else
   {
for (i = p - 1; i< n - 1; i++)
a[i] = a[i+1];
printf("new array:\n");
for (i = 0; i< n - 1; i++)
printf("%d\n", a[i]);
   }
return 0;
}
Output-
Enter the total number of array element
5
Enter array elements
12
10
5
14
8
Enter the position where you want to delete
3
New array:
12
10
14
8
Question 10: write a c program to convert the temperature from Celsius to Fahrenheit and vice versa.  Answer- This program will give you a choice for converting from Celsius to Fahrenheit and vice versa. We will proceed using the basic formula of temp conversion. The required program is-
#include <stdio.h>
int main()
{
floatf,c;
int option;
printf("\n1: Fahrenheit to Celsius.");
printf("\n2: Celsius to Fahrenheit.");
printf("\n Select your option (1, 2): ");
scanf("%d",&option);
if(option ==1){
printf("\nEnter temperature(in Fahrenheit): ");
scanf("%f",&f);
        c= (f - 32) / 1.8;
printf("Temperature(in Celsius): %.2f",c);
    }
else if(option==2){
printf("\nEnter temperature (n Celsius): ");
scanf("%f",&c);
        f= (c*1.8)+32;
printf("Temperature(in Fahrenheit): %.2f",f);
    }
else{
printf("\nInvalid option !!!");
    }
return 0;
}
Output-
1: Fahrenheit to Celsius
2: Celsius to Fahrenheit.
  Select your option (1, 2):    1
Enter temperature (in Fahrenheit): 77
Temperature (in Celsius): 25.00
Question 11: write a c program to calculate the gross salary of an employee. Answer- This program will read the name ; basic salary and some other part of salary to calculate the gross salary. The required program is:
#include <stdio.h>
int main()
{
char name[20];
float basic, hra, da, pf, gross;
printf("Enter employee name: ");
gets(name);
printf("Enter  the Basic Salary: ");
scanf("%f",&basic);
printf("Enter HRA: ");
scanf("%f",&hra);
printf("Enter D.A.: ");
scanf("%f",&da);
pf= (basic*12)/100;
gross=basic+da+hra+pf;
printf("\nName: %s \nBASIC: %f \nHRA: %f \nDA: %f \nPF: %f",name,basic,hra,da,pf);
printf("\n***GROSS SALARY: %f ***",gross);
return 0;
}
Output-
Enter employee name: Abhishek
Enter the basic salary: 25000
Enter HRA: 10000
Enter D.A.:9000
Name: Abhishek
Basic: 25000.00
HRA: 10000.00
DA: 9000.00
PF: 3000.00
*** GROSS SALARY: 47000.00 ***
Question 12: write a C program to convert the number from Decimal to Binary. Answer- We can convert any given decimal number in binary. Decimal numbers have base 10 (0 to 1), and binary numbers have base (0 or 1). We will follow the below process for conversion-
  • We will divide the given number by 2 and store the remainder.
  • Again divide by two
  • Repeat the above steps until the number is greater than zero and print the array in reverse order.
By this process, we can convert any decimal number into binary. Example- if the decimal number is 5. 5/2 remainder =1 and new number is 2 A[0]=1 2/2=0 remainder 0 and new number is 1 A[1]=0 A[2]=1 New array is [101] Now reverse this- 101 This is the required binary number for 5. Let's see the below example.
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10],n,i;
system ("cls");
printf("Enter decimal number: ");
scanf("%d",&n);
for(i=0;n>0;i++)
{
a[i]=n%2;
n=n/2;
}
printf("\n The binary number of the given number is=");
for(i=i-1;i>=0;i--)
{
printf("%d",a[i]);
}
return 0;
}
Output-
Enter decimal number: 5
The binary number of given number is=101
Question 13: write a c program to convert the given binary number into decimal number. In this program we will follow these steps.
  • Take a binary number and store it in the variable n.
  • Initialize the decimal value dec=o and variable base=1.
  • For remainder and quotient we will multiply rem with variable base increment the variable decimal with this new value.
  • Increment the variable base by 2.
  • Repeat this process until the quotient becomes zero.
  • Now the variable dec is our required number.
The required program is-
#include <stdio.h>
void main()
{
int  n, bin, dec= 0, b= 1, rem;
printf("enter the binary number\n");
scanf("%d", &n);
bin= n;
while (n> 0)
    {
rem = n% 10;// for remainder
dec= dec + rem *b;// for quotient
        n = n/ 10 ; //for incrementing the varibale base
        b = b * 2;  // by 2
    }
printf("The required decimal number is %d\n", dec);
}
  Output-
Enter the binary number
1011
The required decimal number is 11
Question 14: how many times the program will print “Tutorialandexample”?
#include<stdio.h>
int main()
{
printf("Tutorialandexample");
main();
return 0;
}
Answer- This program will print “tutorialandexample” till stack overflows, because stack overflow occurs when call the stack (it used too much memory). In the given code,the main() function is called repeatedly and its returned address is stored in the stack. After some time, it will show error like stack overflow because of memory full. Question 15: what will the output of the below program?
#include<stdio.h>
int main()
{
   int n;
   for(n = 5; n!=0; n--)
     printf("n = %d", n--);
   getchar();
   return 0;
}
Answer- Given program will go in infinite loop because n will never be zero because of the loop condition (n!=0). Question 16: what will the output of the below program?
#include <stdio.h>
void main()
{                 
    int a = 25;
    printf("%o %x", a, a);
    getch();
}
Output- This program will give the output as 31 19 because %o is used for octal number and %x is used for hexadecimal number. Octal of 25=31 Hexadecimal of 25=19 Question 17: write a c program to transpose a matrix. Answer- Transpose matrix- Transpose matrix is obtained by interchanging the rows and columns. The transpose of a square matrix remains the same. Transpose of a matrix changes its order. E.g.
1   2    3
4   5    6
Transpose-
1    4
2   5
3   6
C program for transpose of a matrix-
#include <stdio.h>
int main()
{
int a[10][10], t[10][10], row, col, i, j;
printf("Enter the number of rows and columns of matrix: ");
scanf("%d %d", &row, &col);
printf("\nEnter elements of matrix:\n");
for(i=0; i<row; ++i)
for(j=0; j<col; ++j)
        {
printf("Enter the elements a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
        }
printf("\nGiven Matrix: \n");
for(i=0; i<row; ++i)
for(j=0; j<col; ++j)
        {
printf("%d  ", a[i][j]);
if (j == col-1)
printf("\n\n");
        }
for(i=0; i<row; ++i)
for(j=0; j<col; ++j)
        {
t[j][i] = a[i][j];
        }
printf("\nTranspose of Matrix:\n");
for(i=0; i<col; ++i)
for(j=0; j<row; ++j)
        {
printf("%d  ",t[i][j]);
if(j==row-1)
printf("\n\n");
        }
return 0;
}
Output-
Enter the number of rows and columns of matrix:
2
2
Enter elements of matrix:
Enter the elements a11: 8
Enter the elements a12: 9
Enter the elements a21: 3
Enter the elements a22:  5
Given matrix:
8   9
3   5
Transpose of matrix:
8   3
9   5
Question 18: write a c program for multiplying two matrices. Answer- Multiplying a matrix by another matrix- For two matrix multiplication, we need “dot product” of rows and columns. The “dot product” is where we multiply matching number (row or column), then sum up. Dot Products C programming coding (1, 2, 3)*(7, 9, 11) =1*7+2*9+3*11=58 (1, 2, 3)*(8, 10, 12) =1*8+2*10+3*12=64 (4, 5, 6)*(7, 9, 11) = 4*7+5*9+6*11=139 (4, 5, 6) *(8, 10, 12) =4*8+5*10+6*12=154 Resultant matrix will be- Matric C Programming  C program for matrix multiplication-
#include <stdio.h>
int main()
{
int r1, c1, r2, c2, c, d, k, sum = 0;
int a1[10][10], a2[10][10], mul[10][10];
printf("Enter number of rows and columns of first matrix\n");
scanf("%d%d", &r1, &c1);
printf("Enter elements of first matrix\n");
for (c = 0; c < r1; c++)
for (d = 0; d < c1; d++)
scanf("%d", &a1[c][d]);
printf("Enter number of rows and columns of second matrix\n");
scanf("%d%d", &r2, &c2);
if (c1 != r2)
printf("The matrices can't be multiplied with each other.\n");
else
  {
printf("Enter elements of second matrix\n");
for (c = 0; c < r2; c++)
for (d = 0; d < c2; d++)
scanf("%d", &a2[c][d]);
for (c = 0; c < r1; c++) {
for (d = 0; d < c2; d++) {
for (k = 0; k < r2; k++) {
sum = sum + a1[c][k]*a2[k][d];
        }
mul[c][d] = sum;
sum = 0;
      }
    }
printf("Product of the matrices:\n");
for (c = 0; c < r1; c++) {
for (d = 0; d < c2; d++)
printf("%d\t", mul[c][d]);
printf("\n");
    }
  }
return 0;
}
}
Output-
Enter number of rows and columns of first matrix
2
3
Enter elements of first matrix
1
2
3
4
5
6
Enter number of rows and columns of second matrix
3
2
Enter elements of second matrix
7
8
9
10
11
12
Product of the matrices:
58      64
139   154
  Question 19: write a c program to verify the string is palindrome or not. Answer- Required program is-
#include<stdio.h>
#include <string.h>
int main()
{
char a1[50], a2[50];
printf("Enter a string \n");
gets(a1);
strcpy(a2, a1);  // Copying
strrev(a2); //reversing
if (strcmp(a1, a2) == 0)
printf("given string is :palindrome.\n");
else
printf("Given string isn't :palindrome.\n");
return 0;
}
Output- Enter a string Asharahsa Given String is :palindrome Question 20: write a c program to print the source code as output. Answer- It is possible to print source code as output. It's a trick by using file handling. It is possible because of a predefined macro in c _FILE_ containing the location of a C programming file.
#include <stdio.h>
int main(void)
{
    FILE *f;
char c;
    f = fopen(__FILE__, "r");
do
    {
        c=fgetc(f);
putchar(c);
    }
while(c!=EOF);
fclose(f);
return 0;
}
Output-
#include <stdio.h>
int main(void)
{
    FILE *f;
char c;
    f = fopen(__FILE__, "r");
do
    {
        c=fgetc(f);
putchar(c);
    }
while(c!=EOF);
fclose(f);
return 0;
}

Related Topics

Top 15 PostgreSQL Interview Questions for 2024

1) What is PostgreSQL? PostgreSQL is a most advance open source database system. PostgreSQL is an object Oriented Relational Database Management System (ORDBMS). PostgreSQL source code is available free of charge...

3 minutes read.

Symfony PHP Framework Interview Questions

1) What is Symfony? Symfony is an open source, web application framework. It is written in PHP and used to design PHP applications. It was first released on18 October, 2005. 2) What...

5 minutes read.

Top 28 Firebase Interview Questions for 2024

1) What is Firebase? Firebase is a platform which is used for building Web, IOS and Android applications. It offers: Real time database Different APIs Multiple authentication types and Hosting platform 2) What are the features of...

3 minutes read.

Top 40 TensorFlow Interview Questions and Answers

Commonly Asked TensorFlow Interview Questions for Fresher Q1. What is TensorFlow? TensorFlow is the machine learning library developed by Google's brain team, it supports multiple abstraction levels, and we can choose the...

13 minutes read.

Top 16 Arduino Interview Questions for 2024

1) What is Arduino? Arduino is an open source electronic platform. It is based on easy-to-use hardware and software. It able to read input signal. It is used to write and...

2 minutes read.

Top 15 AWS Interview Questions for 2024

1) What is AWS? AWS stands for Amazon Web Services. It is a cloud web hosting platform that offers flexible, scalable, reliable, easy-to-use, and cost-effective solutions. 2) What is Cloud Computing? It is...

2 minutes read.

Top 16 OpenStack Interview Questions for 2024

Most Frequently Asked OpenStack Interview Questions and Answers for Freshers 1. Describe OpenStack. OpenStack is designed as the future of cloud computing and developed as free and open-source software that facilitates a...

8 minutes read.

Top 30 CSS Interview Questions for 2024

1) Define the process where block elements can be centered with css. It can be centered by using margin-left and margin-right properties we can centered the block level elements. 2) Name the...

4 minutes read.

Top 25 CoffeeScript Interview Questions for 2024

1) What is CoffeeScript? It is a language that compiles into JavaScript. CoffeeScript is an attempt to expose good parts of JavaScript so, that user can code more easily. 2) Name the...

3 minutes read.

Top 30 Perl interview questions for 2024

1) What is Perl? It is a programming language which is used for web development, system administration, GUI development and etc. It is also known as cross-platform programming language. 2) What are the...

4 minutes read.

Top 15 Bootstrap Interview Questions for 2024

1) Explain what is Bootstrap? It is a Framework which is used for building the web application with minimal effort. Bootstrap is also used for developing responsive, mobile-first web sites. 2) What...

10 minutes read.

Top 30 Web2py Framework Interview Questions for 2024

1) What is Web2py Framework? Web2py is an open source web application framework. It is written in the Python programming language. It allows web developers to design and develop dynamic web...

7 minutes read.

Top 50 HTML Interview Questions for 2024

1) What is HTML? HTML stands for Hyper Text Markup Language. It is used for creating web pages and web applications. HTML documents are made up of two things: the content and the tags. 2) What are...

10 minutes read.

WebSockets Interview Questions

1) What is Web Sockets? Web Sockets is a two way communication between the clients and the servers. Here, the clients and servers both the parties can communicate and exchange data...

3 minutes read.

Top 25 MongoDB interview Questions for 2024

1) Give some history about MongoDB. MongoDB came into existence when data increased exponentially on the internet. Thus, maintainence of database became very difficult in structured DB. MongoDB is a NoSQL...

3 minutes read.

Top 15 Website Development Interview Questions for 2024

1) What is Website Development? Website Development is a process that includes following steps. web design web content development client-side or server-side scripting network security configuration etc 2) What are the skills...

3 minutes read.

Top 25 MySQL Interview Questions for 2024

1) What is MySQL? MySql is defined as multi-user DBMS which is built and distributed my MySQLab now acquired by Oracle Co. 2) Enlist the features of MySQL. Cross-Platform Support Wide range...

3 minutes read.

Top 24 Clojure Interview Questions for 2024

1) What is Clojure? Clojure is a dynamic, general-purpose programming language. It is simple, coherent and powerful tool. It is based on Lisp Programming language. 2) Which platform is used to run...

4 minutes read.

Flutter Interview Questions

Here’s a list of top Flutter questions that are frequently asked. Q1.  Is Flutter an open-source? A1.  Yes, Flutter is a 100% open-source software development kit. Q2.  What are flutter widgets? A2.  Widgets are...

7 minutes read.

Top 15 Xamarin Interview Questions for 2024

1) What is Xamarin? Xamarin is a .NET framework. It allows to create an application that run on multiple platforms. Xamarin Tutorial uses C# language and .NET framework to deliver native...

2 minutes read.