15 Important Coding Interviews with Detailed Solutions

Top 15 Important Programs for Interviews with Solutions

programming is an essential skill for aspiring software developers. Here, we present several basic programs that illustrate fundamental concepts, such as checking for prime numbers, generating Fibonacci series, and more.

  1. Prime Number Program
  2. Fibonacci Series (Without Recursion)
  3. Fibonacci Series (With Recursion)
  4. Factorial Number Program
  5. Pyramid Star Pattern
  6. Sum of Digits
  7. Palindrome Number Check
  8. Leap Year Check
  9. Armstrong Number Check
  10. Strong Number Check
  11. Perfect Number Check
  12. Swapping Two Numbers Without Using a Third Variable
  13. Harshad Number Check
  14. Even or Odd Number Check
  15. Reversing a Number
  16. Decimal to Binary Conversion
  17. Greatest of Three Numbers
  18. GCD (Greatest Common Divisor) of Two Numbers
  19. LCM (Least Common Multiple) of Two Numbers
  20. Sum of N Natural Numbers

1. Prime Number Program

A prime number is a natural number greater than 1 that is divisible only by 1 and itself.

#include <stdio.h>
int main() {
    int n, i, m = 0, flag = 0;
    printf("Enter the number to check prime: ");
    scanf("%d", &n);
    m = n / 2;
    for (i = 2; i <= m; i++) {
        if (n % i == 0) {
            printf("Number is not prime\n");
            flag = 1;
            break;
        }
    }
    if (flag == 0)
        printf("Number is prime\n");
    return 0;
}

Output:

Enter the number to check prime: 56
Number is not prime

Enter the number to check prime: 23
Number is prime

2. Fibonacci Series

The Fibonacci series is a sequence where the next number is the sum of the previous two.

Without Recursion

#include <stdio.h>
int main() {
    int n1 = 0, n2 = 1, n3, i, number;
    printf("Enter the number of elements: ");
    scanf("%d", &number);
    printf("%d %d", n1, n2);
    for (i = 2; i < number; ++i) {
        n3 = n1 + n2;
        printf(" %d", n3);
        n1 = n2;
        n2 = n3;
    }
    return 0;
}

With Recursion

#include <stdio.h>
void printFibonacci(int n) {
    static int n1 = 0, n2 = 1, n3;
    if (n > 0) {
        n3 = n1 + n2;
        n1 = n2;
        n2 = n3;
        printf("%d ", n3);
        printFibonacci(n - 1);
    }
}
int main() {
    int n;
    printf("Enter the number of elements: ");
    scanf("%d", &n);
    printf("Fibonacci Series: %d %d ", 0, 1);
    printFibonacci(n - 2);
    return 0;
}

3. Factorial Number Program

The factorial of a non-negative integer is the product of all positive integers less than or equal to that number.

#include <stdio.h>
int main() {
    int i, fact = 1, number;
    printf("Enter a number: ");
    scanf("%d", &number);
    for (i = 1; i <= number; i++) {
        fact = fact * i;
    }
    printf("Factorial of %d is: %d\n", number, fact);
    return 0;
}

4. Pattern Program

This program prints a pyramid star pattern.

#include <stdio.h>
#include <conio.h>
void main() {
    int i, j, rows, k = 0;
    printf("Enter a number to define the rows: \n");
    scanf("%d", &rows);
    for (i = 1; i <= rows; i++) {
        for (j = 1; j <= rows - i; j++) {
            printf("  ");
        }
        for (k = 1; k <= (2 * i - 1); k++) {
            printf("* ");
        }
        printf("\n");
    }
    getch();
}

Output:

   *
  * *
 * * *

5. Sum of Digits in a Program

This program calculates the sum of digits of a given number.

#include <stdio.h>
int main() {
    int n, sum = 0, m;
    printf("Enter a number: ");
    scanf("%d", &n);
    while (n > 0) {
        m = n % 10;
        sum = sum + m;
        n = n / 10;
    }
    printf("Sum is = %d\n", sum);
    return 0;
}

Output:

Enter a number: 654
Sum is = 15

Enter a number: 123
Sum is = 6

6. Palindrome Number Check

A palindrome number is a number that remains the same when its digits are reversed.

#include <stdio.h>
int main() {
    int n, r, sum = 0, temp;
    printf("Enter the number: ");
    scanf("%d", &n);
    temp = n;
    while (n > 0) {
        r = n % 10;
        sum = (sum * 10) + r;
        n = n / 10;
    }
    if (temp == sum)
        printf("Palindrome number\n");
    else
        printf("Not a palindrome number\n");
    return 0;
}

Output:

Enter the number: 151
Palindrome number

7. Leap Year Check

This program checks whether a given year is a leap year.

