Top C Programming Questions for Interviews with Solutions

Top C Programming Questions for Interviews with Solutions

Top C Programming Questions for Interviews with Solutions

Introduction

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.


1. Generate Fibonacci Series

Problem Statement:

Write a program to generate the Fibonacci series up to a given limit.

Solution:

#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;
    }
}

Explanation:

  • The program initializes two variables, fib1 and fib2.
  • It prints the first two numbers and then iterates to generate subsequent terms.
  • The while loop ensures the Fibonacci series is printed up to the given limit.

2. Compare Two Strings Without Using strcmp()

Problem Statement:

Write a program to compare two strings without using the built-in strcmp() function.

Solution:

#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;
}

Explanation:

  • The program takes two strings as input.
  • It compares each character sequentially without using strcmp().
  • If all characters match, the strings are equal; otherwise, they are not.

3. Concatenate Two Strings Without Using strcat()

Problem Statement:

Write a program to concatenate two strings without using the strcat() function.

Solution:

#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);
}

Explanation:

  • The program finds the length of str1.
  • It appends str2 to str1 character by character.
  • The concatenated string is printed at the end.

4. Print “Hello World” Without Using a Semicolon

Problem Statement:

Write a program to print “Hello World” without using a semicolon anywhere in the code.

Solution:

#include<stdio.h>

void main() {
    if(printf("Hello World")) {
    }
}

Alternative Solutions:

#include<stdio.h>

void main() {
    while(!printf("Hello World")) {
    }
}
#include<stdio.h>

void main() {
    switch(printf("Hello World")) {
    }
}

Explanation:

  • The printf() function prints the output inside different control structures like if, while, or switch, ensuring no semicolon is explicitly used.

5. Print a Semicolon Without Using a Semicolon

Problem Statement:

Write a program to print a semicolon (;) without using a semicolon anywhere in the code.

Solution:

#include <stdio.h>

void main(void) {
    if (printf("%c", 59)) {
    }
}

Explanation:

  • The ASCII value of ; is 59.
  • Using printf("%c", 59), we print ; without using a direct semicolon in the code.

Conclusion

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!

Top C Programming Questions for Interviews with Solutions