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 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 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;
}
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;
}
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();
}
}
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;
}
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);
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us