Here are few of the frequently asked programs in C language interviews. Each of the questions have been answered with code and , output and explanation.Question.27(even/odd) is a good starter in an C interview for selecting entry level resources. Question.28(greatest of three) tests whether interviewee candidate is able to write a simple program using control structures. Question.29(prime or not) tests whether the candidate is able to write an algorithm for recognizing a prime number. It gives a fair idea of candidate’s programming ability at entry level. Question.30 tests the analytical and programming ability of the candidate. It is also a fair way to know about C programming knowledge of the candidate depending upon his/her choice of storing the variables. See all C interview questions in this blog in one page.
- Write a program to check whether a given number is even or odd. Program:
- Write a program to find the greatest of three numbers.
- Write a program to check whether a given number is a prime.
- Write a program to find the greatest among ten numbers.
#include <stdio.h>
int main() {
int a;
printf("Enter a: \n");
scanf("%d", &a);
/* logic */
if (a % 2 == 0) {
printf("The given number is EVEN\n");
}
else {
printf("The given number is ODD\n");
}
return 0;
}
Output:
Enter a: 2 The given number is EVEN
Explanation with examples:
Example 1: If entered number is an even number
Let value of 'a' entered is 4
if(a%2==0) then a is an even number, else odd.
i.e. if(4%2==0) then 4 is an even number, else odd.
To check whether 4 is even or odd, we need to calculate (4%2).
/* % (modulus) implies remainder value. */
/* Therefore if the remainder obtained when 4 is divided by 2
is 0, then 4 is even. */
4%2==0 is true
Thus 4 is an even number.
Example 2: If entered number is an odd number.
Let value of 'a' entered is 7
if(a%2==0) then a is an even number, else odd.
i.e. if(7%2==0) then 4 is an even number, else odd.
To check whether 7 is even or odd, we need to calculate (7%2).
7%2==0 is false /* 7%2==1 condition fails and else part is
executed */
Thus 7 is an odd number.
Program:
#include <stdio.h>
int main(){
int a, b, c;
printf("Enter a,b,c: \n");
scanf("%d %d %d", &a, &b, &c);
if (a > b && a > c) {
printf("a is Greater than b and c");
}
else if (b > a && b > c) {
printf("b is Greater than a and c");
}
else if (c > a && c > b) {
printf("c is Greater than a and b");
}
else {
printf("all are equal or any two values are equal");
}
return 0;
}
Output:
Enter a,b,c: 3 5 8 c is Greater than a and b
Explanation with examples: consider three numbers a=5,b=4,c=8 if(a>b && a>c) then a is greater than b and c now check this condition for the three numbers 5,4,8 i.e. if(5>4 && 5>8) /* 5>4 is true but 5>8 fails */ so the control shifts to else if condition else if(b>a && b>c) then b is greater than a and c now checking this condition for 5,4,8 i.e. else if(4>5 && 4>8) /* both the conditions fail */ now the control shifts to the next else if condition else if(c>a && c>b) then c is greater than a and b now checking this condition for 5,4,8 i.e. else if(8>5 && 8>4) /* both conditions are satisfied */ Thus c is greater than a and b.
A prime number is a natural number that has only one and itself as factors. Examples: 2, 3, 5, 7,… are prime numbers. Program:
#include <stdio.h>
main() {
int n, i, c = 0;
printf("Enter any number n: \n");
scanf("%d", &n);
/*logic*/
for (i = 1; i <= n; i++) {
if (n % i == 0) {
c++;
}
}
if (c == 2) {
printf("n is a Prime number");
}
else {
printf("n is not a Prime number");
}
return 0;
}
Output:
Enter any number n: 7 n is Prime
consider a number n=5 for(i=0;i<=n;i++) /* for loop is executed untill the n value equals i */ i.e. for(i=0;i<=5;i++) /* here the for loop is executed untill i is equal to n */ 1st iteration: i=1;i<=5;i++ here i is incremented i.e. i value for next iteration is 2 now if(n%i==0) then c is incremented i.e.if(5%1==0)then c is incremented, here 5%1=0 thus c is incremented. now c=1; 2nd iteration: i=2;i<=5;i++ here i is incremented i.e. i value for next iteration is 3 now if(n%i==0) then c is incremented i.e.if(5%2==0) then c is incremented, but 5%2!=0 and so c is not incremented, c remains 1 c=1; 3rd iteration: i=3;i<=5;i++ here i is incremented i.e. i value for next iteration is 4 now if(n%i==0) then c is incremented i.e.if(5%3==0) then c ic incremented, but 5%3!=0 and so c is not incremented, c remains 1 c=1; 4th iteration: i=4;i<=5;i++ here i is incremented i.e. i value for next iteration is 5 now if(n%i==0) then c is incremented i.e. if(5%4==0) then c is incremented, but 5%4!=0 and so c is not incremented, c remains 1 c=1; 5th iteration: i=5;i<=5;i++ here i is incremented i.e. i value for next iteration is 6 now if(n%i==0) then c is incremented i.e. if(5%5==0) then c is incremented, 5%5=0 and so c is incremented. i.e. c=2 6th iteration: i=6;i<=5;i++ here i value is 6 and 6<=5 is false thus the condition fails and control leaves the for loop. now if(c==2) then n is a prime number we have c=2 from the 5th iteration and thus n=5 is a Prime number.
Program:
#include <stdio.h>
int main() {
int a[10];
int i;
int greatest;
int position =0;
printf("Enter ten values:");
//Store 10 numbers in an array
for (i = 0; i < 10; i++) {
scanf("%d", &a[i]);
}
//Assume that a[0] is greatest
greatest = a[0];
for (i = 0; i < 10; i++) {
if (a[i] > greatest) {
greatest = a[i];
position = i+1;
}
}
printf("\n Greatest of ten numbers is %d at position %d \n", greatest, position);
return 0;
}
Output:
Enter ten values: 2 53 65 3 88 8 14 5 77 64 Greatest of ten numbers is 88 at position 5
Explanation with example: Entered values are 2, 53, 65, 3, 88, 8, 14, 5, 77, 64 They are stored in an array of size 10. let a[] be an array holding these values. /* how the greatest among ten numbers is found */ Let us consider a variable ‘greatest’. At the beginning of the loop, variable ‘greatest’ is assinged with the value of first element in the array greatest=a[0]. Here variable ‘greatest’ is assigned 2 as a[0]=2.
Below loop is executed until end of the array 'a[]';.
for(i=0; i<10;>greatest)
{
greatest= a[i];
}
}
For each value of ‘i’, value of a[i] is compared with value of variable ‘greatest’. If any value greater than the value of ‘greatest’ is encountered, it would be replaced by a[i]. After completion of ‘for’ loop, the value of variable ‘greatest’ holds the greatest number in the array. In this case 88 is the greatest of all the numbers.
| Previous | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | Next |