Swapping Two Numbers In C

Introduction
  •  In this article, I'm explaining how to swap two numbers in C.
Software Requirements 
  •  Turbo C++ OR C
Programming
  1. #include <stdio.h>  
  2.    
  3. int main()  
  4. {  
  5.    int x, y, temp;  
  6.    
  7.    printf("Enter the value of x and y\n");  
  8.    scanf("%d%d", &x, &y);  
  9.    
  10.    printf("Before Swapping\nx = %d\ny = %d\n",x,y);  
  11.    
  12.    temp = x;  
  13.    x    = y;  
  14.    y    = temp;  
  15.    
  16.    printf("After Swapping\nx = %d\ny = %d\n",x,y);  
  17.    
  18.    return 0;  
  19. }  
Explanation 
  • In the above program, before we swap the numbers, the values of x and y are in the correct order, as we have assigned them. After swapping, the numbers are interchanged.

    srk
 
Output

Output
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now