The two numbers can be swap with the third variable, without third variable or be using bitwise XOR etc. To understand this example you should know Data Types In C Programming and Operators In C Programming Part: 1 before proceeding further.
Using swap program we can exchange values of two values. Consider your program takes two values as a=5 and b=9 values after the swap has become a=9 and b=5.
Let see all methods one by one.
1. Swapping Using Third Variable
#include<stdio.h> int main() { int a,b,temp; printf("Enter valude of a and b\n"); scanf("%d %d",&a,&b); printf("a and b before swap are a=%d and b=%d\n",a,b); temp=a; a=b; b=temp; printf("a and b after swap are a=%d and b=%d\n",a,b); return 0; }
The output for the above program is
Please feel free to comment if you find anything incorrect or you want to share more information about the topic discussed above.
2. Swapping Without Third Variable
#include<stdio.h> int main() { int a, b; printf("Enter value of a and b\n"); scanf("%d%d", &a, &b); printf("a and b before swap are a=%d b=%d\n",a,b); a=a+b; b=a-b; a=a-b; printf("a and b after swap are a=%d b=%d\n",a,b); return 0; }The output for the above program is
3. Swapping Using Bitwise XOR
For this method, you need to know what is Bitwise XOR and its working. Let's see the program.#include<stdio.h> int main() { int a,b; printf("Enter value of a and b\n"); scanf("%d %d",&a,&b); printf("a and b before swap are a=%d b=%d\n",a,b); a = a^b; b = b^a; a = a^b; // In short above statements can be written as // a^=b^=a^=b; printf("a and b after swap are a=%d b=%d\n",a,b); return 0; }The output for the above program is
4. Swapping Using Comma Operator
For this method, you need to know what is the Comma Operator and it's working. Let's see the program.#include<stdio.h> int main() { int a,b,temp; printf("Enter a value of a and b\n"); scanf("%d %d",&a,&b); printf("a and b before swap are a=%d b=%d\n",a,b); temp=a, a=b, b=temp; printf("a and b after swap are a=%d b=%d\n",a,b); return 0; }The output for the above program is
5. Swapping Using Function
For this method, you should know the concept of function. I have not explained the function concept I will explain the function further days. If you know the function then you will understand the code. If you don't know about function then forget this code and check after learning function concept. Let's see the code.#include <stdio.h> void swap(int, int); int main() { int a,b; printf("Enter a value of a and b\n"); scanf("%d %d",&a,&b); printf("a and b before swap are a=%d b=%d\n",a,b); swap(a,b); return 0; } void swap(int x, int y) { int temp; temp = x; x = y; y = temp; printf("a and b after swap are a=%d b=%d\n",x,y); } The output for above program is
6. Swapping Using Pointer
For this method, you should know the pointer concept. I have not explained the pointer concept I will explain about the pointer further days. If you know the pointer then you will understand the code. If you don't know about pointer then forget this code and check after learning the pointer concept. Let's see the code.#include <stdio.h> int main() { int a,b,*x,*y,temp; printf("Enter a value of a and b\n"); scanf("%d %d",&a,&b); printf("a and b before swap are a=%d b=%d\n",a,b); x = &a; y = &b; temp = *x; *x = *y; *y = temp; printf("a and b after swap are a=%d b=%d\n",a,b); return 0; }The output for the above program is
7. Swapping Using Pointer Function
For this method, you should know the concept of function and pointer. I have not explained the function and pointer concept I will explain the function and pointer further days. If you know the function and pointer then you will understand the code. If you don't know about function and pointer then forget this code and check after learning function and pointer concept. Let's see the code.#include <stdio.h> int swap(); int main() { int a,b; printf("Enter a value of a and b\n"); scanf("%d %d",&a,&b); printf("a and b before swap are a=%d b=%d\n",a,b); swap(&a,&b); printf("a and b after swap are a=%d b=%d\n",a,b); } int swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; return 0; }The output for the above program is
Please feel free to comment if you find anything incorrect or you want to share more information about the topic discussed above.
Nice content
ReplyDeleteThanks Bro. If you like blog then don't forget to share....
Delete