How do we swap or interchange any 2 numbers without using
Temporary variable...Anybody can pls answer it.. Thanks in
Advance
Answers were Sorted based on User's Feedback
Answer / manju
main()
{
int a=16,b=19;
a=a+b;
b=a-b;
a=a-b;
printf("a and b are:%d\t%d",a,b);
}
Output:
a and b are:19 16
Is This Answer Correct ? | 16 Yes | 3 No |
int x,y;
x =x-y;
y =y+x;
x =y-x;
or
int x,y;
x=x^y;
y=x^y;
x=x^y;
Is This Answer Correct ? | 11 Yes | 7 No |
Answer / srikanth
int x,y;
x =x-y;
y =y+x;
x =y-x;
or
int x,y;
x=x^y;
y=x^y;
x=x^y;
or
int x,y;
x*=xy;
y=x/y;
x=x/y;
Is This Answer Correct ? | 4 Yes | 1 No |
Answer / emperor of america
using bitwise operator works 100%:
x^=y;
y^=x;
x^=y;
using +/- works sometime, need to consider overflow.
Is This Answer Correct ? | 4 Yes | 2 No |
Answer / divya
#include<stdio.h>
main()
(
clrscr();
int a,b;
printf("enter a and b values");
scanf("%d \n %d",&a,&b);
printf("before swaping a value is:%d",a);
printf("before swaping b value is:%d",b);
a=a+b;
b=a-b;
a=a-b;
printf("after swaping a value is:%d",a);
printf("after swaping b value is:%d",b);
getch();
}
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / girish
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a,b;
clrscr();
printf("Enter the two values:");
scanf("%d%d",&a,&b);
swap(a,b);
pritnf("the swaping of given two values are:\na=%d,b=%
d",a,b);
getch();
}
Is This Answer Correct ? | 0 Yes | 3 No |
Answer / mangala pandi
Logically it is not possible. so your question is worng.
Is This Answer Correct ? | 1 Yes | 15 No |
what is diference between return 0 and return NULL??
What is s or c?
Explain the binary height balanced tree?
How we add our function in liabrary as liabrary function. Exp. we want use our int factorical(int); function as int pow(int,int); function working in math header file.
What are structures and unions? State differencves between them.
How to print all the 26 alphabets in this order in C. AbCdEfGh..... it should print dynamically from a to z and do not print this using pgm like this print("Ab......"); Use loops or anything to print all alphabets
What is data structure in c programming?
Is both getch() and getchar() functions are similar? if it is similar means why these two functions are used for same usage? if it is not similar means what is the difference?
how to print "hai" in c?
What is time complexity c?
Write a program which returns the first non repetitive character in the string?
Why does not use getgh(); and <conio.h> in c language.