Tuesday, October 25, 2022

C++ Problem (8) : How to swap two numbers? Using third variable.

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

 How to create a shortcut for a program(application) on windows? Fisrt of all if you are at desktop screen, click right on mouse and click o...