how to swap two integers 1 and 32767 without using third
variable
Answers were Sorted based on User's Feedback
Answer / riya ganguly
int a=1,b=32767;
a=a+b;
b=a-b;
a=a-b;
printf("a=%d,b=%d",a,b);
Is This Answer Correct ? | 17 Yes | 5 No |
Answer / mansi_engg
use unsigned before variable a and b in
a=1;
b=32767;
a=a+b;
b=a-b;
a=a-b;
bcoj 32767+1 =32768 which goes out of range of integer and
will be stored as -32768 which wil make the swapping
wrong.by using unsigned, addition will come in range 0-65536
and thus the process works.
Is This Answer Correct ? | 8 Yes | 2 No |
Answer / sharath kumar
As int max valur is 32767. If we increment it it becomes -32768, so its a wrong way to do
a=1; b=32767;
a=-a; b=-b;
a=a+b; //-32768 with in range
b=a-b; //-1
a=a-b; //-32767
printf("%d%d",-a,-b);
Is This Answer Correct ? | 4 Yes | 0 No |
Answer / g.sai lakshmi priyanka
void main()
{
int a,b;
a=32767,b=1;
a=a*b;
b=a/b;
a=a/b;
printf("%d %d",a,b);
}
EXPLAINATION:
a=32767*1=32767
b=32767/1=32767
a=32767/32767=1
a=1,b=32767
Is This Answer Correct ? | 3 Yes | 0 No |
Answer / vidhubala-j
int a=1
int b=32767
a^=b^=a
printf("%d %d",a,b);
Is This Answer Correct ? | 4 Yes | 3 No |
Answer / mani654mani
Int var1=1, var2=32767;
var1 = var1 + var2;
var1 = 1 + 32767 =32768;
var2 = var1 - var2;
var2 = 32768 - 32767 = 1;
var1 = var1 - var2;
var1 = 32768 - 1 = 32767;
So Now var1 = 32767 and var2 = 1
Is This Answer Correct ? | 0 Yes | 0 No |
What are types of functions?
which operator having highest precedence? a.)+ b.)++ c.)= d.)%
Difference between pass by reference and pass by value?
Explain what are preprocessor directives?
15.what is the disadvantage of using macros? 16.what is the self-referential structure? 17.can a union be self-referenced? 18.What is a pointer? 19.What is the Lvalue and Rvalue? 20.what is the difference between these initializations? 21.Char a[]=”string”; 22.Char *p=”literal”; 23.Does *p++ increment p, or what it points to?
is it possible to change the default calling convention in c ?
Write a program to implement queue.
if (i = 0)printf ("True"); elseprintf("False"); Under what conditions will the above print out the string "True" a) Never b) Always c) When the value of i is 0 d) all of the above
What does double pointer mean in c?
If you know then define #pragma?
what is the difference between global variable & static variable declared out side all the function in the file.
What is string in c language?