How To Swap Two Numbers Without Using 3rd Variable in C Programming | C Programming Examples - 12
Write a C program to Swap Two Numbers Without Using Third Variable. How to Swap Two Numbers Without Using Third Variable in C programming. Logic to Swap Two Numbers Without Using Third Variable in C programming.
This is a very simple program for beginners to understand how swapping works and how to Swap Two Numbers Without Using Third Variable in c programming language.
C Programming Examples:
1. Write a C Program to Swap Two Numbers Without Using Third Variable
2. C program to Swap Two Numbers Without Using 3rd Variable
3. How to Swap Two Numbers Without Using Third Variable in C Programming
C Program to Swap Two Numbers Without Using Third Variable : |
#include<stdio.h>
int main()
{
int a,b;
printf("Enter a\n");
scanf("%d",&a);
printf("Enter b\n");
scanf("%d",&b);
printf("The values of a is %d
and b is %d before swaping\n",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("The values of a is %d
and b is %d after swaping\n",a,b);
}
Output:
Enter a
30
Enter b
35
The values of a is 30 and b is 35 before swaping
The values of a is 35 and b is 30 after swaping
Explanation of Swapping Two Numbers Without Using Third Variable in C Programming :
Here we initialized a,b,c
a---> for storing 1st number
b--->for storing second number
Now here goes the logic(let us take a=1 and b=3)
a=a+b, Therefore, a=1+3=4
b=a-b, Therefore b=4-3=1
a=a-b, Therefore a=4-1=3
Now the values of a and b are 3 and 1
The values of a and are printed.
0 Response to How To Swap Two Numbers Without Using Third Variable in C Programming | C Programming Examples - 12
Post a Comment