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 / insane programmer

#include<stdio.h>
#include<conio.h>

void main()
{
int a=100, b=30;
clrscr();

a = a+b;
b = a-b;
a = a-b;

printf("Swapping without using third variable (using + and -).\n\n");
printf("Value of a=%d and b=%d.", a,b);
getch();
}

you can get more example here http://rajkishor09.hubpages.com/_eknow/hub/How-to-swap-two-numbers-without-using-third-temp-variable

Is This Answer Correct ?    1 Yes 0 No

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

Answer / george washington

Suppose:
x=1
y=3

Redefine:
x=x+y; //Or, x=1+3=4 and
y=x-y; // y=4-3=1 and
x=x-y; // x=4-1=3

Is This Answer Correct ?    1 Yes 0 No

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

Answer / ankit

a=a+b;
b=a-b;
a=a-b;

example int a=2;int b=3
put a=2 & b=3
a=2+3;
b=5-3;
a=5-2;
a=3;
b=2;

Is This Answer Correct ?    2 Yes 1 No

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

Answer / amar

void main()
{
int a,b;
printf("enter the two numbers");
scanf("%d%d",&a,&b);
a^=b^=a^=b;
printf("%d%d",a,b);//swapped no
}

Is This Answer Correct ?    1 Yes 0 No

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

Answer / tchoephel

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

int a,b;
printf("enter two nos A and B\n");
scanf("%d%d",a,b);
printf("before swaping A=%d\n B=%d\n",a,b);
a=a+b:
b=a-b;
a=a-b;
prinf("after swaping A=%d\n B=%d\n",a,b);
getch();
}

Is This Answer Correct ?    1 Yes 0 No

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

Answer / akash

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("enter the value a & b");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("interchange in a & b =%d%d",a,b);
getch();
}

Is This Answer Correct ?    1 Yes 0 No

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

Answer / ijagz

#include<conio.h>
#include<stdio.h>

int main()
{
int a,b;
printf("Enter the first number\n");
scanf("%d",&a);
printf("Enter the second number\n");
scanf("%d",&b);
printf("your answer is %d %d",b,a);
getch();
return 0;
}

Is This Answer Correct ?    1 Yes 0 No

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

Answer / rani

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf ("enter the values to a & b");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("%d%d",a,b);
}

Is This Answer Correct ?    1 Yes 0 No

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

Answer / jaspreet singh

hey prasanna the program you hv writen is correct but this
method fails when we give the input as a=32767 and b=1

the correct method is
a^=b^=a^=b

or
a=a^b;
b=a^b;
a=a^b;
it will always return the correct answer even if the input
is a=32767 and b=1...............

Is This Answer Correct ?    17 Yes 17 No

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

Answer / shubham agarwal

the answer is simple

a=a*b;
b=a/b;
a=a/b;


this is one of the few possible ways to swap
simple app. of +,- can also brong the result


by
shubham agarwal
shubham.agarwal363@yahoo.com from NIT Srinagar

Is This Answer Correct ?    8 Yes 8 No

Post New Answer

More C Interview Questions

Is c dynamically typed?

0 Answers  


Explain what is the benefit of using #define to declare a constant?

0 Answers  


Given an array A[n+m] of n+m numbers, where A[1] ... A[n] is sorted and A[n+1] ... A[n+m] is sorted. Design a linear time algorithm to obtain A[1...n+m] sorted using only O(1) extra space. Time Complexity of your algorithm should be O(n) and Space Complexity O(1).

0 Answers  


How do I access command-line arguments?

2 Answers   Bosch, Wipro,


which type of question asked from c / c++ in interview.

2 Answers  


what is meant by c

9 Answers   INiTS,


What do you know about the use of bit field?

0 Answers  


What is the difference between near, far and huge pointers?

0 Answers  


how to find anagram without using string functions using only loops in c programming

1 Answers   Mind Tree, TCS,


Explain how do you list files in a directory?

0 Answers  


What is table lookup in c?

0 Answers  


Explain what will the preprocessor do for a program?

0 Answers  


Categories