How To Swap Two Numbers Using 3rd Variable in C Programming | C Programming Examples - 11
Write a C program to Swap Two Numbers Using Third Variable. How to Swap Two Numbers Using Third Variable in C programming. Logic to Swap Two Numbers 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 Using Third Variable in c programming language.
C Programming Examples:
1. Write a C Program to Swap Two Numbers Using Third Variable
2. C program to Swap Two Numbers Using 3rd Variable
3. How to Swap Two Numbers Using Third Variable in C Programming
C Program to Swap Two Numbers Using Third Variable : |
#include<stdio.h>
main()
{
int a,b,c;
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);
c=a;
a=b;
b=c;
printf("The values of a is %d
and b is %d after swaping\n",a,b);
}
Output:
Enter a
35
Enter b
30
The values of a is 35 and b is 30 before swaping
The values of a is 30 and b is 35 after swaping
Explanation of Swapping Two Numbers Using Third Variable in C Programming :
Here we initialized a,b,c
a---> for storing 1st number
b--->for storing second number
c---->for storing temporary variable
Now here goes the logic(let us take a=1 and b=3)
c=a, Therefore, c=1
a=b, Therefore a=3
b=c, Therefore b=1
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 Using Third Variable in C Programming | C Programming Examples - 11
Post a Comment