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();
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); }
To swap two numbers N1 and N2, using a third variable T... T = N1; N1 = N2; N2 = T;
a ^= b; b ^= a; a ^= b;
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.
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.
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); }
To swap two numbers N1 and N2, using a third variable T... T = N1; N1 = N2; N2 = T;
a ^= b; b ^= a; a ^= b;
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.
// Arithmetic method a = a + b; b = a - b; a = a - b; // XOR method a = a ^ b; b = a ^ b; a = a ^ b;
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; } }
To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;
Yes. int a, b; a= 2; b= 3; a= a+b;
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.
Third March, two thousand and eleven.
Yes, using whole numbers.
choosing a variable to represent one of the unspecified numbers in a problem and using it to write expressions for the other unspecified numbers in the problem.