C interview programs – swap numbers, palindrome or not

by Madhulika Reddy on October 7, 2008


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 interview 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, 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.


  • http://www.blogger.com/profile/17704213744873923566 Shiva

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

  • http://www.interviewmantra.net Sridhar
  • http://rgukt bhavani

    how to find out the GCD in c language?

  • http://wikipedia sunithasri

    programs are very good plz send anether programs ok by my momary increses good8 plz reply ok

  • p

    rubber does not appear a palindrome?

  • Rama

    very nice.

  • Rama

    Is it sarasa not a palindrome.why? plz reply me.

  • p

    Rama, if you think this tutorial very nice, you should understand how sarasa is not a palindrome. The first letter “s” differs from the last (6th) letter “a”. The second letter “a” differs from the last (5th) letter”s”. The third letter “r” differs from the last (4th) letter “a”. ..ok?

  • p

    Rama, if you think this tutorial very nice, you should understand how sarasa is not a palindrome. The first letter “s” differs from the last (6th) letter “a”. The second letter “a” differs from the (5th) letter”s”. The third letter “r” differs from the (4th) letter “a”. ..ok? (oops 5th and 4th letters are not last letters lol… :-p)

  • p

    Rama and others, have you realized why I mentioned rubber? The author of this article Madhulika Reddy said it here. Search this page with your browser if you doubt…

  • admin

    Dear p, Thanks for point out. Removed rubber from palindrome list.

  • pooja

    for palindrome string you can get the algo here

    http://www.gotaninterviewcall.com/forum/?p=547

  • http://omspeaks.com om

    pooja,,you have posted a dead link……can anyone tell to

  • pooja

    @Om I didnt get it ? wht does it mean.

  • Deepika

    how to swap three numbers???? this was the question asked to me in my interview session. please help me

  • Udit Rishu

    good

  • Red Srinu83

    please tell me how to write program onpalindrome with out using built in function

  • madhu

    thanq madhu….
    very useful 4 c++ students :)

  • Luckycherris

    xcellentt…..very much useful fo me

  • Rupinder32bhinder

    thanks….

  • Hello

    what?

  • Danielprakash Raj

    Another Method For Finding Palindrome  
    ************************************

    #include
    #include
    #include
    int main()
    {
            char *p,*q,*str=(char*)malloc(20);
            printf(“Enter the String:”);
            scanf(“%s”,str);
            p=str;
            q=str+(strlen(str)-1);
           while(p < q && *p++!=*q–) 
                                           // Check Middle Position and also check first and last character 
            {
                    printf("Not this word Palindrome!!!n");
                    exit(0);
            }
            printf("%p==%pn",p,q);
            printf("Yes this word Palindrome !!!n");
            return 0;
    }

  • rox

    om is a perfect psycho patient…have u take ur medicine properly?

  • rox

    beware of om….he is totally a sexfreek guy….even he oftenfy fuck his mother….more over om as well as his father is aids patient…beware of om..

Previous post:

Next post: