Skip to content

Commit 7438405

Browse files
Dadenaike251Dadenaike251
andauthored
feat: adds David's code samples (#170)
* Completed HW - No Comments * meant to make one commit --------- Co-authored-by: Dadenaike251 <[email protected]>
1 parent e814e63 commit 7438405

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

lesson_04/davidadenaike/readme.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
2+
## Python implementation
3+
4+
```python
5+
def is_even(number):
6+
return number % 2 == 0
7+
8+
# Example usage:
9+
print(is_even(4)) # Output: True
10+
print(is_even(7)) # Output: False
11+
```
12+
13+
## JavaScript implementation
14+
15+
```javascript
16+
function isEven(number) {
17+
return number % 2 === 0;
18+
}
19+
20+
// Example usage:
21+
console.log(isEven(4)); // Output: true
22+
console.log(isEven(7)); // Output: false
23+
```
24+
25+
note need to write 100 words explanation of the two
26+
-->
27+
28+
## JavaScript
29+
```java script
30+
31+
function isPrime(number) {
32+
if (number <= 1) {
33+
return false; // Numbers less than or equal to 1 are not prime
34+
}
35+
36+
for (let i = 2; i <= Math.sqrt(number); i++) {
37+
if (number % i === 0) {
38+
return false; // If divisible by any number other than 1 and itself, it's not prime
39+
}
40+
}
41+
42+
return true; // If no divisors found, the number is prime
43+
}
44+
```
45+
46+
47+
## Java
48+
```java
49+
import java.util.Scanner;
50+
51+
public class PrimeNumberChecker {
52+
53+
// Method to check if a number is prime
54+
public static boolean isPrime(int num) {
55+
// Handle cases for numbers less than 2
56+
if (num <= 1) {
57+
return false;
58+
}
59+
60+
// Check divisibility from 2 to the square root of num
61+
for (int i = 2; i * i <= num; i++) {
62+
if (num % i == 0) {
63+
return false; // num is divisible by i, so it's not prime
64+
}
65+
}
66+
67+
return true; // num is prime if no divisors were found
68+
}
69+
70+
public static void main(String[] args) {
71+
Scanner scanner = new Scanner(System.in);
72+
73+
// Input number from user
74+
System.out.print("Enter a number: ");
75+
int number = scanner.nextInt();
76+
77+
// Check if the number is prime
78+
if (isPrime(number)) {
79+
System.out.println(number + " is a prime number.");
80+
} else {
81+
System.out.println(number + " is not a prime number.");
82+
}
83+
84+
scanner.close();
85+
}
86+
}
87+
```
88+
89+
## Explanation
90+
91+
Java uses a method called `PrimeNumberChecker` to check if a number is prime. The method is made up of two components. An `if` statement and a `for` loop. The `if` statement handles numbers that are less than 2. As the for loop then checks for divisibility from 2 to the square root of the number represented as int “i”. Both the if statement and for loop is nested within a public class `PrimeNumberChecker`
92+
93+
JavaScript however is more straight forward. It uses a function called `‘isPrime’` that checks numbers that are less than or equal to 1. Once it does that, it returns false since the result would not be a prime number. Afterward an `if` statement is used, similarly to java. Then a `for` loop checks if the number is divisible by any number other than 1 and itself.
94+
95+
### Differences
96+
Biggest difference is that java because it is an object-oriented language has the method `‘PrimeNumberChecker’` in a class to use.
97+
98+
### Similarities:
99+
Both languages use an if statement and a for loop to determine whether a number is prime or not.

0 commit comments

Comments
 (0)