Write a C plus plus program to interchange two numbers by using function?

1 answer

Answer

1293288

2026-07-16 18:11

+ Follow

#include<iOStream>

template<typename _Ty>void swap (_Ty& a, _Ty& b)

{

_Ty temp = a;

a = b;

b = temp;

}

int main()

{

int x = 42;

int y = 0;

std::cout << "Before swap:" << std::endl;

std::cout << "x = " << x << ", y = " << y << std::endl;

swap (x, y);

std::cout << "After swap:" << std::endl;

std::cout << "x = " << x << ", y = " << y << std::endl;

}

Example Output

Before swap:

x = 42, y = 0

After swap:

x = 0, y = 42

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.