Skip to content

Commit a879367

Browse files
authored
feat: adds Chanel's code samples (#160)
1 parent 8bf2104 commit a879367

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
## Java implementation
2+
3+
```java
4+
public class PrimeCheck {
5+
public static boolean isPrime(int num) {
6+
if (num <= 1) return false;
7+
for (int i = 2; i<= Math.sqrt(num); i++) {
8+
if (num % i == 0) return false;
9+
}
10+
return true;
11+
}
12+
# Example usage:
13+
public static void main(String[] args) {
14+
int num = 29;
15+
System.out.println(isPrime(num)); // Output: true
16+
System.out.println(isPrime(4)); // Output: false
17+
}
18+
}
19+
```
20+
21+
## C++ implementation
22+
23+
24+
```C++
25+
26+
#include<iostream>
27+
#include <cmath>
28+
using namespace std;
29+
30+
bool isPrime(int num) {
31+
if (num <= 1) return false;
32+
for (int i = 2; i <= sqrt(num); i++) {
33+
if (num % i == 0) return false;
34+
}
35+
return true;
36+
}
37+
int main() {
38+
int num = 29;
39+
# Example usage:
40+
std::cout << isPrime(num) << std::endl;
41+
std::cout << isPrime(4) << std::endl;
42+
return 0;
43+
}
44+
45+
```
46+
47+
## Explanation
48+
The Java implementation uses a function named `isPrime`. Similiar to C++, the formula checks to see if a `num` varaible is less than or equal to 1, if so it will render a `false` result. If you have no variable that divides into your number (same logic as C++), it will render a `true` result.
49+
50+
The C++ implementation uses a function named `isPrime` that could give a `False` result if the `num` variable is less than or equal to 1 or if it divides equally or exactly (i.e., num % i == 0). If you have no variable that divides into your number it will render a `true` result.
51+
52+
53+
54+
55+
### Differences
56+
57+
In Java the text `public` means the method will be accessible and `PrimeCheck` is a method to check for prime or non prime numbers. `Boolean` in Java, is the code is an indicator method that will give an true or false value.
58+
59+
C++ includes "<iostream>" is an input-output stream that allows the function `std:: cout and cin` to be used for input-output operations. The `#include<cmath>` provides C++ math library that will include functions like square root; `std` is a prefix function that gives access to the C++ library. The `std` is a standard point for either input or output.
60+

0 commit comments

Comments
 (0)