write a program to swap Two numbers without using temp variable.

Answers were Sorted based on User's Feedback



write a program to swap Two numbers without using temp variable...

Answer / vigneswari

#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c,d;
cout<<"\n enter a:";
cin>>a;
cout<<"\n enter b:";
cin>>b;
c=a+b;
d=a-b;
c=a-b;
cout<<"\n answer a :"<<c;
cout<<"\n answer b:"<<d;
getch();
}

Is This Answer Correct ?    1 Yes 0 No

write a program to swap Two numbers without using temp variable...

Answer / ashish omar

Suppose: a = 3
b=2;

11
10
a = a ^ b;

11
10
==
01

b = a ^ b;
01
10
==
11

a = a ^ b;

01
11
==
10

Is This Answer Correct ?    1 Yes 0 No

write a program to swap Two numbers without using temp variable...

Answer / dhruv

I am not giving an answer, but just wanted to point out that
most of the solutions which try to add (a+b) and then
subtract from it will not work if (a+b) exceeds the maximum
value an int can hold.

So even if it looks good on paper it will give erroneous
results when run on a machine

Is This Answer Correct ?    1 Yes 0 No

write a program to swap Two numbers without using temp variable...

Answer / bhokal

#include <stdio.h>
#include <stdlib.h>

void main()
{
int p,r;
printf("enter no to b swaped:\n");
scanf("%d%d",&p,&r);
printf("swapped no . are\n%d\n%d\n",r,p);
getch();
}

Is This Answer Correct ?    2 Yes 1 No

write a program to swap Two numbers without using temp variable...

Answer / ashish kumar sharma

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("enter the Value of a:\n");
scanf("%d",&a);
printf("enter the Value of b:\n");
scanf("%d",&b);
a=a+b;
b=a-b;
a=a-b;
printf("Display The Swapping:\n");
printf("a=%d And b=%d",a,b);
getch();
}

Is This Answer Correct ?    1 Yes 0 No

write a program to swap Two numbers without using temp variable...

Answer / rakesh

#include<stdio.h>
#include<conio.h>
void main()
{

int a=10,b=8;
a=a+b;//a=10+8=18
b=a-b;//b=18-8=10
a=a-b;//a=18-10=8
//hence a=8,b=10
}

Is This Answer Correct ?    1 Yes 0 No

write a program to swap Two numbers without using temp variable...

Answer / guest

using xor technique is much faster than using addition and
subtraction process , and xor instruction is much simpler at
processor level.

so be simply

x = x xor y
y = x xor y
x = x xor y

Is This Answer Correct ?    1 Yes 0 No

write a program to swap Two numbers without using temp variable...

Answer / chandan

#!/usr/bin/perl
print "Enter Values for a & b\n";
my ($a,$b);
$a=<STDIN>;
$b=<STDIN>;
print "Value of A & B Befor Swap\n";
print "Value of a = $a\n";
print "Value of b = $b\n";
$b=($a+$b)-($a=$b);
print "Value of A & B After Swaping\n";
print "Value of a = $a\n";
print "Value of b = $b\n";

Is This Answer Correct ?    1 Yes 0 No

write a program to swap Two numbers without using temp variable...

Answer / tamal datta

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("\n Enter a = ");
scanf("%d",&a);
printf("\n Enter b = ");
scanf ("%d",&b);
printf ("\nBefore swapping a = %d",a);
printf ("\nBefore swapping b = %d",b);
//swaping of 2 numbers without using temp variable
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter swapping a = %d",a);
printf ("\nAfter swapping b= %d",b);
getch();
}

Is This Answer Correct ?    2 Yes 1 No

write a program to swap Two numbers without using temp variable...

Answer / kabita shah

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int x,y;
printf("enter two value");
scanf("%d%d",&x,&y);
x=x+y;
y=x-y;
x=x-y;
printf("value of x=%d",x);
printf("value of y=%d",y);
getch();
}

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More C Interview Questions

Please send me WIPRO technical question to my mail ID.. its nisha_g28@yahoo.com please its urgent

0 Answers  


int main() { int days; printf("enter days you are late"); scanf("%d",days); if (days<=5) printf("5o paisa fine"); if (days<=10&&days>=6) printf("1rs fine"); if(days>10) printf("10 rs fine"); if(days=30) printf("membership cancelled"); return 0; } tell me whats wrong in this program? is it right?

2 Answers  


Write a program to compute the similarity between two strings - The program should get the two strings as input - Then it will output one single number which is the percentage of similarity between the two strings

0 Answers  


What character terminates all strings composed of character arrays? 1) 0 2) . 3) END

3 Answers  


# define x=1+4; main() { int x; printf("%d%d",x/2,x/4); }

5 Answers  






Explain how do you override a defined macro?

0 Answers  


Explain how do you declare an array that will hold more than 64kb of data?

0 Answers  


What does 4d mean in c?

0 Answers  


What are the benefits of organizational structure?

0 Answers  


What are # preprocessor operator in c?

0 Answers  


Write the following function in C. stripos — Find position of first occurrence of a case- insensitive string int stripos ( char* haystack, char* needle, int offset ) Returns the numeric position of the first occurrence of needle in the haystack string. Note that the needle may be a string of one or more characters. If needle is not found, stripos() will return - 1. The function should not make use of any C library function calls.

3 Answers   Google, Infosys, JTL, OpenFeel,


Can you please explain the scope of static variables?

0 Answers  


Categories