The Program in C that SWAPs (interchange) three numbers is given below:
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Enter the third number: ");
scanf("%d", &num3);
printf("Before swapping: num1 = %d, num2 = %d, num3 = %d\n", num1, num2, num3);
int temp;
temp = num1;
num1 = num2;
num2 = num3;
num3 = temp;
printf("After swapping: num1 = %d, num2 = %d, num3 = %d", num1, num2, num3);
return 0;
}
Output:
Enter the first number: 1999
Enter the second number: 1996
Enter the third number: 1992
Before swapping: num1 = 1999, num2 = 1996, num3 = 1992
After swapping: num1 = 1996, num2 = 1992, num3 = 1999
Pro-Tips💡
This program takes input of three integers and uses a temporary variable to swap the values of the three variables.
The program first stores the value of num1 in a temporary variable called temp, then assigns the value of num2 to num1, then assigns the value of num3 to num2
and finally assigns the value of temp to num3. This way the values of num1, num2, and num3 are interchanged
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.