swap without using third variable

Solutions on MaxInterview for swap without using third variable by the best coders in the world

showing results for - "swap without using third variable"
Noah
05 Jun 2018
1#!/bin/bash
2echo "enter first number"
3read a
4echo "enter second number"
5read b
6echo "a before swapping is $a and b is $b"
7#swapping
8a=$((a+b))
9b=$((a - b))
10a=$((a-b))
11echo "a after swapping is  $a and b is $b"
12
Tilio
24 Jan 2019
1a=10;
2b=20;
3a=a+b;//a=30 (10+20)    
4b=a-b;//b=10 (30-20)    
5a=a-b;//a=20 (30-10)    
Juan Pablo
18 Jan 2018
1// SWAPPING WITHOUT USING THIRD VARIABLE
2#include<stdio.h>  
3 int main()    
4{    
5int a=10, b=20;      
6printf("Before swap a=%d b=%d",a,b);      
7a=a+b;//a=30 (10+20)    
8b=a-b;//b=10 (30-20)    
9a=a-b;//a=20 (30-10)    
10printf("\nAfter swap a=%d b=%d",a,b);    
11return 0;  
12}   
Fatma
06 Jun 2017
1import java.*; 
2  
3class noTemp { 
4  
5    public static void main(String a[]) 
6    { 
7        int x = 10; 
8        int y = 5; 
9        x = x + y; 
10        y = x - y; 
11        x = x - y; 
12        System.out.println(x , y); 
13    } 
14} 
Jerald
20 Jan 2019
1using System;
2
3class MainClass {
4  public static void Main (string[] args) {
5    int num1 = int.Parse(Console.ReadLine());
6    int num2 = int.Parse(Console.ReadLine());
7      num1 = num1 + num2; 
8      num2 = num1 - num2; 
9      num1 = num1 - num2; 
10      Console.WriteLine("After swapping: num1 = "+ num1 + ", num2 = " + num2); 
11    Console.ReadLine();
12  }
13}
Max
13 Nov 2018
1// SWAP USING THIRD VARIBLE
2#include <stdio.h> 
3int main()
4{
5int var1, var2, temp; 
6printf("Enter two integersn");
7scanf("%d%d", &var1, &var2);
8printf("Before SwappingnFirst variable = %dnSecond variable = %dn", var1, var2);
9temp = var1;
10var1 = var2;
11var2 = temp;
12printf("After SwappingnFirst variable = %dnSecond variable = %dn", var1, var2);
13return 0;
14}
similar questions
queries leading to this page
swap without using third variable