WAP to swap two numbers without using third variable

Опубликовано: 08 Июнь 2026
на канале: APS.INNOVCODE
128
7

In this video, I explain how to swap two numbers without using a third variable in C programming.
This is a common interview question and a great way to understand logic building in programming.

🔹 What you’ll learn:
Swapping numbers using arithmetic operators
Logic behind the technique
Step-by-step explanation with code

💻 Language used: C Programming
🎯 Level: Beginner-friendly

If you find this helpful, don’t forget to like 👍, share, and subscribe to APS InnovCode for more coding tutorials!

Program :-
int main(){
int a,b;
printf("enter the value of a ");
scanf("%d",&a);
printf("enter the value of b ");
scanf("%d",&b);
printf("the number before swapping are \n");
printf("a=%d\nb=%d\n",a,b);
a=a+b;
a=a-b;
a=a-b;
printf("the number after swapping are \n");
printf("a=%d\nb=%d",a,b);
return 0;
}
🔹 Explanation
• Step 1: → sum stored in .
• Step 2: → original stored in .
• Step 3: → original stored in .
Result: Values of a and b get swapped without using a third variable.

🔹 Swapping Two Numbers Using XOR (Without Third Variable)
• → stores combined XOR of and in .
• → now becomes original .
• → now becomes original .
Result: The values of a and b are swapped without using a third variable, and without arithmetic operations.

👉 If you want the explanation of the logic behind swapping two numbers using the XOR operator, comment below and I’ll make a detailed video on it!

#coding #programming #Cprogramming #swapnumbers #logicbuilding #interviewquestions #APSInnovCode