DekGenius.com
C
swap two numbers without third variable
int a=4;
int b=5;
a=a+b; // 4 + 5 = 9
b=a-b; // 9 - 5 = 4
a=a-b; // 9 - 4 = 5
swap without using third variable
// SWAPPING WITHOUT USING THIRD VARIABLE
#include<stdio.h>
int main()
{
int a=10, b=20;
printf("Before swap a=%d b=%d",a,b);
a=a+b;//a=30 (10+20)
b=a-b;//b=10 (30-20)
a=a-b;//a=20 (30-10)
printf("
After swap a=%d b=%d",a,b);
return 0;
}
swap 2 numbers without using 3rd variable
a=10;
b=20;
a=a+b;//a=30 (10+20)
b=a-b;//b=10 (30-20)
a=a-b;//a=20 (30-10)
swap of two numbers without using third variable
a=20;
b=40;
a=a+b;
b=a-b;
a=a-b;
how to swap 2 numbers without 3rd variable
int a = 3;
int b = 5;
a = b*a;
b = a/b
a = a/b
swapping data of two variable without third one
X= 25 (First number), Y= 23 (second number)
Swapping Logic:
X = X + Y = 25 +23 = 48
Y = X - Y = 48 - 23 = 25
X = X -Y = 48 - 25 = 23
and the numbers are swapped as X =23 and Y =25.
Swapping Two Numbers Using Third Variable
public static void main(String[] args) {
int x = 10;
int y = 20;
int temp;
temp = x;
x = y;
y = temp;
System.out.println("x:"+x +" y:" + y);
}
Swapping Variables
x = 10
y = 11
x, y = y, x
"Swapping by simultaneously creating a tuple and unpacking it"
print(x, y)
swap two numbers without using third variable
var a=window.prompt('enter a number')var b=window.prompt('enter a number')var a=a+b;var b=a-b;var a=a-b;console.log ("value of a is",a);console.log('value of b is',b);
Swapping Two Numbers without Using Third Variable
public static void main(String[] args) {
int x = 10;
int y = 20;
x=x+y;
y=x-y;
x=x-y;
System.out.println("x:"+x+" y:"+y);
}
Swapping two variable without initializing the third variable
#include <iostream>
using namespace std;
int main()
{
int first_number = 10, last_number = 20;
first_number = first_number + last_number;
last_number = first_number - last_number;
first_number = first_number - last_number;
cout << first_number << " " << last_number << endl;
return 0;
}
© 2022 Copyright:
DekGenius.com