Swapping of two numbers

How to swap two numbers without using temporary variable?

Questions by rabia.sultana14

Showing Answers 1 - 7 of 7 Answers

sasanka

  • Aug 7th, 2011
 

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

e.g:if a=5 b=3
a=5*3=15
b=15/3=5
a=15/5=3
=>a=3 b=5

Code
  1. #include<stdio.h>

  2. "a=%d b=%d""a=%d b=%d",a,b);

  3. }

  Was this answer useful?  Yes

shiva

  • Sep 27th, 2011
 

we can write swapping of two numbers single statement also
i.e a=(a+b)-(b=a);

Take any two values then perform a=(a+b)-(b=a);
Instead of writing 3 statements

i.e
a=a+b;
b=a-b;
a=a-b;

Here no need of temp variable and no need of 3 statements

  Was this answer useful?  Yes

Tanuja Jaiswal

  • Jan 26th, 2012
 

#include
void main()
{
int a,b,temp;
printf("Enter values for a and b ");
scanf("%d %d",&a , &b);
printf("values before swaping are : %d %d",a,b);
temp=a;
a=b;
b=temp;
printf("After swaping values are : a= %d b=%d,a,b);
getch();
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions