diff --git a/snippets/c/bit-manipulation/check-power-of-two.md b/snippets/c/bit-manipulation/check-power-of-two.md new file mode 100644 index 00000000..904c6bac --- /dev/null +++ b/snippets/c/bit-manipulation/check-power-of-two.md @@ -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 // 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 +``` \ No newline at end of file diff --git a/snippets/c/bit-manipulation/reverse-bits.md b/snippets/c/bit-manipulation/reverse-bits.md new file mode 100644 index 00000000..23c6279f --- /dev/null +++ b/snippets/c/bit-manipulation/reverse-bits.md @@ -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) +``` \ No newline at end of file diff --git a/snippets/c/bit-manipulation/xor-of-range.md b/snippets/c/bit-manipulation/xor-of-range.md new file mode 100644 index 00000000..4afed9bd --- /dev/null +++ b/snippets/c/bit-manipulation/xor-of-range.md @@ -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) +``` \ No newline at end of file diff --git a/snippets/c/mathematical-functions/check-perfect-number.md b/snippets/c/mathematical-functions/check-perfect-number.md new file mode 100644 index 00000000..a155378a --- /dev/null +++ b/snippets/c/mathematical-functions/check-perfect-number.md @@ -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 + +// 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 +``` \ No newline at end of file diff --git a/snippets/c/mathematical-functions/compound-interest.md b/snippets/c/mathematical-functions/compound-interest.md new file mode 100644 index 00000000..230b3b4e --- /dev/null +++ b/snippets/c/mathematical-functions/compound-interest.md @@ -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 + +// 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 +``` \ No newline at end of file diff --git a/snippets/c/mathematical-functions/fibonacci-number.md b/snippets/c/mathematical-functions/fibonacci-number.md new file mode 100644 index 00000000..8855718f --- /dev/null +++ b/snippets/c/mathematical-functions/fibonacci-number.md @@ -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 +``` \ No newline at end of file diff --git a/snippets/c/mathematical-functions/sum-of-digits.md b/snippets/c/mathematical-functions/sum-of-digits.md new file mode 100644 index 00000000..bc6c4837 --- /dev/null +++ b/snippets/c/mathematical-functions/sum-of-digits.md @@ -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 +``` \ No newline at end of file diff --git a/snippets/cpp/bit-manipulation/find-non-repeating-number.md b/snippets/cpp/bit-manipulation/find-non-repeating-number.md new file mode 100644 index 00000000..1195587d --- /dev/null +++ b/snippets/cpp/bit-manipulation/find-non-repeating-number.md @@ -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& nums) { + int result = 0; + for (int num : nums) { + result ^= num; + } + return result; +} + +// Usage: +std::vector nums = {4, 1, 2, 1, 2}; +find_non_repeating(nums); // Returns: 4 +``` \ No newline at end of file diff --git a/snippets/cpp/debuging/vector-print.md b/snippets/cpp/debuging/vector-print.md deleted file mode 100644 index 9fe8550b..00000000 --- a/snippets/cpp/debuging/vector-print.md +++ /dev/null @@ -1,29 +0,0 @@ ---- -title: Vector Print -description: Overloads the << operator to print the contents of a vector just like in python. -author: Mohamed-faaris -tags: printing,debuging,vector ---- - -```cpp -#include -#include - -template -std::ostream& operator<<(std::ostream& os, const std::vector& vec) { - os << "["; - for (size_t i = 0; i < vec.size(); ++i) { - os << vec[i]; // Print each vector element - if (i != vec.size() - 1) { - os << ", "; // Add separator - } - } - os << "]"; - return os; // Return the stream -} - -// Usage: -std::vector numbers = {1, 2, 3, 4, 5}; -std::cout << numbers << std::endl; // Outputs: [1, 2, 3, 4, 5] - -``` diff --git a/snippets/cpp/math-and-numbers/binary-to-decimal-conversion.md b/snippets/cpp/math-and-numbers/binary-to-decimal-conversion.md new file mode 100644 index 00000000..b2a607cb --- /dev/null +++ b/snippets/cpp/math-and-numbers/binary-to-decimal-conversion.md @@ -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 +``` \ No newline at end of file