Skip to content

added odd even number check #125

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

Closed
wants to merge 8 commits into from
Closed
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
25 changes: 25 additions & 0 deletions public/consolidated/cpp.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,37 @@
],
"contributors": [],
"code": "bool is_prime(int n) {\n if (n < 2) return false;\n if (n == 2 || n == 3) return true;\n if (n % 2 == 0) return false;\n for (int i = 3; i * i <= n; i += 2) {\n if (n % i == 0) return false;\n }\n return true;\n}\n\n// Usage\n#include <iostream>\n\nint main() {\n std::cout << is_prime(29) << std::endl; // Output: 1\n return 0;\n}\n"
},
{
"title": "Even-Odd Check",
"description": "This code demonstrates the fastest way to check if a number is even or odd using bitwise operations. It's more efficient than using the modulor operator (%) since bitwise operations are performed at the CPU level.",
"author": "Beast177 ",
"tags": [
"cpp",
"number",
"even",
"odd",
"bitwise"
],
"contributors": [],
"code": "\nbool isEven(int num) {\n return !(num & 1);\n}\n\n// usage -- [odd-even]\n\n#include <iostream>\n\nint main(){\n\n int num1 = 2;\n int num2 = 3;\n\n std::cout << num1 << \" is \" << (isEven(num1) ? \"Even\" : \"Odd\") << \"\\n\"; // output - [Even] \n std::cout << num2 << \" is \" << (isEven(num2) ? \"Even\" : \"Odd\") << \"\\n\"; // output - [Odd]\n}\n\n"
}
]
},
{
"categoryName": "String Manipulation",
"snippets": [
{
"title": "Count Occurrences",
"description": "Counts occurrences of a substring, uses modern C++ 20",
"author": "beast177",
"tags": [
"cpp",
"string"
],
"contributors": [],
"code": "#include <iostream>\n#include <string>\n#include <string_view>\n\nstatic size_t countOccurrences(std::string_view text, std::string_view substr) {\n size_t count = 0;\n size_t pos = 0;\n while ((pos = text.find(substr, pos)) != std::string_view::npos) {\n ++count;\n pos += substr.length();\n }\n return count;\n}\n\n// use case --\n\nint main(){\n // Count and replace\n std::string text = \"Hello, world\";\n\n auto count = countOccurrences(text, \"l\");\n std::cout << std::format(\"Count of 'l': {}\\n\", count); // output -> Count of 'l' : 3\n}\n"
},
{
"title": "Reverse String",
"description": "Reverses the characters in a string.",
Expand Down
28 changes: 28 additions & 0 deletions snippets/cpp/math-and-numbers/odd-even-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: Even-Odd Check
description: This code demonstrates the fastest way to check if a number is even or odd using bitwise operations. It's more efficient than using the modulor operator (%) since bitwise operations are performed at the CPU level.
tags: cpp, number, even, odd, bitwise
author: Beast177
---

```cpp

bool isEven(int num) {
return !(num & 1);
}

// usage -- [odd-even]

#include <iostream>

int main(){

int num1 = 2;
int num2 = 3;

std::cout << num1 << " is " << (isEven(num1) ? "Even" : "Odd") << "\n"; // output - [Even]
std::cout << num2 << " is " << (isEven(num2) ? "Even" : "Odd") << "\n"; // output - [Odd]
}

```

32 changes: 32 additions & 0 deletions snippets/cpp/string-manipulation/count-occurrences.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
title: Count Occurrences
description: Counts occurrences of a substring, uses modern C++ 20
author: beast177
tags: cpp,string
---

```cpp
#include <iostream>
#include <string>
#include <string_view>

static size_t countOccurrences(std::string_view text, std::string_view substr) {
size_t count = 0;
size_t pos = 0;
while ((pos = text.find(substr, pos)) != std::string_view::npos) {
++count;
pos += substr.length();
}
return count;
}

// use case --

int main(){
// Count and replace
std::string text = "Hello, world";

auto count = countOccurrences(text, "l");
std::cout << std::format("Count of 'l': {}\n", count); // output -> Count of 'l' : 3
}
```