Skip to content

Commit 621db73

Browse files
Merge pull request #172 from Emosans/Emosans/string-int-snippet
new snippet - swap numbers
2 parents de6a842 + e4b3b5f commit 621db73

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

public/consolidated/c.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@
2828
],
2929
"contributors": [],
3030
"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"
31+
},
32+
{
33+
"title": "Swap numbers",
34+
"description": "Swaps two numbers without using third variable",
35+
"author": "Emosans",
36+
"tags": [
37+
"swap",
38+
"numbers"
39+
],
40+
"contributors": [],
41+
"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"
3142
}
3243
]
3344
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Swap numbers
3+
description: Swaps two numbers without using third variable
4+
author: Emosans
5+
tags: swap,numbers
6+
---
7+
8+
```c
9+
#include<stdio.h>
10+
void swap(int* num1,int* num2){
11+
*num1 = *num1 + *num2;
12+
*num2 = *num1 - *num2;
13+
*num1 = *num1 - *num2;
14+
}
15+
16+
// Usage:
17+
int a = 3,b = 4;
18+
swap(&a,&b); // swaps the values of the a and b variables
19+
```

0 commit comments

Comments
 (0)