Skip to content

Commit

Permalink
Merge pull request #172 from Emosans/Emosans/string-int-snippet
Browse files Browse the repository at this point in the history
new snippet - swap numbers
  • Loading branch information
Mathys-Gasnier authored Jan 4, 2025
2 parents de6a842 + e4b3b5f commit 621db73
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
11 changes: 11 additions & 0 deletions public/consolidated/c.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@
],
"contributors": [],
"code": "int factorial(int x) {\n int y = 1;\n\n for (int i = 2; i <= x; i++)\n y *= i;\n\n return y;\n}\n\n// Usage:\nfactorial(4); // Returns: 24\n"
},
{
"title": "Swap numbers",
"description": "Swaps two numbers without using third variable",
"author": "Emosans",
"tags": [
"swap",
"numbers"
],
"contributors": [],
"code": "#include<stdio.h>\nvoid swap(int* num1,int* num2){\n *num1 = *num1 + *num2;\n *num2 = *num1 - *num2;\n *num1 = *num1 - *num2;\n}\n\n// Usage:\nint a = 3,b = 4;\nswap(&a,&b); // simply use printf after this to print swapped values\n"
}
]
}
Expand Down
19 changes: 19 additions & 0 deletions snippets/c/mathematical-functions/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
---

```c
#include<stdio.h>
void swap(int* num1,int* num2){
*num1 = *num1 + *num2;
*num2 = *num1 - *num2;
*num1 = *num1 - *num2;
}

// Usage:
int a = 3,b = 4;
swap(&a,&b); // swaps the values of the a and b variables
```

0 comments on commit 621db73

Please sign in to comment.