12.Write a program in C to swap two numbers without using a temporary or third variable.
This problem needs writing a C program code to swap the values of two variables without using a temporary or third variable. The program need to perform an exchange operation using arithmetic or bitwise XOR operations to achieve a direct swapping values between two variables. This approach avoids the need to allocate additional memory, making the solution more space efficient.
swapping in C language:
Swapping in C is the process of exchanging the values of two variables. In this operation the value of one variable is assigned to another variable and vice versa, so that each variable has the initial value of the other. Swapping the values is a common process in several algorithms and applications, such as sorting algorithms to reorganize data.
Applications or Use of Swapping
Swapping is extensively used in numerous programming situations, comprising:
• Permutations: Making permutations of a set of values frequently contains swapping features.
• Sorting Algorithms: Algorithms like quick sort, bubble sort, and others often swap elements to organize data in a specific order.
• Data Manipulation: Several data manipulation tasks need swapping values to succeed preferred outcomes.
Sample Solution
1.Swap the Numbers Using arithmetic operations
2.Swap the number using bitwise XOR operations
1. Swap the Numbers Using arithmetic operations
/* a program in C to Swap the number using arithmetic Operators. */
#include <stdio.h>
int main()
{
int x, y;
printf("\n Enter any two numbers: \n");
scanf("%d%d",&x,&y);
printf("\n Values before swapping:”);
printf(“\n x=%d\nb=%d”,x,y);
// swap the numbers using arithmetic operators
x=x+y;
y=x-y;
x=x-y;
printf("\nValues After Swapping:”);
printf(“\n x=%d\nb=%d”,x,y);
return 0;
}
Output:
Enter any two numbers:
8 4
Values before swapping:
x=8
y=4
Values After Swapping:
x=4
y=8
Explanation
• Include the #include<stdio.h> Standard Input-Output Library header file to ensures that the functions for input and output processes are accessible.
• int main() function definition starts the execution of the program.
• int x, y; the statement declares two integer variables x and y.
• Input from user :
• printf("\n Enter any two numbers: \n"); prompts the user to input values for x and y.
• scanf("%d%d", &x, &y); function reads the input values provided by the user and stores them in x and y variable.
• Display values before exchange or swapping:
printf("\n Values before swapping:”);
printf(“\n x=%d\nb=%d”,x,y);
these statements print the values of x and y before swapping.
• Using Arithmetic Operations to swap values:
• x = x + y; adds x and y and hold the result in variable x.
• y = x - y; subtracts the new changed value of y (which was the original x + y) by y to acquire the original value of x and stores it in y.
• x = x - y; now subtracts the new updated value of y (i.e. original x) from x to acquire the original value of y and hold it in x.
• Show Swapped Values:
printf("\nValues After Swapping:”);
printf(“\n x=%d\nb=%d”,x,y);
Display the values of x and y after swapping.
• Return Statement: i.e return 0; shows that the program has finished successfully.
2.Swap the number using bitwise XOR operations
/* a program in C to Swap the number using bitwise XOR operations. */
#include <stdio.h>
int main()
{
int p, q;
// Prompt user to input values for x and y
printf("Enter a value for p & q: \n");
scanf("%d%d", &p, &q);
// show the values of variables x and y before swapping
printf("Values Before swapping\n");
printf(“p=%d\nq=%d”,p,q);
// Swap the values of p and q using bitwise XOR operator
p = p ^ q; // p holds the result of p XOR q
q = p ^ q; // q now holds the initial value of p
p =p ^ q; // p now holds the original value of q
// Display the values after swapping
printf("Values After swapping\n”);
printf(“p=%d\nq=%d”,p,q);
return 0;
}
Output
Enter a value for p & q:
2 3
Values Before swapping
p=2
q=3
Values After Swapping:
p=3
q=2
Explanation:
• Include the Standard #include <stdio.h> Input-Output Library header file to confirms that the functions for input and output operations are accessible.
• int main() starts the execution of the program.
• int p, q; declares two integer variables p and q.
• User Input :
• Enter a value for p & q: "); prompts the user to input values for p and q.
• scanf("%d%d", &p, &q); reads the input values from the user and stores them in p and q.
printf("Values Before swapping\n");
printf(“p=%d\nq=%d”,p,q);
prints the values before swapping.
• Exchange or swap Values Using Bitwise XOR Operator:
• p = p ^ q; stores the result of x XOR q in p.
• q = p ^ q; stores the original value of p in q by execution p XOR q (since p now holds p XOR q, this operation efficiently separates the original p).
• x = x ^ y; stores the original value of y in x by performing x XOR y (since y now holds the original x).
• Display the Swapped Values:
printf("Values After swapping\n”);
printf(“p=%d\nq=%d”,p,q);
prints the values of p and q after swapping.
• return 0; specifies that the program has completed successfully.
1.Write a program to add two numbers using the '+' operator.
2.Write a program in C to subtract two numbers using the '-' operator.
3.Write a program in C to multiply two numbers using the '*' operator.
4.Write a program to divide two numbers using the '/' operator.
5.Write a program in C to find the remainder of two numbers using the '%' operator.
6.Implement a program in C to perform bitwise AND on two numbers using the '&' operator.
7.Write a program in C to perform bitwise OR on two numbers using the '|' operator.
8.Write a program to perform bitwise XOR on two numbers using the '^' operator.
9.Write a program in C to perform bitwise left shift on a number using the '<<' operator.
10.Implement a program in C to perform bitwise right shift on a number using the '>>' operator.
11.Write a program to in C check if a number is even or odd using the bitwise AND operator.
13.Write a program in C to find the minimum of two numbers without using the 'if' statement.
14. Write a program in C to check if a number is negative without using the 'if' statement.
15. Write a program in C to toggle a particular bit in a number using bitwise XOR.
Other Topic:-->>Nested While Loop. || conditional statements Assignments in C.