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.
(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-
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;
}