Swapping Two Numbers In C Programing
Swap Definition
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
Using a temporary variable
The simplest and probably most widely used method to swap two variables is to use a third temporary variable:
temp := x
x := y
y := temp
Swapping Two Numbers Example Program
/*##Swapping Two Numbers*/
/*##Calculation Programs, Datatype Programs, Basic Programs*/
/*##Example*/
#include <stdio.h>
int main()
{
int num1, num2, temp;
printf("Enter the 2 numbers to be swapped : \n");
scanf("%d%d", &num1, &num2);
printf("Before Swapping, the values are : \nnum1 = %d\nnum2 = %d\n",num1,num2);
temp = num1;
num1 = num2;
num2 = temp;
printf("After Swapping, the values are : \nnum1 = %d\nnum2 = %d\n",num1,num2);
return 0;
}
Sample Output:
Enter the 2 numbers to be swapped :
100
99
Before Swapping, the values are :
num1 = 100
num2 = 99
After Swapping, the values are :
num1 = 99
num2 = 100
Read More Articles
- Use of getch(),getche() and getchar() in C
- Switch Case Statement Example Program In C Programming Language
- C Character Set
- Convert a Floating-point value to an Integer in C
- Data Input and Output gets and puts Example Program In C
- Special Operators In C
- Pointer Representation and Pointer Example Programs
- C Data Input and Data Output
- Simple While Loop Example Program In C Programming Language
- Data Output printf and putchar Example Program In C
- C Introduction
- C Operators
- Storage Classes In C
- C Pointers
- File Management
- C Identifiers
- Loop Control Statements
- Hello World - Simple C Program
- C Array
- Single Character Output Function : putchar()
- C Reserve Words
- C Specific Properties and Implementation
- If else Statement Example Program In C Programming Language
- If Statement Example Program In C Programming Language
- Confusing Array in C ( Array Representation and Initialization )