EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

Write a program to exchange two variables in C without third variable or stack or cpu register.

Source Code

/* xchg.cpp : Exchange two variables in C without third variable or stack or cpu register / */
#include<stdio.h>
int main(int argc, char* argv[])
{
  int a, b;
  a = 2;
  b = 3;
  printf("Enter value for a = ");
10    scanf("%d", &a);
11 
12    printf("Enter value for b = ");
13    scanf("%d", &b);
14 
15    printf("Doing exchange with XCHG\n");
16    _asm{
17     mov eax, a
18     xchg eax, b
19     mov a, eax
20    }
21    printf("Values after XCHG a = %d, b = %d\n", a, b);
22    return 0;
23  }
24 
25 

Find More from our code collection
Armstrong number, binary number to a decimal number, bubble sort, decimal number to binary number, factorial of the given number factors, fibonacci numbers, HCF and LCM, matrix, mergesort, salary of the employee. palindrome, quadratic equation, star patterns, series etc. and much more...
#Return to Example Source Code