answersLogoWhite

0

With one variable swap the number using c language?

Updated: 8/17/2019
User Avatar

Wiki User

14y ago

Best Answer
includeinclude
main()
{
int a,b;
clrscr();
printf("enter the value of a & b\n");
scanf("%d%d,&a,&b");
a=a+b;
b=a-b;
a=a-b;
printf(" value of a is %d & b is %d after swapping");
getch();
}
User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: With one variable swap the number using c language?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How you can interchange two integer values using swap variable in c language?

t = a; a = b; b = t; // t is a third integer variable (swap variable) But here's a way without a swap variable, given as as a macro in C: #define SWAP(a,b) { if (a!=b) { a^=b; b^=a; a^=b; }} // Swap macro by XOR Once you define it, you can say swap(x,y) to swap x and y. The numbers kind of flow through each other and end up swapped.


Swapping of number with using third variable?

To swap two numbers N1 and N2, using a third variable T... T = N1; N1 = N2; N2 = T;


Swap logic without using third variable?

a ^= b; b ^= a; a ^= b;


Swapping without third variable?

There are two ways in which you can swap without a third variable. 1. Using xor operation swap( int *a, int *b) { *a = *a ^ *b; *b = *a ^ *b; *a = *a ^ *b; } 2. Using addition and subtraction swap( int *a, int *b) { *a = *a + *b; *b = *a - *b; *a = *a - *b; } }


How do you swap variables in PHP?

By using a third temporary variable. $tmp = $a; $a = $b; $b = $tmp;


What is swap in c plus plus programming language?

It is not a reserved word, so can be an identifier (name of a type/variable/function).


How would you write a program to swap the values of character variable using call by address?

void swap (int* a, int* b) { if (!a !b) return; // can't swap a pointer to null *a^=*b^=*a^=*b; }


How develop c program that swap two number with out using temporary variable?

void main() { int a=2,b=5; b=a+b; a=b-a; b=b-a; getch(); }


How do you write a program in C plus plus plus plus How do you write a program in C to swap two variables without using the third oneo swap two variables without using the third one?

To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;


Swapping of 2 numbers using java language?

Use a temporary variable. Example (swap variables "a" and "b"): int a = 5; int b = 10; // Swap the variables int temp; temp = a; a = b; b = temp; System.out.println("a = " + a); System.out.println("b = " + b);Use a temporary variable. Example (swap variables "a" and "b"): int a = 5; int b = 10; // Swap the variables int temp; temp = a; a = b; b = temp; System.out.println("a = " + a); System.out.println("b = " + b);Use a temporary variable. Example (swap variables "a" and "b"): int a = 5; int b = 10; // Swap the variables int temp; temp = a; a = b; b = temp; System.out.println("a = " + a); System.out.println("b = " + b);Use a temporary variable. Example (swap variables "a" and "b"): int a = 5; int b = 10; // Swap the variables int temp; temp = a; a = b; b = temp; System.out.println("a = " + a); System.out.println("b = " + b);


What is the Algorithm for exchanging values of two variables?

There are three primary algorithms to exchange the values of two variables. Exchange with Temporary Variable temp = a; a = b; b = temp; Exchange Without Temporary Variable Using Exclusive Or a = a ^ b; b = b ^ a; a = a ^ b; Exchange Without Temporary Variable Using Arithmetic a = a + b; b = b - a; a = a - b;


Swapping of two variables withou using temp variable?

example: x = x-y; y = y-x; x = y-x; <><><> It is not possible to swap two variables without using a temp variable. The code in the answer above, while clever, does not swap the variables. It will exchange the variable values for certain values of x and y, e.g. when x and y are small integers. If x and y are other values, such as strings, pointers, Infinity, NaN (not a number), floating point, or near the limits of the representation (causing over/underflow) then the code will not "swap" the values. <---> Note: there is absolutely no point in swapping two variables without using temopral variable, it's just a typical homework question, already asked here countless times. Another variation: a ^= b; b ^= a; a ^= b;