In this tutorial learn how you can input two numbers from keyboard and store it in two variable A and B and swap the values. Here the trick is so simple that we use a third variable.
First, we store the value of A to C the third variable. And then we store the value of B to A. And finally, we store the value of C to B. By this we swapped the values.
C programming is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, etc.
Write a Program to Swap the Values of Two Numbers from Keyboard
Here is the code to input two numbers from keyboard and store it in two variable A and B and swap the values in C programming.
#include <stdio.h> //WAP to input two numbers from keyboard, store it in two variable A and B . Swap the values.
int main(){
int A,B,C;
printf("Enter 2 numbers:");
scanf("%d%d",&A,&B);
C=B;
B=A;
A=C;
printf("Swapped values of A is %d and B is %d.",A,B);
return 0;
}
Test Live At Jdoodle
Conclusion
This is how you can input two numbers from the keyboard and store it in two variables A and B and swap the values in C programming. Comment below if you like our tutorial.