Write a C++ program to swap the values of two variables if values of both variables are not the same using a switch statement
e.g
before swapping : a=5; b=6
after swapping:Â a=6, b=5
Flowchart of the program to swap the values of two variables using the switch statement
C++ Source Code to swap the values of two variables using the switch statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> using namespace std; int main() { int a,b,temp; cout<<"Please input the value of a : "; cin>>a; cout<<"Please input the value of b : "; cin>>b; switch(a==b) { case 0: temp=a; a=b; b=temp; cout<<"\nAfter Swap\n"; cout<<"a : "<<a; cout<<"\nb : "<<b; break; case 1: cout<<"Values are same"; break; } } |
Output
Please input the value of a :
6
Please input the value of b :
9
After Swap
a: 9
b: 6
SFT (Shamil’s Flow Table )
Are you interested to Read about SFT(Shamil’s Flow Table)?