|
| 1 | +## JAVA Implementation |
| 2 | + |
| 3 | +```java |
| 4 | + |
| 5 | +public class EvenCheck { |
| 6 | + // Method to check if a number is even |
| 7 | + public static boolean isEven(int number) { |
| 8 | + return number % 2 == 0; |
| 9 | + } |
| 10 | + |
| 11 | + // Main method for running the program |
| 12 | + public static void main(String[] args) { |
| 13 | + System.out.println(isEven(4)); // Output: true |
| 14 | + System.out.println(isEven(7)); // Output: false |
| 15 | + } |
| 16 | +} |
| 17 | +``` |
| 18 | + |
| 19 | + |
| 20 | +## C++ Implementation |
| 21 | +#include <iostream> |
| 22 | +using namespace std; |
| 23 | + |
| 24 | +``` |
| 25 | +
|
| 26 | +// Function to check if a number is even |
| 27 | +bool isEven(int number) { |
| 28 | + return number % 2 == 0; |
| 29 | +} |
| 30 | +
|
| 31 | +int main() { |
| 32 | +
|
| 33 | + cout << isEven(4) << endl; // Output: 1 (true) |
| 34 | + cout << isEven(7) << endl; // Output: 0 (false) |
| 35 | + return 0; |
| 36 | +} |
| 37 | +
|
| 38 | +``` |
| 39 | + |
| 40 | +## Explanation |
| 41 | + |
| 42 | +First I want to talk about a prime number. A prime number is a natural number only divisible by 1 and its self. |
| 43 | + |
| 44 | +Implementing it into code; |
| 45 | + |
| 46 | + |
| 47 | +The "Java" implementation uses a function named called IsPrime. This Method needs a number as an imput and checks if its prime. The first being if the number is less than or equal to 1. If the results arent a prime number the method is false. However it runs a loop starting from 2 to the sqrt of the number. Being that if the number is divisible by anything in that range the meothod will be false. if no divide then method is true. |
| 48 | + |
| 49 | + |
| 50 | +The "C++" Implementation also uses the function IsPrime. This function also works in a simimlar way then java. In thid method you need to first check if the number is 1 or less than, which returns false in that sense. After, the function literally loops from 2 up to the sqrt of the number simlar to java, looking for nay divisors. If i does find a divide then this method will return false. Concluding that if no divisor was found then menthod remains true. |
| 51 | + |
| 52 | +# Similarities and Differences Index Chart |
| 53 | + |
| 54 | +| Feature | Similarities | Differences | |
| 55 | +|-----------------------|-----------------------------------------------|-----------------------------------------------| |
| 56 | +| **Syntax** | Both programs usse a function with a simlilar purpose | Java uses "IsPrime" as C++ uses "isPrime" but written out differently.| |
| 57 | +| **Input** | Both take number input to check if output is prime. | Java has a personal writting style while C++ follows an ordered structure.| |
| 58 | +| **Main Function** | Both have main start section to run code. | Java uses "public static void Main()" C++ uses "int main()" to start.| |
| 59 | +| **First Line of Code** | Both need a special line including designated tools for code to start and run. | Java starts with "public class;". C++ starts with "#include <iostream>"| |
| 60 | +| **Printing Output** | Both Java and C++ use printing outputs to show if number is prime. | Java uses "System.out.print()". C++ uses "cout" but, for larger programs uses std::cout to avoid potential name conflicts.| |
0 commit comments