Skip to content

Commit c5c30de

Browse files
enyabutiEzra Nyabuti
andauthored
feat: Ezra's lesson 4 assignment (#150)
* feat: Ezra's lesson 4 assignment * fix: Made minor changes. * fix: Deleted PrimeNumber.java file * feat: Ezra's lesson 4 assignment * fix: Made minor changes. * fix: Fixed the indentation and removed the priod. * fix: Fixed the indentation and removed the priod. * Delete PrimeNumber.java --------- Co-authored-by: Ezra Nyabuti <[email protected]>
1 parent fc38bc6 commit c5c30de

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

lesson_04/ezra4/PrimeNumber.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.Scanner; // This package for reading input
2+
3+
public class PrimeNumber { // This is the class name
4+
5+
public static boolean isPrime(int num) { // Function to check if number is prime or not.
6+
if (num <= 1) return false; // If number is less than or equal to 1, it is not prime.
7+
for (int i = 2; i <= Math.sqrt(num); i++) { // Loop to check if number is prime or not.
8+
if (num % i == 0) return false;
9+
}
10+
return true;
11+
}
12+
13+
public static void main(String[] args) { // This is the main function
14+
Scanner scanner = new Scanner(System.in);
15+
System.out.print("Enter a number:");
16+
int num = scanner.nextInt();
17+
18+
if (isPrime(num)) {
19+
System.out.println(num + " is prime.");
20+
} else {
21+
System.out.println(num + " is not prime.");
22+
}
23+
}
24+
}

lesson_04/ezra4/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## Python Implementation
2+
3+
```python
4+
def prime_num(num):
5+
if num <= 1:
6+
return False
7+
for i in range(2, num):
8+
if num % i == 0:
9+
return False
10+
return True
11+
```
12+
13+
## Java Implementation
14+
15+
16+
```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+
}
25+
else {
26+
System.out.println(num + " is not prime.");
27+
}
28+
}
29+
```
30+
31+
32+
### Python Explanations
33+
When we look at the python implementation, we define a function and give it and attribute `(num)`. We introduce the `if` statement and `for` loop to iterate over the funtion and check if it matches the requirements. We later propmt user to input a number and the code will verify if all the requirements are met to qualify it as a prime number.
34+
35+
36+
37+
### Java Explanation
38+
39+
In java we have a different approach. Unlike python we define our class using `(public class)` named `PrimeNumber`.Both Python and java use conditional statement loops to iterate over the functions. The function is later iterated on the main class
40+
41+
42+
43+
### Difference
44+
45+
1. Python uses `def` keyword to define a funtion while Java uses, `public static boolean` to define a function.
46+
47+
2. Java uses `System.out.print()` to print out the statement and python uses `print()` to print statement.
48+
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()`

lesson_04/ezra4/numbers.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
# Function to check if a number is prime
3+
4+
def prime_num(num): # This is function to check if a number is prime
5+
if num <= 1: # This will help check if number is less than or equal to 1
6+
return False
7+
for i in range(2, num): # This will help check if number is divisible by any number between 2 and the number itself
8+
if num % i == 0:
9+
return False
10+
return True
11+
12+
13+
# Ask user for a number and check if it is prime
14+
number = int(input("Enter a number: "))
15+
if prime_num(number):
16+
print(f"{number} is a prime number.")
17+
else:
18+
print(f"{number} is not a prime number.")

0 commit comments

Comments
 (0)