C Programming Exercise - Program to Swap 2 Numbers Without Using Temporary, Third Variable

preview_player
Показать описание
In this C programming language video tutorial / lecture for beginners, you will learn how to swap 2 numbers without using third or temporary variable in detail with example.

Here in this tutorial i have explained this program using c and logic is the same for any other programming language.

Learn Programming in HINDI at our youtube channel

Catch us on SocialMedia
Рекомендации по теме
Комментарии
Автор

Faster aternative.
a ^= b;
b ^= a;
a ^= b;
Remember that ^ is an xor operator, not an exponentitation (power) operator.

rodrigoqteixeira
Автор

Here's a better, faster, way:
*a=a+b-(b=a)*
works with numbers as long as they are the same type, and with characters.

diogoxp
Автор

hello, may you please help me write a C program in functions
1. by refrence
2. By value

desteriusanami
Автор

Sir 2135 out 5132 hota h to program kyasay bnayegay

priyankasinghaniya
Автор

which terminal is this?? it is not devcpp which u used in previous videos.

Ketan
Автор

Here's an alternative method using xor: a ^= b ^= a ^= b;

mitchelltaylor
Автор

why don't we swap a, b to b, a in the printf statements?

ahammadjassim
Автор

int a, b;
printf(" Enter Two values : ");
scanf(" %d %d ", &a, &b);
printf(" Before swap a = %d and b = %d ", a, b );

a=a+b;
b=a-b;
a=a-b;
printf(" After swap a = %d and b = %d ", a, b );

cant run

h.m.anwarhossainanu
Автор

This method works only if 2 number a + b is not overflow.
It's better to use the XOR method.
A ^= B
B ^= A
A ^= B
or combine them A ^= B ^= A ^= B

Dminicn
Автор

You don't have to fake an American accent

udita_bhaumik