answersLogoWhite

0


Best Answer

#include<stdio.h>

void main()

{

int a=2,b=4;

printf("Program for swapping two numbers ");

printf("Numbers before swapping");

printf("a=%d and b=%d",a,b);

a=((a+b)-(b=a));

printf("Numbers after swapping");

printf("a=%d and b=%d",a,b);

getch();

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

echo Enter valuefor a:

read a

echo Enter valuefor b:

read b

clear

echo Values of variables before swaping

echo A=$a

echo B=$b

echo Values of variables after swaping

a=`expr $a + $b`

b=`expr $a - $b`

a=`expr $a - $b`

echo A=$a

echo B=$b

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

The operating system is immaterial. There are two ways to swap two variables.

Using a temporary variable:

void swap (int* a, int* b) {

int temp;

temp = (*a);

(*a) = (*b);

(*b) = temp;

}

Without using a temporary variable:

void swap (int* a, int* b) {

(*a) ^= (*b) ^= (*a) ^= (*b);

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a c program to swap the values of two variables in unix?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Java program to swap values of 2 variables?

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


How do you write a program in C plus plus plus plus How do you write a program in C to swap two variables without using the third oneo swap two variables without using the third one?

To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;


How do you write a program in C to swap two variables using the third variable?

Consider the following declarations:int x = 0;int y = 1;In order to swap the values, we need to use a temporary variable:int t = x;x = y;y = t;However, it is possible to swap the values without using a third variable:x ^= y ^= x ^= y;


How do you write a program in Perl to swap two variables without using the third one?

Use list assignment i.e. for two variables $a, $b: ($a,$b) = ($b,$a)


How do you write a program in C to swap two variables without using the third one using XOR?

a=a^b; b=a^b; a=a^b;


How would you write a program to swap the values of character variable using call by address?

void swap (int* a, int* b) { if (!a !b) return; // can't swap a pointer to null *a^=*b^=*a^=*b; }


Program designed to swap the values of two variables?

You'll need 3 variables for this, here's a pseudo code for swapping values of 2 variables.ALGORITHM SWAPvar1 = 10var2 = 20temp = 0temp = var1 "temp = 10"var1 = var2 "var1 = 20, originally 10"var2 = temp "var2 = 10, originally 20"END SWAP


How do you write a program in c to swap two values by using functions?

#include using namespace std; void swap(int &amp;a, int &amp;b); int main() { int x=5,y=3; cout


How do we write c program without using swap to interchange values by accessing another variable?

int a,b; a=a+b; b=a-b; a=a-b; that's it simple


What is meant by swapping of variables?

It means that you swap the values of that variables EX: -==- before swapping :- Variable1 = 5; variable2 = 10; after swapping :- Variable1 = 10; variable2 = 5;


What is the Algorithm for exchanging values of two variables?

There are three primary algorithms to exchange the values of two variables. Exchange with Temporary Variable temp = a; a = b; b = temp; Exchange Without Temporary Variable Using Exclusive Or a = a ^ b; b = b ^ a; a = a ^ b; Exchange Without Temporary Variable Using Arithmetic a = a + b; b = b - a; a = a - b;


How do you write a program in c to swap two variables without using the add and multiplication?

int varA, varB; int tmp; // common way to swap 2 variables in the same type; tmp = varA; // store the original value of varA varA = varB; // assign A with B varB = tmp; // B now has the original value of varA