Skip to content

Commit fc38bc6

Browse files
feat: adds Jason code samples (#149)
* Lesson_04 * Lesson_04 * Feat: Jason lesson_04 ReadMe * chore: revert lesson_03 changes Signed-off-by: Anthony D. Mays <[email protected]> --------- Signed-off-by: Anthony D. Mays <[email protected]> Co-authored-by: Anthony D. Mays <[email protected]>
1 parent c4a2b7e commit fc38bc6

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

lesson_04/JasonWatson/README.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
## C
2+
3+
```C
4+
#include <stdio.h>
5+
#include <stdbool.h>
6+
7+
bool isPrime(int n) {
8+
if (n <= 1) return false;
9+
for (int i = 2; i * i <= n; i++) {
10+
if (n % i == 0) return false;
11+
}
12+
return true;
13+
}
14+
15+
int main() {
16+
int number = 29;
17+
if (isPrime(number))
18+
printf("%d is a prime number . \n", number );
19+
else
20+
printf("%d is not a prime number . \n", number );
21+
return 0;
22+
}
23+
```
24+
25+
## C++
26+
27+
```Cpp
28+
#include <iostream>
29+
#include <cmath>
30+
31+
bool isPrime(int n) {
32+
if (n <= 1) return false;
33+
for (int i = 2; i <= sqrt(n); i++) {
34+
if (n % i == 0) return false;
35+
}
36+
return true;
37+
}
38+
39+
int main() {
40+
int number = 29;
41+
std::cout << std::boolalpha <<
42+
isPrime(number) << std::end1;
43+
return 0;
44+
}
45+
```
46+
47+
## Explanation
48+
This C program checks if a given nember is a prime number.It uses a function isPrime() to then determine whether a number is a prime number and the gives the output.
49+
50+
This C++ program checks when a given number is a prime number by using the simple function isPrime() and the gives an out. It does so by using the <iostream> which is a library for input/output and the <cmath> for calculation.
51+
52+
53+
### DIFFERENCES
54+
55+
1. **Syntax**:
56+
- In C it uses a standard input-output library which gives us the ability to use functions such as print() to give output.
57+
- In C++ it uses a standard input-output stream library which makes us be able to use std::cout and std::endl to display the output.
58+
59+
2. **How they carry out calculation**:
60+
- C uses the bool data type to which provides true and false values for logical operation whilst the C++ use a math library to do mathematical functions such as sqrt() that calculkates square root numbers.
61+
62+
63+
64+
### SIMILARITIES
65+
66+
1. **Codes**:
67+
- Both C and C++ use the function if (n <= 1) return false to determine if the number is a prime number.
68+
- They both have the ability to do loop for Checking Divisibility.
69+
- the function return true; is use by both programs to return true if No Divisors found.
70+

0 commit comments

Comments
 (0)