Skip to content

Added code snippets of C++ on bit manipulation, math & number, searching, sorting #217

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 5 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions snippets/c/bit-manipulation/check-power-of-two.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: Check Power of Two
description: Checks if a given number is a power of two using bitwise operations.
tags: bit-manipulation, power-of-two
author: ashukr07
---

```c
#include <stdbool.h> // Include the standard boolean library

bool is_power_of_two(int n) {
return n > 0 && (n & (n - 1)) == 0; // Bitwise check for power of two
}

// Usage:
is_power_of_two(16); // Returns: true
is_power_of_two(18); // Returns: false
```
21 changes: 21 additions & 0 deletions snippets/c/bit-manipulation/reverse-bits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: Reverse Bits
description: Reverses the bits of a given unsigned integer.
tags: bit-manipulation, reverse-bits
author: ashukr07
---

```c
unsigned int reverse_bits(unsigned int n) {
unsigned int result = 0;
for (int i = 0; i < 32; ++i) {
result <<= 1; // Shift result left by 1
result |= n & 1; // Add the least significant bit of n to result
n >>= 1; // Shift n right by 1
}
return result;
}

// Usage:
reverse_bits(43261596); // Returns: 964176192 (Binary: 00000010100101000001111010011100 -> 00111001011110000010100101000000)
```
18 changes: 18 additions & 0 deletions snippets/c/bit-manipulation/xor-of-range.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: XOR of Range
description: Finds XOR of all numbers from 1 to n using properties of XOR.
tags: bit-manipulation, xor
author: ashukr07
---

```c
int xor_upto_n(int n) {
if (n % 4 == 0) return n;
if (n % 4 == 1) return 1;
if (n % 4 == 2) return n + 1;
return 0;
}

// Usage:
xor_upto_n(5); // Returns: 1 (1 ^ 2 ^ 3 ^ 4 ^ 5 = 1)
```
28 changes: 28 additions & 0 deletions snippets/c/mathematical-functions/check-perfect-number.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: Check Perfect Number
description: Checks if a number is a perfect number. A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself).
tags: math, perfect-number
author: ashukr07
---

```c
#include <stdbool.h>

// Function to check if a number is a perfect number
bool is_perfect(int n) {
if (n <= 1) return false;

int sum = 1; // 1 is a divisor for all n > 1
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
sum += i;
if (i != n / i) sum += n / i;
}
}
return sum == n;
}

// Usage
is_perfect(28); // Returns: true
is_perfect(12); // Returns: false
```
23 changes: 23 additions & 0 deletions snippets/c/mathematical-functions/compound-interest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: Compound Interest
description: Calculates the compound interest for a given principal, rate, time, and number of times interest applied per time period.
tags: math, finance
author: ashukr07
---

```c
#include <math.h>

// Function to calculate compound interest
double compound_interest(double principal, double rate, double time, int n) {
return principal * pow(1 + rate / n, n * time);
}

// Usage:
double principal = 1000.0; // Initial amount
double rate = 0.05; // Annual interest rate (5%)
double time = 2; // Time in years
int n = 4; // Compounded quarterly

compound_interest(principal, rate, time, n); // Returns: 1104.081632653061
```
17 changes: 17 additions & 0 deletions snippets/c/mathematical-functions/fibonacci-number.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: Fibonacci Number
description: Calculates the nth Fibonacci number using recursion.
tags: math, fibonacci, recursion
author: ashukr07
---

```c
// Function to calculate the nth Fibonacci number
int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}

// Usage:
fibonacci(6); // Returns: 8
```
21 changes: 21 additions & 0 deletions snippets/c/mathematical-functions/sum-of-digits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: Sum of Digits
description: Calculates the sum of the digits of an integer.
tags: math, digits
author: ashukr07
---

```c
// Function to calculate the sum of the digits of an integer
int sum_of_digits(int n) {
int sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
}
return sum;
}

// Usage:
sum_of_digits(123); // Returns: 6
```
20 changes: 20 additions & 0 deletions snippets/cpp/bit-manipulation/find-non-repeating-number.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: Find Non-Repeating Number
description: Finds the number that appears only once in an array where every other number appears twice.
tags: bit-manipulation, xor
author: ashukr07
---

```cpp
int find_non_repeating(const std::vector<int>& nums) {
int result = 0;
for (int num : nums) {
result ^= num;
}
return result;
}

// Usage:
std::vector<int> nums = {4, 1, 2, 1, 2};
find_non_repeating(nums); // Returns: 4
```
29 changes: 0 additions & 29 deletions snippets/cpp/debuging/vector-print.md

This file was deleted.

25 changes: 25 additions & 0 deletions snippets/cpp/math-and-numbers/binary-to-decimal-conversion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: Binary to Decimal Conversion
description: Converts a binary number represented as a string to its decimal equivalent.
tags: binary, conversion
author: ashukr07
---

```cpp
int binary_to_decimal(const std::string& binary) {
int decimal = 0;
int base = 1; // Base value for the least significant bit

for (int i = binary.length() - 1; i >= 0; --i) {
if (binary[i] == '1') {
decimal += base;
}
base *= 2; // Move to the next power of 2
}
return decimal;
}

// Usage:
std::string binary = "1011"; // Binary representation of 11
binary_to_decimal(binary); // Returns: 11
```
Loading