Skip to content

new snippet - swap numbers #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions snippets/cpp/math-and-numbers/swap-numbers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: Swap numbers
description: Swaps two numbers without using third variable
author: Emosans
tags: swap,numbers
---

```cpp
#include<tuple>
std::tuple<int,int> swap(int num1,int num2){
num1=num1+num2;
num2=num1-num2;
num1=num1-num2;
return std::make_tuple(num1,num2);
}

// Usage
auto swapped=swap(3,4); // Returns a tuple (access the values using std::get<0>(swapped)/std::get<1>(swapped))
```