Looking For Anything Specific?

ads header

Coding problems for Technical Interview - Part 1

Coding problems for technical interview with answers in C programming language.



 1. Linear Search

#include <stdio.h>

int main()

{

  int arr[100], search, i, n;

  printf("Enter number of elements in array:\n");

  scanf("%d", &n);

  printf("Enter %d integers:\n", n);

  for (i = 0; i < n; i++)

    scanf("%d", &arr[i]);

  printf("Enter a number to search\n");

  scanf("%d", &search);

  for (i = 0; i < n; i++)

  {

    if (arr[i] == search)

    {

      printf("%d is present at %d position in the array.\n", search, i+1);

      break;

    }

  }

  if (i == n)

    printf("%d is not present in the array.\n", search);

 return 0;

}


2. Swapping two numbers without a variable

#include<stdio.h>  

 int main()    

{    

int a=20, b=30;      

printf("Before swap a=%d b=%d",a,b);      

a=a+b;    

b=a-b;    

a=a-b;  

printf("\nAfter swap a=%d b=%d",a,b);    

return 0;  

}   


3. Swapping numbers using variable

#include <stdio.h> 

int main()

{

int a, b, temp; 

printf("Enter two integers:\n");

scanf("%d%d", &a, &b);

printf("Before Swapping:\nFirst variable = %d\nSecond variable = %d\n", a, b);

temp = a;

a = b;

b = temp;

printf("After Swapping:\nFirst variable = %d\nSecond variable = %d\n", a, b);

return 0;

}


4. Remove duplicate elements in an array
#include <stdio.h>  
#include <conio.h>  
int main ()  
{  
    int arr[20], i, j, k, size;  
    printf ("Number of elements in an array: ");  
    scanf (" %d", &size);  
    printf (" \nEnter %d elements: \n ", size);  
    for ( i = 0; i < size; i++)  
    {  
        scanf (" %d", &arr[i]);  
    }  
    for ( i = 0; i < size; i ++)  
    {  
        for ( j = i + 1; j < size; j++)  
        {  
            if ( arr[i] == arr[j])  
            {  
                for ( k = j; k < size - 1; k++)  
                {  
                    arr[k] = arr [k + 1];  
                }  
                size--;  
                j--;      
            }  
        }  
    }  
    printf (" \n Array after deleting duplicate elements: ");  
    for ( i = 0; i < size; i++)  
    {  
        printf (" %d \t", arr[i]);  
    }  
    return 0;  
}  

5. Factorial of a number
#include <stdio.h>
int main() {
    int n, i;
    unsigned long long fact = 1;
    printf("Enter an integer: ");
    scanf("%d", &n);
    if (n < 0)
        printf("Error!");
    else {
        for (i = 1; i <= n; ++i) {
            fact *= i;
        }
        printf("Factorial of %d = %llu", n, fact);
    }
    return 0;
}

6. Fibonacci series upto n terms
#include <stdio.h>
int main() {
  int i, n;
  int n1 = 0, n2 = 1;
  int nextTerm = n1 + n2;
  printf("Enter the number of terms: ");
  scanf("%d", &n);
  printf("Fibonacci Series: %d, %d, ", n1, n2);
  for (i = 3; i <= n; ++i) {
    printf("%d, ", nextTerm);
    n1 = n2;
    n2 = nextTerm;
    nextTerm = n1 + n2;
  }
  return 0;
}

7. Sort array in ascending order
   #include <stdio.h>
    void main()
    {
        int i, j, a, n, number[30];
        printf("Enter the number of elements in array: \n");
        scanf("%d", &n);
        printf("Enter the numbers: \n");
        for (i = 0; i < n; ++i)
            scanf("%d", &number[i]);
        for (i = 0; i < n; ++i) 
        {
            for (j = i + 1; j < n; ++j)
            {
                if (number[i] > number[j]) 
                {
                    a =  number[i];
                    number[i] = number[j];
                    number[j] = a;
                }
            }
        }
        printf("The numbers arranged in ascending order is: \n");
        for (i = 0; i < n; ++i)
            printf("%d\n", number[i]);
 
    }

8. Check if a number is palindrome or not.
#include <stdio.h>
int main() {
  int n, rev = 0, rem, n1;
    printf("Enter an integer: ");
    scanf("%d", &n);
    n1 = n;
    while (n != 0) {
        rem = n % 10;
        rev = rev * 10 + rem;
        n /= 10;
    }
    if (n1 == rev)
        printf("%d is a palindrome.", n1);
    else
        printf("%d is not a palindrome.", n1);
    return 0;
}

9. Reverse a string/ number
#include <stdio.h>
int main()
{
  char str[1000], rev[1000];
  int i, j, count = 0;
  scanf("%s", str);
  printf("\nBefore reversing: %s", str);
  while (str[count] != '\0')
  {
    count++;
  }
  j = count - 1;
  for (i = 0; i < count; i++)
  {
    rev[i] = str[j];
    j--;
  }
  printf("\nAfter reversing: %s", rev);
}

10. Convert binary number to decimal
#include <stdio.h>
#include <math.h>
int convert(long long);
int main() {
  long long n;
  printf("Enter binary number: ");
  scanf("%lld", &n);
  printf("%lld in binary = %d in decimal", n, convert(n));
  return 0;
}
int convert(long long n) {
  int dec = 0, i = 0, rem;
  while (n!=0) {
    rem = n % 10;
    n /= 10;
    dec += rem * pow(2, i);
    ++i;
  }
  return dec;
}

11. Perfect number
#include <stdio.h>
 int main()
{
    int num, rem, sum = 0, i;
    printf("Enter a Number:\n");
    scanf("%d", &num);
    for (i = 1; i <= (num - 1); i++)
    {
        rem = num % i;
if (rem == 0)
        {
            sum = sum + i;
        }
    }
    if (sum == num)
        printf("It is a perfect number");
    else
        printf("It is not a perfect number");
    return 0;
}

12. Armstrong number
#include<stdio.h>
int main()
{
   int num,n1,sum=0,rem;
   printf("\nEnter a number:");
   scanf("%d",&num);
   n1 = num;
   while (num != 0)
   {
      rem = num % 10;
      sum = sum + (rem*rem*rem);
      num = num / 10;
   }
   if(n1 == sum)
      printf("\n%d is an Armstrong Number",n1);
   else
      printf("\n%d is not an Armstrong Number",n1);
   return(0);
}

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

14. Prime number or not
#include <stdio.h> 
int main() 
{
  int num, i, j = 0;
  printf("Enter a number:");
  scanf("%d", &num);
  for (i = 1; i <= num; i++) {
      if (num % i == 0) {
         j++;
      }
  }
  if (j == 2) {
  printf("It is a Prime number");
  }
  else {
  printf("It is not a Prime number");
  }
  return 0;    
}

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





Post a Comment

0 Comments