Skip to content

Commit 9b3bf57

Browse files
authored
chore: updates Ezra's code samples (#327)
1 parent 03d4f32 commit 9b3bf57

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

lesson_04/ezra4/README.md

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,47 @@
11
## Python Implementation
22

33
```python
4-
def prime_num(num):
4+
def is_prime(num):
55
if num <= 1:
66
return False
7-
for i in range(2, num):
7+
for i in range(2, num):
88
if num % i == 0:
99
return False
1010
return True
11+
12+
def main():
13+
num = int(input("Enter a number: "))
14+
print(f"{num} is prime." if is_prime(num) else f"{num} is not prime.")
15+
16+
if __name__ == "__main__":
17+
main()
1118
```
1219

1320
## Java Implementation
1421

1522

1623
```java
17-
public static void main(String[] args) {
18-
Scanner scanner = new Scanner(System.in);
19-
System.out.print("Enter a number:");
20-
int num = scanner.nextInt();
21-
22-
if (isPrime(num)) {
23-
System.out.println(num + " is prime.");
24+
import java.util.Scanner;
25+
26+
public class PrimeChecker {
27+
public static void main(String[] args) {
28+
Scanner scanner = new Scanner(System.in);
29+
System.out.print("Enter a number: ");
30+
int num = scanner.nextInt();
31+
System.out.println(num + (isPrime(num) ? " is prime." : " is not prime."));
32+
scanner.close();
2433
}
25-
else {
26-
System.out.println(num + " is not prime.");
34+
35+
public static boolean isPrime(int num) {
36+
if (num <= 1) {
37+
return false;
38+
}
39+
for (int i = 2; i < num; i++) {
40+
if (num % i == 0) {
41+
return false;
42+
}
43+
}
44+
return true;
2745
}
2846
}
2947
```
@@ -46,5 +64,6 @@ In java we have a different approach. Unlike python we define our class using `(
4664

4765
2. Java uses `System.out.print()` to print out the statement and python uses `print()` to print statement.
4866

49-
50-
3. I notice i have to import `java.util.Scanner` to prompt user to input a number while python you only need to use, `input()`
67+
3. I notice i have to import `java.util.Scanner` to prompt user to input a number while python you only need to use, `input()`
68+
4. Java uses `{}` for blocks and `;` to end statement while python doesn't need semicolons.
69+

0 commit comments

Comments
 (0)