C++ (8) : How to swap two numbers? Using third variable.
Code:
#include<iostream>
using namespace std;
int main(){
double a,b,c;
cout<<"*****Swapping two numbers using third variable.*****"<<endl;
cout<<"Enter first number."<<endl;
cin>>a;
cout<<"Enter second number."<<endl;
cin>>b;
cout<<"The given numbers are:"<<endl;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
c=a;
a=b;
b=c;
cout<<"The numbers after swapping."<<endl;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
return 0;
}
Output:
*****Swapping two numbers using third variable.*****
Enter first number.
10
Enter second number.
5
The given numbers are:
a = 10
b = 5
The numbers after swapping.
a = 5
b = 10
No comments:
Post a Comment