In this tutorial learn how you can input two numbers from keyboard, store it in two variable A and B . Interchange their values without using third variable. Here the trick is so simple that we store both A and B on a single variable then subtract B for the value of A and again for B subtract A.

Here is a representation:

A=A+B (Add Both Values)

B=A-B (Now Subtract B, and we get the value of A on B.)

B=A-B (Now Subtract A, and we get the value of B on A.)

C programming is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, etc.


write c program to swap values of two numbers from keyboard without using third variable



Write a Program to Swap the Values of Two Numbers from Keyboard Without using Third Variable

Here is the code to input two numbers from keyboard, store it in two variable A and B and Interchange their values without using third variable in C programming.

#include <stdio.h> //WAP to input two numbers from keyboard, store it in two variable A and B . Interchange their values without using third variable. int main(){     int A,B;     printf("Enter 2 numbers:");     scanf("%d%d",&A,&B);     A=A+B;     B=A-B;     A=A-B;     printf("Swappd 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 keyboard, store it in two variable A and B . Interchange their values without using third variable. in C programming. Comment below if you like our tutorial.