How to swap two values using a single variable ?
condition: Not to use Array and Pointer ?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
void main() { char c; while(c=getchar()!='\n') printf("%d",c); } o/p=11 why?
how to write hello word without using semicolon at the end?
What is the purpose of clrscr () printf () and getch ()?
When do you not use the keyword 'return' when defining a function a) Always b) Never c) When the function returns void d) dfd
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); }
What is #line?
What is the use of function in c?
Explain how can you avoid including a header more than once?
What is scanf () in c?
whether itis a structured language?
without using arithmatic operator solve which number is greater??????????
a=5 a=a++/++a