Friday, 2024-03-29, 9:49 AM
ebooks Programming Computer Science
Welcome Guest | RSS
Site menu
Section categories
My articles [23]
Main » Articles » My articles

how to swap two numbers without using third variable
Swapping of two numbers in c

#include <stdio.h>
 
int main()
{
   int x, y, temp;
 
   printf("Enter the value of x and y\n");
   scanf("%d%d", &x, &y);
 
   printf("Before Swapping\nx = %d\ny = %d\n",x,y);
 
   temp = x;
   x = y;
   y = temp;
 
   printf("After Swapping\nx = %d\ny = %d\n",x,y);
 
   return 0;
}
Swapping of two numbers without third variable
You can also swap two numbers without using temp or temporary or third variable. In that case c program will be as shown :-

#include <stdio.h>
 
int main()
{
   int a, b;
 
   printf("Enter two integers to swap\n");
   scanf("%d%d", &a, &b);
 
   a = a + b;
   b = a - b;
   a = a - b;
 
   printf("a = %d\nb = %d\n",a,b);
   return 0;
}
To understand above logic simply choose a as 7 and b as 9 and then do what is written in program. You can choose any other combination of numbers as well. Sometimes it's a good way to understand a program.

Swap two numbers using pointers
#include <stdio.h>
 
int main()
{
   int x, y, *a, *b, temp;
 
   printf("Enter the value of x and y\n");
   scanf("%d%d", &x, &y);
 
   printf("Before Swapping\nx = %d\ny = %d\n", x, y);
 
   a = &x;
   b = &y;
 
   temp = *b;
   *b = *a;
   *a = temp;
 
   printf("After Swapping\nx = %d\ny = %d\n", x, y);
 
   return 0;
}
Swapping numbers using call by reference
In this method we will make a function to swap numbers.

#include <stdio.h>
 
void swap(int*, int*);
 
int main()
{
   int x, y;
 
   printf("Enter the value of x and y\n");
   scanf("%d%d",&x,&y);
 
   printf("Before Swapping\nx = %d\ny = %d\n", x, y);
 
   swap(&x, &y); 
 
   printf("After Swapping\nx = %d\ny = %d\n", x, y);
 
   return 0;
}
 
void swap(int *a, int *b)
{
   int temp;
 
   temp = *b;
   *b = *a;
   *a = temp;   
}
C programming code to swap using bitwise XOR
#include <stdio.h>
 
int main()
{
  int x, y;
 
  scanf("%d%d", &x, &y);
 
  printf("x = %d\ny = %d\n", x, y);
 
  x = x ^ y;
  y = x ^ y;
  x = x ^ y;
 
  printf("x = %d\ny = %d\n", x, y);
 
  return 0;
}
The code to swap two variables using macro expansions

Code: C
#include<stdio.h>
#include<conio.h>
#define SWAPE(x,y) int t;t=x;x=y;y=t;
main()
{
    int a,b;
   
    printf("\n Enter two number");
    scanf("%d%d",&a,&b);
    printf("\n Before swaping the Value of a=%d and b=%d",a,b);
    SWAPE(a,b);
    printf("\n After swap value of a=%d and b=%d",a,b);
    return 0;
}

//Code for swapping two numbers without using temp variable
Code: CPP
#include <iostream>
using namespace std;
int main ()
{
  int a = 10;
  int b = 5;

  cout << "before swap: a = " << a << " b = " << b << endl;
 
  //Swapping numbers without using a temp var : method1
  a = a ^ b;
  b = b ^ a;
  a = a ^ b;

  cout << "after swap using method1: a = " << a << " b = " << b << endl;

  //Swapping numbers without using a temp var : method2
  a = a + b;
  b = a - b;
  a = a - b;
  cout << "after swap using method2: a = " << a << " b = " << b << endl;

  return 0;
}
the below logic also works as follows for swapping of nos without external variable introduction
Code:
main()
{ 
 int a,b;
 printf("enter the nos");
 scanf("%d %d",&a,&b);
 printf("nos before swapping a=%d ,b=%d", a,b);
 a=a*b;
 b=a/b;
 a=a/b;
 printf("nos after swapping a=%d ,b=%d", a,b);
}
Category: My articles | Added by: Sumrat (2012-08-23)
Views: 15987 | Comments: 1 | Rating: 1.0/2
Total comments: 1
1 Anubhav  
0
http://javarevisited.blogspot.com.br/2013/02/swap-two-numbers-without-third-temp-variable-java-program-example-tutorial.html

Name *:
Email *:
Code *:
Our poll
Rate my site
Total of answers: 164
Statistics

Total online: 1
Guests: 1
Users: 0
Login form