Answers for "build swap function c++"

C++
1

swap using Function template in c++

#include <bits/stdc++.h>
using namespace std;
template<class T , class T1>
void excha(T &a , T1 &b){
    T temp = a;
    a = b;
    b = temp;
    
}
int main(){
    // int a , b;
    // cout<<"Enter the value of a and B "<<endl;
    // cin>>a>>b;
    // excha(a,b);
    // cout<<a<<" "<<b<<endl;

    // cout<<endl;

    // float a , b;
    // cout<<"Enter the value of a and B "<<endl;
    // cin>>a>>b;
    // excha(a,b);
    // cout<<a<<" "<<b<<endl;

    cout<<endl;

    float a ;int b;
    cout<<"Enter the value of a and B "<<endl;
    cin>>a;
    cin>>b;
    excha(a,b);
    cout<<a<<" "<<b<<endl;

    return 0;
}
Posted by: Guest on September-11-2021
3

C++ Swap Function

int a, b;

    cout << "Input first number: ";
    cin >> a;
    cout << "Input second number: ";
    cin >> b;

    swap (a, b);

    cout << "After swapping the first number: " << a << endl;
    cout << "After swapping the second number: " << b << endl;
Posted by: Guest on March-13-2022

Browse Popular Code Answers by Language