Skip to content

Commit 421f9cc

Browse files
Added Scalar Product and Prime Number Checker
1 parent ef9a4e5 commit 421f9cc

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

assignment-3-simple_C/Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
main: main.c
2+
gcc -o main -ansi -pedantic-errors -Wall -Werror main.c

assignment-3-simple_C/main

8.77 KB
Binary file not shown.

assignment-3-simple_C/main.c

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <stdio.h>
2+
3+
const int first[] = {1, 2, 3, 4, 5};
4+
const int second[] = {6, 7, 8, 9, 10};
5+
6+
int scalar(const int* first, const int* second, size_t length) {
7+
size_t i;
8+
int result = 0;
9+
for(i = 0; i < length; i++) {
10+
result += (first[i] * second[i]);
11+
}
12+
return result;
13+
}
14+
15+
int is_prime(unsigned long n) {
16+
size_t i;
17+
for(i = 2; i < n / 2; i++) {
18+
if(n % i == 0) {
19+
return 0;
20+
}
21+
}
22+
return 1;
23+
}
24+
25+
void print_array(const int* arr, size_t length) {
26+
size_t i;
27+
for(i = 0; i < length; i++) {
28+
printf("%d ", arr[i]);
29+
}
30+
printf("\n");
31+
}
32+
33+
34+
35+
int main(int argc, char** argv) {
36+
unsigned long n = 0;
37+
size_t first_length = sizeof(first) / sizeof(int);
38+
size_t second_length = sizeof(second) / sizeof(int);
39+
size_t length = first_length > second_length ? second_length : first_length;
40+
41+
print_array(first, first_length);
42+
print_array(second, second_length);
43+
printf( "A scalar product of two vectors is: %d \n", scalar(first, second, length));
44+
printf("Input unsigned long: ");
45+
scanf("%lu", &n);
46+
printf("%lu is prime?\n", n);
47+
if(is_prime(n)) {
48+
printf("Yes\n");
49+
} else {
50+
printf("No\n");
51+
}
52+
return 0;
53+
}

0 commit comments

Comments
 (0)