You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lesson_04/JasonWatson/README.md
+24-9Lines changed: 24 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,21 +1,23 @@
1
1
## C
2
2
3
-
```c
3
+
```C
4
4
#include<stdio.h>
5
5
#include<stdbool.h>
6
6
7
7
boolisPrime(int n) {
8
8
if (n <= 1) return false;
9
-
for (int i = 2; i <= sqrt(n); i++) {
9
+
for (int i = 2; i * i <= n; i++) {
10
10
if (n % i == 0) return false;
11
11
}
12
12
return true;
13
13
}
14
14
15
-
nt main() {
15
+
int main() {
16
16
int number = 29;
17
17
if (isPrime(number))
18
-
printf("%d is a prime number . \n", )
18
+
printf("%d is a prime number . \n", number );
19
+
else
20
+
printf("%d is not a prime number . \n", number );
19
21
return 0;
20
22
}
21
23
```
@@ -43,13 +45,26 @@ isPrime(number) << std::end1;
43
45
```
44
46
45
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.
46
61
47
62
48
63
49
-
### Differences
64
+
### SIMILARITIES
50
65
51
-
1.**
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.
0 commit comments