answersLogoWhite

0


Best Answer

int a,b,temp;

printf("enter the values of a & b"); //could be done in diff. two lines for the input//

scanf("%d %d",&a,&b);

printf("the values of a=%d & b=%d before swapping",a,b);

// swapping without using arithmetic operators.....

temp=a;

a=b;

b=temp;

printf("the values of a=%d & b=%d after swapping",a,b);

getch();

User Avatar

Wiki User

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

Wiki User

16y ago

int iA = 5;

int iB = 6;

int iA = iA + iB; // iA is now 11

int iB = iA - iB; // iB is now 5

int iA = iA - iB; // iA is now 6

Note: Even though no overt variable has been created, the compiler has created temporary variables because a variable left of the equal sign has been used on the right.

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

# include

# include

void main()

{

clrscr();

int a,b;

printf ("Enter the first value:");

scanf ("%d",& a );

printf ("Enter the second value:");

scanf ("%d",& b );

printf ("\n\nBefor swaping the values ");

printf ("\nThe first value is %d",a);

printf ("\nthe second value is %d",b);

printf ("\n\nAfter swaping the values ");

printf ("\nThe first value is %d",b);

printf ("\nthe second value is %d",a);

}

OUTPUT

Enter the first value:8

Enter the second value:5

Befor swaping the values

The first value is 8

the second value 5

After swaping the values

The first value is 5

the second value 8

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Swapping two values without using a third variable is made possible through a series of three XOR/assign operations, alternating the operands.


Consider the following declarations:


int x = 0;

int y = 1;


The following statement will swap the values of x and y:


x ^= y ^= x ^= y; // swap



The same statement implemented as a function:


void swap(int &x, int &y){ x ^= y ^= x ^= y; }

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

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

int temp = *a;

*a = *b;

*b = t;

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

With a temporary variabele:

tmp= x, x=y, y=tmp;

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

void swap (int* x, int* y) { (*x)^=(*y)^=(*x)^=(*y);

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

swap (int *a, int *b) {

*a ^= *b;

*b ^= *a;

*a ^= *b;

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you swap two numbers without using third variable in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is program in c to swap entered two numbers without using third variable?

The required c program is given below /*Swapping(interchange) the two entered numbers*/ #include<stdio.h> main() { /*Without using third variable*/ int a,b,t; printf("Enter a:"); scanf("%d",&a); printf("Enter b:"); scanf("%d",&b); a=a+b; b=a-b; a=a-b; printf("\n After swapping without using third variable"); printf("\na=%d\nb=%d",a,b); }


Swapping of number with using third variable?

To swap two numbers N1 and N2, using a third variable T... T = N1; N1 = N2; N2 = T;


Swap logic without using third variable?

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


How do you find the greatest of three numbers using ternery operator?

Compare the first two numbers with the ternary operator. Store the result in a temporary variable. Compare the temporary variable with the third number, again using the ternary operator.


How do you swap in c programming?

the simple way can be explained by example as: let there be two integers as : int a=10,b=5; if we want to use third variable then let third variable be int c; and sorting is done as : c=a; a=b; b=c; if it is to be done by without using third variable then : a=a+b; b=a-b; a=a-b; at last the variable is sorted.

Related questions

What is program in c to swap entered two numbers without using third variable?

The required c program is given below /*Swapping(interchange) the two entered numbers*/ #include<stdio.h> main() { /*Without using third variable*/ int a,b,t; printf("Enter a:"); scanf("%d",&a); printf("Enter b:"); scanf("%d",&b); a=a+b; b=a-b; a=a-b; printf("\n After swapping without using third variable"); printf("\na=%d\nb=%d",a,b); }


Swapping of number with using third variable?

To swap two numbers N1 and N2, using a third variable T... T = N1; N1 = N2; N2 = T;


Swap logic without using third variable?

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


How do you find the greatest of three numbers using ternery operator?

Compare the first two numbers with the ternary operator. Store the result in a temporary variable. Compare the temporary variable with the third number, again using the ternary operator.


What is coding of swaping of two numbers without using third variable in c sharp?

// Arithmetic method a = a + b; b = a - b; a = a - b; // XOR method a = a ^ b; b = a ^ b; a = a ^ b;


Swapping without third variable?

There are two ways in which you can swap without a third variable. 1. Using xor operation swap( int *a, int *b) { *a = *a ^ *b; *b = *a ^ *b; *a = *a ^ *b; } 2. Using addition and subtraction swap( int *a, int *b) { *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;


Can you write addition of 2 no program without using third variable?

Yes. int a, b; a= 2; b= 3; a= a+b;


How do you swap in c programming?

the simple way can be explained by example as: let there be two integers as : int a=10,b=5; if we want to use third variable then let third variable be int c; and sorting is done as : c=a; a=b; b=c; if it is to be done by without using third variable then : a=a+b; b=a-b; a=a-b; at last the variable is sorted.


How do you write 3rd march 2011 without using numbers?

Third March, two thousand and eleven.


Can a discrete variable be measured?

Yes, using whole numbers.


Program in c using pointers to interchange 2 values without using third variable?

swap (int *pa, int *pb) { *pa ^= *pb; *pa ^= *pa; *pa ^= *pb; }