Answers for "swap without using third variable"

C
1

swapping of two numbers without using third variable in shell script

#!/bin/bash
echo "enter first number"
read a
echo "enter second number"
read b
echo "a before swapping is $a and b is $b"
#swapping
a=$((a+b))
b=$((a - b))
a=$((a-b))
echo "a after swapping is  $a and b is $b"
Posted by: Guest on September-03-2020
2

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("\nAfter swap a=%d b=%d",a,b);    
return 0;  
}   
Posted by: Guest on March-03-2021
4

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)
Posted by: Guest on May-16-2021
0

swap variables without temp

import java.*; 
  
class noTemp { 
  
    public static void main(String a[]) 
    { 
        int x = 10; 
        int y = 5; 
        x = x + y; 
        y = x - y; 
        x = x - y; 
        System.out.println(x , y); 
    } 
}
Posted by: Guest on January-22-2021
0

swap 2 integers without using temporary variable

using System;

class MainClass {
  public static void Main (string[] args) {
    int num1 = int.Parse(Console.ReadLine());
    int num2 = int.Parse(Console.ReadLine());
      num1 = num1 + num2; 
      num2 = num1 - num2; 
      num1 = num1 - num2; 
      Console.WriteLine("After swapping: num1 = "+ num1 + ", num2 = " + num2); 
    Console.ReadLine();
  }
}
Posted by: Guest on September-23-2020
2

how to swap 2 numbers without 3rd variable

int a = 3;
int b = 5;
a = b*a;
b = a/b
a = a/b
Posted by: Guest on April-02-2021

Code answers related to "swap without using third variable"

Code answers related to "C"

Browse Popular Code Answers by Language