Create a function to swap the values of two variables

Question: Create a function to swap the values of two variables.

Create a function to swap the values of two variables

What is swapping in C language?
   In computer programming, the act of swapping two variables refers to mutually exchanging the values of the variables. Usually, this is done with the data in memory. For example, in a program, two variables may be defined thus (in C program) #include <stdio.h>

What is swapping explain?
   Swapping is a mechanism in which a process can be swapped temporarily out of main memory (or move) to secondary storage (disk) and make that memory available to other processes.

Why are swaps used?
   Swaps also help companies hedge against interest rate exposure by reducing the uncertainty of future cash flows. ... Currency and interest rate swaps are used as financial tools to lower the amount needed to service a debt as a result of these advantages.

C programming: swapping two variables
   Swapping two variables refers to mutually exchanging the values of the variables. Generally, this is done with the data in memory.

The simplest method to swap two variables is to use a third temporary variable:
define swap(a, b)
    temp := a
    a := b
    b := temp

Pictorial Presentation:
The simplest method to swap two variables is to use a third temporary variable
Example: 1
Sample Solution:
C Code:
  1. #include<stdio.h>

  2. void swap(int *,int *);
  3. int main()
  4. {

  5.     int n1,n2;
  6. printf("\n\n Function : swap two numbers using function :\n");
  7. printf("------------------------------------------------\n");    
  8.     printf("Input 1st number : ");
  9.     scanf("%d",&n1);
  10.     printf("Input 2nd number : ");
  11.     scanf("%d",&n2);

  12.     printf("Before swapping: n1 = %d, n2 = %d ",n1,n2);
  13. //pass the address of both variables to the function.
  14.     swap(&n1,&n2);

  15.     printf("\nAfter swapping: n1 = %d, n2 = %d \n\n",n1,n2);
  16.     return 0;
  17. }

  18. void swap(int *p,int *q)
  19. {
  20. //p=&n1 so p store the address of n1, so *p store the value of n1
  21. //q=&n2 so q store the address of n2, so *q store the value of n2

  22.     int tmp;
  23.     tmp = *p; // tmp store the value of n1
  24.     *p=*q;    // *p store the value of *q that is value of n2
  25.     *q=tmp;   // *q store the value of tmp that is the value of n1
  26. }

Output:
 Function : swap two numbers using function :
------------------------------------------------
Input 1st number : 2
Input 2nd number : 4
Before swapping: n1 = 2, n2 = 4
After swapping: n1 = 4, n2 = 2


Output Screenshot:
swap two numbers using function output screenshot

Example: 2
C Code:
  1. #include<stdio.h>

  2. void swap(int *,int *);

  3. int main(){

  4.         int a,b;

  5.    

  6.     printf("Enter any two integers: ");

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

  8.  

  9.     printf("Before swapping: a = %d, b=%d",a,b);

  10.  

  11.     swap(&a,&b);

  12.  

  13.     printf("nAfter swapping: a = %d, b=%d",a,b);

  14.     return 0;

  15. }

  16.  

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

  18.     int *temp;

  19.     temp = a;

  20.     *a=*b;

  21.     *b=*temp;

  22. }

Output:
Enter any two integers: 3 6
Before swapping: a = 3, b=6
After swapping: a = 6, b =3

Output Screenshot:

Download the Android app to get all Government Job Notifications on your Mobile.
Download Now
Important: Please always Check and Confirm the above details with the official Advertisement / Notification.
Previous Post Next Post