Try it in C Language

 Write a C program to swap two variables without using a third variable.

This is one of the very common C interview questions. It can be solved in a total of five steps. For this, I have considered two variables as a and b, such that a = 5 and b = 10.

#include<stdio.h>
int main(){
    int a=5,b=10;

    a=b+a;
    b=a-b;
    a=a-b;
    printf("a= %d  b=  %d",a,b);

    a=5;
    b=10;
    a=a+b-(b=a);
    printf("\na= %d  b=  %d",a,b);

    a=5;
    b=10;
    a=a^b;
    b=a^b;
    a=b^a;
    printf("\na= %d  b=  %d",a,b);
   
    a=5;
    b=10;
    a=b-~a-1;
    b=a+~b+1;
    a=a+~b+1;
    printf("\na= %d  b=  %d",a,b);
   
    a=5,
    b=10;
    a=b+a,b=a-b,a=a-b;
    printf("\na= %d  b=  %d",a,b);
    return 0;
}
2) What is Pointer?
int main()
{
    int x=10;
    int far *ptr;
    ptr=&x;
    printf("%d",sizeof ptr);
    return 0;
}
3)
struct  ABC{
    int a;
    float b;
    char c;
};
int main()
{
    struct ABC *ptr=(struct ABC *)0;
    ptr++;
    printf("Size of structure is: %d",*ptr);
    return 0;
}
4) #include< stdio.h>
#include< conio.h>
void main()
{
    int i,j=2,num;
    clrscr();
    printf("Enter any number: ");
    scanf("%d",&num);
    printf("Next Prime number: ");
    for(i=num+1; i< 3000; i++)
    {
        for(j=2; j< i; j++)
        {
            if(i %j==0)
            {
                break;
            } // if
        } // for
        if(i==j || i==1)
        {
            printf("%d\t",i);
            break;
        }// if
    }// outer for getch();
}
5)Write a C program to solve the “Tower of Hanoi” question without using recursion.

Comments