Showing posts with label C Interviews. Show all posts
Showing posts with label C Interviews. Show all posts

26.6.08

Swapping two integers without temporary variable.

We can do this thing in many ways, here are some.


int main()
{
int x, y;
printf("Enter x and y values \n");
scanf("%d %d", &x, &y);
printf("X=%d\t Y=%d\n", x, y);
printf("AFTER SWAP \n");

/* Swap using addition and subtraction */
x = x+y;/*Limitation is there addition should */
y = x-y;/* not over flow */
x = x-y;
printf("X=%d\t Y=%d\n", x, y);

/* Swap using multiplication and division */
x = x*y;/* Limitation is, Y should not be zero and*/
y = x/y;/* and x*y should not overflow */
x = x/y;
printf("X=%d\t Y=%d\n", x, y);

/* swap using exclusive OR */
x = x^y;
y = x^y;
x = x^y;
printf("X=%d\t Y=%d\n", x, y);

return 0;
}
Give the one line code to tell the given number is power of '2' or not.

The "C" code snippet is here:
  int main()
{
int num;

printf("Enter a number other than Zero \n");
scanf("%d", &num);

num&(num-1)?printf("Not a Power of 2 \n"):printf("Power of 2 \n");

return 0;
}

7.3.08

passing arguments

Passing arguments to function.

Let say
int fun ( int ) ;
is the function prototype.

Now function call was made like this.

fun(x++);

This is not good practice.
In the above call our intension is to pass the incremented value of 'x',
but it pass the value of x not x+1.The best practice is, fun(++x).

Code:

int main()
{
void test_fun(int);
int num=10;
test_fun(num++);
return 0;
}
void test_fun(int num)
{
printf("NUM=%d", num);
}

Output is : NUM=10

print 1 to 100 without using loop

Printing from 0 to 100 without using
any loop.

we can do this using recursion.

Code:
void print()
{

static int x;
if (100 == x )
return; /* or use exit call*/

printf("%d", x++);
print(x);

}

28.2.08

Swap the two numbers

This is the most frequently asked C question.

How to swap two numbers without using temporary variable.

There are many ways to do that, here are some.

Code snippets:
1)

int x, y; /* are the two numbers */
x = x+y;
y = x - y;
x = x - y;

2)
x = x*y;
y = x/y;
x = x/y;
3)
x = x ^ y;
y = x ^ y ;
x = x ^ y ;
NOTE: These logics have some limitations
Logic 1 has the Overflow problem when we add (x+y).
Logic 2 has the Overflow problem when we multiply ( x*y) and y should not be Zero.
Logic 3 has the same and its not works on the float numbers.

27.2.08

Even/Odd number

This is also one of the C Interview questions.

How to tell the given number is even or odd number in single line C code.

Code snippet:
int num;
num&1 ? printf("Odd Number\n") : printf("Even number\n");

Logic is every odd number will have least significant bit has 1
in binary representation of the number.

So when we do bitwise AND with number 1, obviously the
result is '1' for odd numbers and '0' for even numbers.

power of 2

This is one tricky bit usually asked in C interviews

How to tell the given number is Power of 2 or not in a one line 'C 'code.

There are many ways to do this, here is my logic.

Code snippet:

int num; /* holds the integer value */

num & (num-1) ? printf("Not Power of 2\n") : printf("Power of 2\n");