Coding Interview Questions In C Commonly asked _ PrepInsta
Coding Interview Questions In C Commonly asked _ PrepInsta
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.
Basics of C
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);
if (low % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d ", low);
OUTPUT
*
* *
* * *
* * * *
* * * * *
Solution:-
#include <stdio.h>
int main() {
int i, j, rows;
scanf("%d", &rows);
printf("* ");
printf("\n");
}
return 0;
Solution:-
#include
Numbers(int n);
int main() {
int num;
scanf("%d", &num);
return 0;
int addNumbers(int n) {
if (n != 0)
( )
else
return n;
OUTPUT
Solution:-
#include<stdio.h>
int main() {
int i, n;
float arr[100];
printf("Enter the number of elements (1 to 100): ");
scanf("%d", &n);
return 0;
}
OUTPUT
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);
OUTPUT
10 8 6
Solution:-
#include
int main() {
char line[150];
OUTPUT
Solution:-
#include
int main() {
char s[] = "Programming is fun";
int i;
OUTPUT
Solution:-
#include
struct TIME {
int seconds;
int minutes;
int hours;
};
int main() {
struct TIME startTime, stopTime, diff;
OUTPUT
Solution:-
#include
int main() {
int num;
float *data;
printf("Enter the total number of elements: ");
scanf("%d", &num);
return 0;
}
10. Write a C Program to Access Array Elements Using Pointer
Solution:-
#include <stdio.h>
int main() {
int data[5];
OUTPUT
Enter elements: 1
2
3
5
4
You entered:
1
2
3
5
4
#include
int main() {
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
OUTPUT
Entered matrix:
1 4 0
-5 2 7
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];
[j 1]
array[j+1]=swap;
}
}
}
printf("Sorted Array:n");
for(i = 0; i < n; i++)
printf("%dn", array[i]);
return 0;
}
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;
}
OUTPUT
#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);
}
}
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:
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
Output :
1987384758
2057844389
3475398489
2247357398
1435983905
17. Write a C program to print hello world without using a semicolon (;).
Solution:-
#include<stdio.h>
void main()
{
if(printf("hello world")){}
}
Output:
hello world
18. Write a program to swap two numbers without using the third variable.
Solution:-
#include<stdio.h>
#include<conio.h>
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
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;
}
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