Swap the values of variables Program in C++ and C with the flowchart
Flowchart to Swap the values of variables

Program in C++ to swap the values of variables
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include<iostream> using namespace std; int main() { float first_veri,Second_veri,Temp_veri; cout<<"Enter value of 1st variable: "; cin>>first_veri; cout<<"Enter value of 2nd veriable: "; cin>>Second_veri; Temp_veri=first_veri; first_veri=Second_veri; Second_veri=Temp_veri; cout<<"After swaping the values in both veriables:"<<endl; cout<<"1st veriable: "<<first_veri<<endl; cout<<"2nd veriable: "<<Second_veri<<endl; return 0; } |
Output

Program in C++ to swap the values of variables using if-else statement
Let’s see the “Program in C++ to swap the values of variables using the if-else 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 25 26 |
#include<iostream> using namespace std; int main() { int a,b,c; cout<<"Please Give a value to 'a' "<<endl; cin>>a; cout<<"please Assign another value to 'b' "<<endl; cin>>b; cout<<"The value of 'a' and 'b' is = "<<a<<" & "<<b<<" respectively.."<<endl; if(c==0) { c=a; a=b; b=c; } else { cout<<endl; } cout<<"Now the value of 'a' and 'b' is = "<<a<<" & "<<b<<" respectively.."<<endl; } |
Program in C++ to swap the values of variables using User Define Functions
Let’s see the “Program in C++ to swap the values of variables using the user define functions”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include<iostream> using namespace std; int swap(int&,int&); int main() { int a,b,c; cout<<"Please Give a value to 'a' "<<endl; cin>>a; cout<<"please Assign another value to 'b' "<<endl; cin>>b; cout<<"The values of 'a' and 'b' were = "<<a<<" & "<<b<<" respectively.."<<endl; swap(a,b); cout<<endl<<"Now the values of 'a' and 'b' are = "<<a<<" & "<<b<<" respectively.."<<endl; } int swap(int &a, int &b) { int c=0; int p=1; int i=0; while(i<p) { c=a; a=b; b=c; p=0; i++; } } |
Program in C to swap the values of variables
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> int main() { double oneNumber, twoNumber, temporarynumber; printf("Enter first number:\n "); scanf("%lf", &oneNumber); printf("Enter second number:\n "); scanf("%lf",&twoNumber); temporarynumber = oneNumber; oneNumber = twoNumber; twoNumber = temporarynumber; printf("\nAfter swapping, values in 1st variable wiil be = \n%.2lf\n", oneNumber); printf("After swapping, values in second veriable = \n%.2lf", twoNumber); } |
Output