#include <stdio.h>
int main() {
    int year;
    printf("Enter a year to check if it is a leap year: ");
    scanf("%d", &year);
    if (year % 400 == 0)
        printf("%d is a leap year.\n", year);
    else if (year % 100 == 0)
        printf("%d is not a leap year.\n", year);
    else if (year % 4 == 0)
        printf("%d is a leap year.\n", year);
    else
        printf("%d is not a leap year.\n", year);
    return 0;
}

Output:

Enter a year to check if it is a leap year: 2012
2012 is a leap year.

8. Armstrong Number Check

An Armstrong number is a number that is equal to the sum of cubes of its digits.

#include <stdio.h>
int main() {
    int n, r, sum = 0, temp;
    printf("Enter the number: ");
    scanf("%d", &n);
    temp = n;
    while (n > 0) {
        r = n % 10;
        sum = sum + (r * r * r);
        n = n / 10;
    }
    if (temp == sum)
        printf("Armstrong number\n");
    else
        printf("Not an Armstrong number\n");
    return 0;
}

9. Strong Number Check

A strong number is a number where the sum of the factorial of its digits equals the number itself.

#include <stdio.h>
int fact(int r) {
    int mul = 1;
    for (int i = 1; i <= r; i++) {
        mul = mul * i;
    }
    return mul;
}
int main() {
    int n, sum = 0, r;
    printf("Enter a number: ");
    scanf("%d", &n);
    int k = n;
    while (k != 0) {
        r = k % 10;
        sum = sum + fact(r);
        k = k / 10;
    }
    if (sum == n)
        printf("Number is a strong number\n");
    else
        printf("Number is not a strong number\n");
    return 0;
}

10. Perfect Number Check

A perfect number is a positive integer that is equal to the sum of its proper divisors.

#include <stdio.h>
#include <conio.h>
void main() {
    int i = 1, num, sum = 0;
    printf("Enter any number to check Perfect Number: ");
    scanf("%d", &num);
    while (i < num) {
        if (num % i == 0)
            sum = sum + i;
        i++;
    }
    if (sum == num)
        printf("%d is a Perfect Number\n", num);
    else
        printf("%d is not a Perfect Number\n", num);
    getch();
}

11. Swapping Two Numbers Without Using a Third Variable

This program swaps two numbers without using a third variable.

#include <stdio.h>
int main() {
    int a = 10, b = 20;
    printf("Before swap a=%d b=%d\n", a, b);
    a = a + b;
    b = a - b;
    a = a - b;
    printf("After swap a=%d b=%d\n", a, b);
    return 0;
}

12. Harshad Number Check

A Harshad number is an integer that is divisible by the sum of its digits.

#include <stdio.h>
int main() {
    int num = 156, rem = 0, sum = 0, n;
    n = num;
    while (num > 0) {
        rem = num % 10;
        sum = sum + rem;
        num = num / 10;
    }
    if (n % sum == 0)
        printf("%d is a Harshad number\n", n);
    else
        printf("%d is not a Harshad number\n", n);
    return 0;
}

13. Even or Odd Number Check

This program checks whether a given number is even or odd.

#include <stdio.h>
int main() {
    int num;
    printf("Enter an integer: ");
    scanf("%d", &num);
    if (num % 2 == 0)
        printf("%d is even.\n", num);
    else
        printf("%d is odd.\n", num);
    return 0;
}

14. Reversing a Number

This program reverses the digits of a given number.

#include <stdio.h>
int main() {
    int n, reverse = 0, rem;
    printf("Enter a number: ");
    scanf("%d", &n);
    while (n != 0) {
        rem = n % 10;
        reverse = reverse * 10 + rem;
        n = n / 10;
    }
    printf("Reversed Number: %d\n", reverse);
    return 0;
}

15. Decimal to Binary Conversion

This program converts a decimal number to its binary equivalent.

#include <stdio.h>
#include <stdlib.h>
int main() {
    int a[10], n, i;
    system("cls");
    printf("Enter the number to convert: ");
    scanf("%d", &n);
    for (i = 0; n > 0; i++) {
        a[i] = n % 2;
        n = n / 2;
    }
    printf("Binary of Given Number is = ");
    for (i = i - 1; i >= 0; i--) {
        printf("%d", a[i]);
    }
    return 0;
}

Output:

Enter the number to convert: 5
Binary of Given Number is = 101

Conclusion

Mastering these 15 essential programs will significantly enhance your problem-solving skills and prepare you for technical coding interviews. Each program covers a fundamental concept, ranging from prime number checks and Fibonacci series to more complex topics like palindrome verification and decimal to binary conversion. understanding and implementing these solutions, you’ll be well-equipped to tackle a wide variety of questions commonly posed by interviewers. Practice these programs thoroughly, and you’ll gain the confidence and proficiency needed to excel in your coding interviews. Happy coding and best of luck with your interviews!

Best career options after B-Tech for students with backlogs
Grab Program Analyst Role in Top Company with High Pay Scales