1
1
## Python Implementation
2
2
3
3
``` python
4
- def prime_num (num ):
4
+ def is_prime (num ):
5
5
if num <= 1 :
6
6
return False
7
- for i in range (2 , num):
7
+ for i in range (2 , num):
8
8
if num % i == 0 :
9
9
return False
10
10
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()
11
18
```
12
19
13
20
## Java Implementation
14
21
15
22
16
23
``` 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();
24
33
}
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 ;
27
45
}
28
46
}
29
47
```
@@ -46,5 +64,6 @@ In java we have a different approach. Unlike python we define our class using `(
46
64
47
65
2 . Java uses ` System.out.print() ` to print out the statement and python uses ` print() ` to print statement.
48
66
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