How to swap two values using a single variable ?

condition: Not to use Array and Pointer ?

Answers were Sorted based on User's Feedback



How to swap two values using a single variable ? condition: Not to use Array and Pointer ?..

Answer / vignesh1988i

GOOD MORNING,
swapping itself means that there must be minimum 2
variables ... if there is the way for above , that one is
not called as swapping......

Is This Answer Correct ?    17 Yes 2 No

How to swap two values using a single variable ? condition: Not to use Array and Pointer ?..

Answer / ankit anupam

well i think this was wht u were lookin for
#include<stdio.h>
int main()
{
int a, b;
printf("Enter two no");
scanf("%d%d",&a,&b);
printf("a=%d nd b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("a=%d nd b=%d",a,b);
return 0;
}

Is This Answer Correct ?    16 Yes 3 No

How to swap two values using a single variable ? condition: Not to use Array and Pointer ?..

Answer / jp

The trick is to use register variables. I have written a sample code in C. There might be some cosmetic works for indention, plz don't bother ;)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#include <dos.h>
#include <stdio.h>
union REGS ax_regs;

/*
Title : Using Registers To Swap Values

Description : Declaring a single variable and using it to swap two values

Author : Jayapriyan VITTOBANE
Turbo C++ V 3.0
*/

void main()
{
clrscr();
ax_regs.h.ah=2;
ax_regs.h.al=3;
printf("\n\n\n\n\n\t\t\tBefore Swapping");
printf("\n\n\n\t\tFirst Value:%5d",ax_regs.h.ah);
printf("\n\n\n\t\tSecond Value:%5d",ax_regs.h.al);
ax_regs.h.ah=ax_regs.h.ah^ax_regs.h.al;
ax_regs.h.al=ax_regs.h.ah^ax_regs.h.al;
ax_regs.h.ah=ax_regs.h.ah^ax_regs.h.al;
printf("\n\n\n\n\n\n\n\n\n\n\t\t\tAfter Swapping");
printf("\n\n\n\t\tFirst Value:%5d",ax_regs.h.ah);
printf("\n\n\n\t\tSecond Value:%5d",ax_regs.h.al);
getch();
}

Is This Answer Correct ?    5 Yes 0 No

How to swap two values using a single variable ? condition: Not to use Array and Pointer ?..

Answer / aditya

main(){
int a=10,b=20;
printf("%d%d",a,b);
a^=b,b^=a,a^=b;
printf("%d%d",a,b);

}

Is This Answer Correct ?    3 Yes 1 No

How to swap two values using a single variable ? condition: Not to use Array and Pointer ?..

Answer / jyotsna

let take a single variable 'x'
a=3
b=2
hence
x=a;
a=b;
b=x;
swapping is occured now a=2 and b=3

Is This Answer Correct ?    10 Yes 12 No

How to swap two values using a single variable ? condition: Not to use Array and Pointer ?..

Answer / guest

use actual and formal function

Is This Answer Correct ?    2 Yes 9 No

Post New Answer

More C Interview Questions

void main() { char c; while(c=getchar()!='\n') printf("%d",c); } o/p=11 why?

8 Answers   Wipro,


how to write hello word without using semicolon at the end?

6 Answers   Accenture,


What is the purpose of clrscr () printf () and getch ()?

0 Answers  


When do you not use the keyword 'return' when defining a function a) Always b) Never c) When the function returns void d) dfd

0 Answers  


main() { FILE *fs; char c[10]; fs = fopen(“source.txt”, ”r”); /* source.txt exists and contains “Vector Institute” */ fseek(fs,0,SEEK_END); fseek(fs,-3L,SEEK_CUR); fgets(c,5,fs); puts(c); }

1 Answers   Vector,






What is #line?

0 Answers  


What is the use of function in c?

0 Answers  


Explain how can you avoid including a header more than once?

0 Answers  


What is scanf () in c?

0 Answers  


whether itis a structured language?

1 Answers   Microsoft,


without using arithmatic operator solve which number is greater??????????

1 Answers   Accenture,


a=5 a=a++/++a

14 Answers   Bhel,


Categories