Answers for "swap 2 numbers without using 3rd variable"

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
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
1

Write a program to show swap of two numbers 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 February-02-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

swap using third variable

// SWAP USING THIRD VARIBLE
#include <stdio.h> 
int main()
{
int var1, var2, temp; 
printf("Enter two integersn");
scanf("%d%d", &var1, &var2);
printf("Before SwappingnFirst variable = %dnSecond variable = %dn", var1, var2);
temp = var1;
var1 = var2;
var2 = temp;
printf("After SwappingnFirst variable = %dnSecond variable = %dn", var1, var2);
return 0;
}
Posted by: Guest on March-03-2021
0

swap 2 numbers without using 3rd variable

//Swapping Using Addition and Subtraction(+ & -)
void swapping(int x, int y)
{
    x = x + y;    //1
    y = x - y;    //2
    x = x - y;    //3
    printf("The values of a and b AFTER swapping are a = %d & b = %d \n", x, y);
   
}

//Swapping Using Multiplication and Division(* & /)
void swapping(int x, int y)
{
     x = x * y;   //1
     y = x / y;   //2
     x = x / y;   //3
    printf("The values of a and b AFTER swapping are a = %d & b = %d \n", x, y);
   
}

//Swapping Using Bitwise XOR
void swapping(int x, int y)
{
    x = x ^ y;
    y = x ^ y;
    x = x ^ y;
    printf("The values of a and b AFTER swapping are a = %d & b = %d \n", x, y);
   
}
Posted by: Guest on September-22-2021

Code answers related to "swap 2 numbers without using 3rd variable"

Browse Popular Code Answers by Language