diff --git a/public/consolidated/c.json b/public/consolidated/c.json index a9a23234..1588cd69 100644 --- a/public/consolidated/c.json +++ b/public/consolidated/c.json @@ -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\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" } ] } diff --git a/snippets/c/mathematical-functions/swap-numbers.md b/snippets/c/mathematical-functions/swap-numbers.md new file mode 100644 index 00000000..bbbc4a2b --- /dev/null +++ b/snippets/c/mathematical-functions/swap-numbers.md @@ -0,0 +1,19 @@ +--- +title: Swap numbers +description: Swaps two numbers without using third variable +author: Emosans +tags: swap,numbers +--- + +```c +#include +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 +``` \ No newline at end of file