C programming is a crucial part of technical interviews, especially for freshers and those preparing for coding rounds. Below, we explore five commonly asked C programming questions, their solutions, and explanations to help you ace your interviews.
Write a program to generate the Fibonacci series up to a given limit.
#include <stdio.h>
void main() {
int fib1 = 0, fib2 = 1, fib3, limit, count = 0;
printf("Enter the limit to generate the Fibonacci Series:\n");
scanf("%d", &limit);
printf("Fibonacci Series: \n");
printf("%d\n", fib1);
printf("%d\n", fib2);
count = 2;
while (count < limit) {
fib3 = fib1 + fib2;
count++;
printf("%d\n", fib3);
fib1 = fib2;
fib2 = fib3;
}
}
fib1
and fib2
.while
loop ensures the Fibonacci series is printed up to the given limit.strcmp()
Write a program to compare two strings without using the built-in strcmp()
function.
#include<stdio.h>
int main() {
char str1[20], str2[20];
int i = 0, c = 0;
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
while((str1[i] != '\0') || (str2[i] != '\0')) {
if(str1[i] != str2[i])
c++;
i++;
}
if(c == 0)
printf("Strings are equal.\n");
else
printf("Strings are not equal.\n");
return 0;
}
strcmp()
.strcat()
Write a program to concatenate two strings without using the strcat()
function.
#include<stdio.h>
void main() {
char str1[25], str2[25];
int i = 0, j = 0;
printf("Enter First String: ");
gets(str1);
printf("Enter Second String: ");
gets(str2);
while(str1[i] != '\0')
i++;
while(str2[j] != '\0') {
str1[i] = str2[j];
j++;
i++;
}
str1[i] = '\0';
printf("Concatenated String: %s\n", str1);
}
str1
.str2
to str1
character by character.Write a program to print “Hello World” without using a semicolon anywhere in the code.
#include<stdio.h>
void main() {
if(printf("Hello World")) {
}
}
#include<stdio.h>
void main() {
while(!printf("Hello World")) {
}
}
#include<stdio.h>
void main() {
switch(printf("Hello World")) {
}
}
printf()
function prints the output inside different control structures like if
, while
, or switch
, ensuring no semicolon is explicitly used.Write a program to print a semicolon (;
) without using a semicolon anywhere in the code.
#include <stdio.h>
void main(void) {
if (printf("%c", 59)) {
}
}
;
is 59.printf("%c", 59)
, we print ;
without using a direct semicolon in the code.Mastering fundamental C programming questions like these will strengthen your problem-solving skills and boost your confidence during interviews. Stay prepared, practice regularly, and ace your coding tests!