C interview programs – swap numbers, palindrome or not


c interview questions Here are more of the frequently asked C language programs in an interview. Each of the questions have been answered with code and , output and explanation. Question.31(swapping using temp variable) is a good starter in an IT interview. Question.32(swapping without temp variable) takes the interveiw further to test the candidate’s usage of variables. Question.33(swapping using bitwise operations) takes swapping program to the next level to test the knowledge of bitwise operators in C language. Question.34, 35 are about palindrome programs in C language. They are two of the top ten interview questions asked in a C language interview. See all C interview questions in this blog in one page.

  1. Write a program to swap two numbers using a temporary variable.
  2. Swapping interchanges the values of two given variables.
    Logic:

               step1: temp=x;
               step2: x=y;
               step3: y=temp;

    Example:

    if x=5 and y=8, consider a temporary variable temp.
               step1: temp=x=5;
               step2: x=y=8;
               step3: y=temp=5;
    
    Thus the values of the variables x and y are interchanged.

    Program:

    #include <stdio.h>
    int main() {
     int a, b, temp;
     printf("Enter the value of a and b: \n");
     scanf("%d %d", &a, &b);
     printf("Before swapping a=%d, b=%d \n", a, b);
     /*Swapping logic */
     temp = a;
     a = b;
     b = temp;
     printf("After swapping a=%d, b=%d", a, b);
     return 0;
    }

    Download Code

    Output:

    Enter the values of a and b: 2 3
    Before swapping a=2, b=3
    After swapping a=3, b=2

    Back to top

  3. Write a program to swap two numbers without using a temporary variable.
  4. Swapping interchanges the values of two given variables.
    Logic:

              step1: x=x+y;
              step2: y=x-y;
              step3: x=x-y;

    Example:

    if x=7 and y=4
        step1: x=7+4=11;
        step2: y=11-4=7;
        step3: x=11-7=4;
    Thus the values of the variables x and y are interchanged.

    Program:

    #include <stdio.h>
    int main() {
     int a, b;
     printf("Enter values of a and b: \n");
     scanf("%d %d", &a, &b);
     printf("Before swapping a=%d, b=%d\n", a,b);
     /*Swapping logic */
     a = a + b;
     b = a - b;
     a = a - b;
     printf("After swapping a=%d b=%d\n", a, b);
     return 0;
    }

    Download Code

    Output:

    Enter values of a and b: 2 3
    Before swapping a=2, b=3
    The values after swapping are a=3 b=2

    Back to top

  5. How to swap two numbers using bitwise operators?
  6. Program:

    #include <stdio.h>
    int main() {
     int i = 65;
     int k = 120;
     printf("\n value of i=%d k=%d before swapping", i, k);
     i = i ^ k;
     k = i ^ k;
     i = i ^ k;
     printf("\n value of i=%d k=%d after swapping", i, k);
     return 0;
    }

    Download Code

    Explanation:

    i = 65; binary equivalent of 65 is 0100 0001
    k = 120; binary equivalent of 120 is 0111 1000
    i = i^k;
    i...0100 0001
    k...0111 1000
    ---------
    val of i = 0011 1001
    ---------
    
    k = i^k
    i...0011 1001
    k...0111 1000
    ---------
    val of k = 0100 0001 binary equivalent of this is 65
    ---------(that is the initial value of i)
    
    i = i^k
    i...0011 1001
    k...0100 0001
    ---------
    val of i = 0111 1000 binary equivalent of this is 120
    ---------(that is the initial value of k)

    Back to top

  7. Write a program to check whether a given number is a palindromic number.
  8. If a number, which when read in both forward and backward way is same, then such a number is called a palindrome number.

    Program:

    #include <stdio.h>
    int main() {
     int n, n1, rev = 0, rem;
     printf("Enter any number: \n");
     scanf("%d", &n);
     n1 = n;
     /* logic */
     while (n > 0){
      rem = n % 10;
      rev = rev * 10 + rem;
      n = n / 10;
     }
     if (n1 == rev){
      printf("Given number is a palindromic number");
     }
     else{
      printf("Given number is not a palindromic number");
     }
     return 0;
    }

    Download Code

    Output:

    Enter any number: 121
    Given number is a palindrome
    Explanation with an example:

    consider a number n=121, reverse=0, remainder;
        number=121
        now the while loop is executed /* the condition (n>0) is satisfied */
    
    /* calculate remainder */
        remainder of 121 divided by 10=(121%10)=1;
    
      now  reverse=(reverse*10)+remainder
                  =(0*10)+1     /* we have initialized reverse=0 */
                  =1
    
           number=number/10
                 =121/10
                 =12
    
        now the number is 12, greater than 0. The above process is repeated for number=12.
             remainder=12%10=2;
             reverse=(1*10)+2=12;
             number=12/10=1;
    
       now the number is 1, greater than 0. The above process is repeated for number=1.
             remainder=1%10=1;
             reverse=(12*10)+1=121;
             number=1/10 /* the condition n>0 is not satisfied,control leaves the while loop */

    Program stops here. The given number=121 equals the reverse of the number. Thus the given number is a palindrome number.
    Back to top

  9. Write a program to check whether a given string is a palindrome.
  10. Palindrome is a string, which when read in both forward and backward way is same.
    Example: radar, madam, pop, lol, rubber, etc.,

    Program:

    #include <stdio.h>
    #include <string.h>
    int main() {
     char string1[20];
     int i, length;
     int flag = 0;
     printf("Enter a string: \n");
     scanf("%s", string1);
    
     length = strlen(string1);
     for(i=0;i < length ;i++){
      if(string1[i] != string1[length-i-1]){
       flag = 1;
       break;
      }
     }
    
     if (flag) {
      printf("%s is not a palindrome\n", string1);
     }
     else {
      printf("%s is a palindrome\n", string1);
     }
     return 0;
    }

    Download Code

    Output:

    Enter a string: radar
    “radar” is a palindrome

    Explanation with example:

    To check if a string is a palindrome or not, a string needs to be compared with the reverse of itself.
    
    Consider a palindrome string: "radar",
    ---------------------------
    index: 0 1 2 3 4
    value: r a d a r
    ---------------------------
    To compare it with the reverse of itself, the following logic is used:
    
    0th character in the char array, string1 is same as 4th character in the same string.
    1st character is same as 3rd character.
    2nd character is same as 2nd character.
    . . . .
    ith character is same as 'length-i-1'th character.
    
    If any one of the above condition fails, flag is set to true(1), which implies that the string is not a palindrome.
    By default, the value of flag is false(0). Hence, if all the conditions are satisfied, the string is a palindrome.

    Back to top


About the Author:  This post was written by Madhulika Reddy. You can connect with Madhulika on Orkut.



All comments of post - "C interview programs – swap numbers, palindrome or not":

:Haha! I'am the first! Yeh~

Thank you!

Add a Comment / Trackback url

  1. #2  Sridhar

  2. #1  Shiva

    can any1 tel me hw to swap 2 integr with temporary variable using FUNCTIONS.?

    09/02/23 05:17

Show all Show 5 so far Close all

Comment begin from here or jump up!