C language interview questions solution for freshers beginners placement tricky good pointers answers explanation operators data types arrays structures functions recursion preprocessors looping file handling strings switch case if else printf advance linux objective mcq faq online written test prime numbers Armstrong Fibonacci series factorial palindrome code programs examples on c++ tutorials and pdf

First take 2's complement of -3 which will be as: 3 = 0000 0011 // binary value of 3 = 1111 1100 // take 1's complement i.e invert 0's & 1's 2's complement = 1111 1101 // add 1

We have got result for -3. Now operation for ~ (1's complement is pending). We know in 1's complement 0's will replaced by 1's & 1's will be replaced by 0's as we did above in second step.

Therefore 1's complement of 1111 1101 will be = 0000 0010 Result = 2

#define SWAP(x,y) x^=y^=x^=y

ReplyDeletewould serve

Here is couple of more way to swap two numbers without using temp variable in Java

DeleteHi trinath your this program do my help & made my image in class is a intelligenc boy so thenku very much

Deleteone more way

Delete#include

#include

int main( )

{

static int a=2,b=5;

a=a*b;

b=a/b;

a=a/b;

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

}

hi TRINATH SOMAROUTHU

ReplyDeleteThank you for posting another solution

#define swap(x,y) x^=y^=x^=y

void main()

{

int a=10,b=20;

swap(a,b);

clrscr();

printf("%d %d" ,a,b);

getch();

}

output: 20 10

void main()

ReplyDelete{

int a,b,c;

clrscr();

printf("\n Enter two no ");

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

printf("\n Before Swaping value of a and b %d %d",a,b);

c=a;

a=b;

b=c;

printf("\n After Swaping value of a and b %d %d",a,b);

getch();

}

its using third var...

Deleteread the question first. we don't have to use third variable.

Deleteif do not use third variable then

DeleteTHANKS ,THANKS A LOT

ReplyDeletegood job..

ReplyDeleteplease solve the problem

ReplyDeleteint a=~-3;

actually i do,not understand about ~ operator that how exactly it works .please solve step by step

First take 2's complement of -3 which will be as:

Delete3

= 0000 0011 // binary value of 3

= 1111 1100 // take 1's complement i.e invert 0's & 1's

2's complement = 1111 1101 // add 1

We have got result for -3. Now operation for ~ (1's complement is pending). We know in 1's complement 0's will replaced by 1's & 1's will be replaced by 0's as we did above in second step.

Therefore 1's complement of 1111 1101 will be =

0000 0010

Result = 2

process 4 is not working

ReplyDeleteerror is 'wrong type argument to bit-complement' in float value

can anybody explain in detail how a=a+b-(b=a); will work out for swapping

ReplyDeletea=a+b-(b=a)

Deletenow since value of a will be assigned to b

so equation will be

a=a+b-(a)

a=b

learn c programming and c graphics from programmingcampus

ReplyDeletewhere you can get best online tutorials which make simple and easy to your learning.

a=a+b

ReplyDeletea=a-b

b=a-b

thank u very much. this is the best solution without using third variable. simple and short

Deletethe right order is a=a+b,b=a-b,a=a-b

Deleteyupp...its correct order..(y)

Deleteu r most welcome

ReplyDeleteHI can u please help me to solve the following

ReplyDeleteSwap the values in 2 variBles using a third variAble?? Plzzzzz rply its urgent

Process 1 & 5 are approx same.

ReplyDeleteI have got another simple solution for this.

ReplyDeleteA=A*B

B=A/B

A=A/B

Swaping done.

@Ashok Natarajan this is also correct when given both values some numbers.. if your give A=0,B=4 that time floating exception will happen..

DeleteSwapping of two numbers can be done in 5 methods u can see in following url...

ReplyDeletewww.programmerschat.blogspot.in

void main()

ReplyDelete{

int a,b;

clrscr();

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

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

printf("\n Before Swaping value of a and b %d %d",a,b);

b=a+b-(a=b);

printf("\n After Swaping value of a and b %d %d",a,b);

getch();

}

Very Nice Article helps a lot ...

ReplyDeleteYou can also find 6 type of logic to swap two number here check one it will be helpful ...

Swapping of two numbers

This comment has been removed by the author.

ReplyDeleteprocess 4 is not working. I things it's

ReplyDeleteb= b - ~a - 1;

a= a + ~b +1;

what u say?

How to swap 2 variable using only assignment operator and no other operator in C

ReplyDeleteNice Examples... visit java examples with solution java examples

ReplyDelete