Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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

Explain what is the difference between a string and an array?

0 Answers  


What is function and its example?

0 Answers  


What is the Purpose of 'extern' keyword in a function declaration?

0 Answers  


what is the difference between malloc() and calloc() function?

1 Answers  


Can I use base-2 constants (something like 0b101010)? Is there a printf format for binary?

0 Answers  


Write a program that accepts a string where multiple spaces are given in between the words. Print the string ignoring the multiple spaces. Example: Input: “ We Are Student “ Output: "We Are Student"

1 Answers  


What are pointers? Why are they used?

0 Answers  


What is sparse file?

1 Answers  


What is a static function in c?

0 Answers  


which of the following is allowed in a "C" arithematic instruction a) [] b) {} c) () d) none of the above

0 Answers  


What is the difference between Printf(..) and sprint(...) ?

0 Answers   InterGraph,


What do you mean by a local block?

0 Answers   InterGraph,


Categories