Skip to content

Commit 8121612

Browse files
committed
Lesson_04
1 parent d9c68df commit 8121612

File tree

1 file changed

+39
-24
lines changed

1 file changed

+39
-24
lines changed

lesson_04/JasonWatson/README.md

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,55 @@
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+
}
614

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+
}
1021
```
1122
12-
## JavaScript implementation
23+
## C++
1324
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;
1735
}
1836
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+
}
2243
```
2344

2445
## Explanation
2546

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`.
2747

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.
2948

3049
### Differences
3150

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. **
3552

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. **
3854

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

Comments
 (0)