|
1 |
| -## Python implementation |
2 |
| - |
3 |
| -```python |
4 |
| -def is_even(number): |
5 |
| - return number % 2 == 0 |
| 1 | +## C |
| 2 | + |
| 3 | +```c |
| 4 | +#include <stdio.h> |
| 5 | +#include <stdbool.h> |
| 6 | + |
| 7 | +bool isPrime(int n) { |
| 8 | + if (n <= 1) return false; |
| 9 | + for (int i = 2; i <= sqrt(n); i++) { |
| 10 | + if (n % i == 0) return false; |
| 11 | + } |
| 12 | + return true; |
| 13 | +} |
6 | 14 |
|
7 |
| -# Example usage: |
8 |
| -print(is_even(4)) # Output: True |
9 |
| -print(is_even(7)) # Output: False |
| 15 | +nt main() { |
| 16 | + int number = 29; |
| 17 | + if (isPrime(number)) |
| 18 | + printf("%d is a prime number . \n", ) |
| 19 | + return 0; |
| 20 | +} |
10 | 21 | ```
|
11 | 22 |
|
12 |
| -## JavaScript implementation |
| 23 | +## C++ |
13 | 24 |
|
14 |
| -```javascript |
15 |
| -function isEven(number) { |
16 |
| - return number % 2 === 0; |
| 25 | +```Cpp |
| 26 | +#include <iostream> |
| 27 | +#include <cmath> |
| 28 | +
|
| 29 | +bool isPrime(int n) { |
| 30 | + if (n <= 1) return false; |
| 31 | + for (int i = 2; i <= sqrt(n); i++) { |
| 32 | + if (n % i == 0) return false; |
| 33 | + } |
| 34 | + return true; |
17 | 35 | }
|
18 | 36 |
|
19 |
| -// Example usage: |
20 |
| -console.log(isEven(4)); // Output: true |
21 |
| -console.log(isEven(7)); // Output: false |
| 37 | +int main() { |
| 38 | + int number = 29; |
| 39 | + std::cout << std::boolalpha << |
| 40 | +isPrime(number) << std::end1; |
| 41 | + return 0; |
| 42 | +} |
22 | 43 | ```
|
23 | 44 |
|
24 | 45 | ## Explanation
|
25 | 46 |
|
26 |
| -The Python implementation uses a function named `is_even` that takes a single argument `number`. It returns `True` if the number is even (i.e., when the remainder of the division of the number by 2 is zero), otherwise, it returns `False`. |
27 | 47 |
|
28 |
| -The JavaScript implementation uses a function named `isEven` that also takes a single argument `number`. It returns `true` if the number is even (using the same logic as the Python function) and `false` otherwise. |
29 | 48 |
|
30 | 49 | ### Differences
|
31 | 50 |
|
32 |
| -1. **Syntax**: |
33 |
| - - In Python, functions are defined using the `def` keyword, whereas in JavaScript, the `function` keyword is used. |
34 |
| - - Python uses `True` and `False` for boolean values, while JavaScript uses `true` and `false`. |
| 51 | +1. ** |
35 | 52 |
|
36 |
| -2. **Type Coercion**: |
37 |
| - - JavaScript has type coercion, which can sometimes lead to unexpected results if the input is not properly handled. In contrast, Python is more strict with types. |
| 53 | +2. ** |
38 | 54 |
|
39 |
| -3. **Function Calls**: |
40 |
| - - The syntax for calling functions and printing to the console/output is slightly different. Python uses `print()`, while JavaScript uses `console.log()`. |
| 55 | +3. ** |
0 commit comments