From 3ea39e1afae3f678d7c3e004d5cbb2274fcc1e3d Mon Sep 17 00:00:00 2001 From: Ilham Dosdiyev Date: Mon, 15 Sep 2025 14:35:42 +0000 Subject: [PATCH 1/7] Update hello message --- src/hello.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hello.c b/src/hello.c index 9d6338f..c49b823 100644 --- a/src/hello.c +++ b/src/hello.c @@ -3,7 +3,7 @@ int main(int argc, char *argv[]) { // This is your first C program my friend - printf("Hello, RTU World from C Lab in 2025!\n"); + printf("Hello from Ilham! \n"); printf("You passed %d argument(s).\n", argc - 1); for (int i = 1; i < argc; ++i) { printf(" arg[%d] = %s\n", i, argv[i]); From b4c8aad2e41070f69a7320fb7917f647bdc09e56 Mon Sep 17 00:00:00 2001 From: Valdis Saulespurens Date: Mon, 22 Sep 2025 09:58:06 +0300 Subject: [PATCH 2/7] added lab 2 stubs --- src/lab2_1.c | 28 ++++++++++++++++++++++++++++ src/lab2_2.c | 28 ++++++++++++++++++++++++++++ src/lab2_3.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 src/lab2_1.c create mode 100644 src/lab2_2.c create mode 100644 src/lab2_3.c diff --git a/src/lab2_1.c b/src/lab2_1.c new file mode 100644 index 0000000..699339a --- /dev/null +++ b/src/lab2_1.c @@ -0,0 +1,28 @@ +#include + +/* + Task: + Write a function `int sum_to_n(int n)` that computes + the sum of all integers from 1 up to n using a for loop. + + In main(): + - Ask user for a positive integer n + - If n < 1, print an error + - Otherwise, call sum_to_n and print the result +*/ + +int sum_to_n(int n) { + // TODO: implement sum with a for loop + return 0; // placeholder +} + +int main(void) { + int n; + + printf("Enter a positive integer n: "); + scanf("%d", &n); + + // TODO: validate input, call function, and print result + + return 0; +} diff --git a/src/lab2_2.c b/src/lab2_2.c new file mode 100644 index 0000000..d88ef6d --- /dev/null +++ b/src/lab2_2.c @@ -0,0 +1,28 @@ +#include + +/* + Task: + Write a function `long long factorial(int n)` that computes n! + using a loop (not recursion). + + In main(): + - Ask user for an integer n + - If n is negative, print an error and exit + - Otherwise, call factorial and print the result +*/ + +long long factorial(int n) { + // TODO: compute factorial iteratively + return 1; // placeholder +} + +int main(void) { + int n; + + printf("Enter a non-negative integer n: "); + scanf("%d", &n); + + // TODO: validate input, call function, print result + + return 0; +} diff --git a/src/lab2_3.c b/src/lab2_3.c new file mode 100644 index 0000000..ddc8af6 --- /dev/null +++ b/src/lab2_3.c @@ -0,0 +1,28 @@ +#include + +/* + Task: + Write a function `int is_prime(int n)` that returns 1 if n is prime, + 0 otherwise. + + In main(): + - Ask user for an integer n (>= 2) + - If invalid, print an error + - Otherwise, print all prime numbers up to n +*/ + +int is_prime(int n) { + // TODO: check if n is prime using loop up to sqrt(n) + return 0; // placeholder +} + +int main(void) { + int n; + + printf("Enter an integer n (>= 2): "); + scanf("%d", &n); + + // TODO: validate input and print all primes up to n + + return 0; +} From ca240730f15be55cd827f3de23f1c32f3afc9fbb Mon Sep 17 00:00:00 2001 From: Valdis Saulespurens Date: Mon, 22 Sep 2025 09:59:54 +0300 Subject: [PATCH 3/7] updated makefile --- Makefile | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index c1afeec..9876be5 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,9 @@ SRC_DIR = src PROGRAMS = $(BUILD_DIR)/hello $(BUILD_DIR)/calculator $(BUILD_DIR)/formats -all: $(PROGRAMS) +all: $(BUILD_DIR)/hello $(BUILD_DIR)/calculator $(BUILD_DIR)/formats \ + $(BUILD_DIR)/lab2_1 $(BUILD_DIR)/lab2_2 $(BUILD_DIR)/lab2_3 + $(BUILD_DIR)/hello: $(SRC_DIR)/hello.c @mkdir -p $(BUILD_DIR) @@ -22,9 +24,24 @@ $(BUILD_DIR)/formats: $(SRC_DIR)/format_specifiers.c @mkdir -p $(BUILD_DIR) $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) +$(BUILD_DIR)/lab2_1: $(SRC_DIR)/lab2_1.c + @mkdir -p $(BUILD_DIR) + $(CC) $(CFLAGS) $< -o $@ + +$(BUILD_DIR)/lab2_2: $(SRC_DIR)/lab2_2.c + @mkdir -p $(BUILD_DIR) + $(CC) $(CFLAGS) $< -o $@ + +$(BUILD_DIR)/lab2_3: $(SRC_DIR)/lab2_3.c + @mkdir -p $(BUILD_DIR) + $(CC) $(CFLAGS) $< -o $@ + hello: $(BUILD_DIR)/hello calculator: $(BUILD_DIR)/calculator formats: $(BUILD_DIR)/formats +lab2_1: $(BUILD_DIR)/lab2_1 +lab2_2: $(BUILD_DIR)/lab2_2 +lab2_3: $(BUILD_DIR)/lab2_3 run-hello: hello ./$(BUILD_DIR)/hello @@ -35,5 +52,14 @@ run-calculator: calculator run-formats: formats ./$(BUILD_DIR)/formats +run-lab2_1: lab2_1 + ./$(BUILD_DIR)/lab2_1 + +run-lab2_2: lab2_2 + ./$(BUILD_DIR)/lab2_2 + +run-lab2_3: lab2_3 + ./$(BUILD_DIR)/lab2_3 + clean: rm -rf $(BUILD_DIR)/*.o $(PROGRAMS) From 033cf630f68e72498580a6093368063f6af86a32 Mon Sep 17 00:00:00 2001 From: Ilham Dosdiyev Date: Mon, 22 Sep 2025 13:19:26 +0000 Subject: [PATCH 4/7] all problems solved --- src/lab2_1.c | 25 +++++++++++++++++-------- src/lab2_2.c | 14 +++++++++++--- src/lab2_3.c | 21 ++++++++++++++++++++- 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/src/lab2_1.c b/src/lab2_1.c index 699339a..202ba11 100644 --- a/src/lab2_1.c +++ b/src/lab2_1.c @@ -12,17 +12,26 @@ */ int sum_to_n(int n) { - // TODO: implement sum with a for loop - return 0; // placeholder + // TODO: implement sum with a for loop + int sum = 0; + for (int i = 1; i <= n; i++) { + sum += i; + } + return sum; } int main(void) { - int n; + int n; - printf("Enter a positive integer n: "); - scanf("%d", &n); + printf("Enter a positive integer n: "); + scanf("%d", &n); - // TODO: validate input, call function, and print result - - return 0; + // TODO: validate input, call function, and print result + if (n < 1) { + printf("Error: n must be n>=1\n"); // We use \n for new line + } else { + int result = sum_to_n(n); + printf("Sum from 1 to %d = %d\n", n, result); + } + return 0; } diff --git a/src/lab2_2.c b/src/lab2_2.c index d88ef6d..93622a7 100644 --- a/src/lab2_2.c +++ b/src/lab2_2.c @@ -12,8 +12,11 @@ */ long long factorial(int n) { - // TODO: compute factorial iteratively - return 1; // placeholder + long long result = 1; + for (int i = 1; i <= n; i++) { + result *= i; // faktorial: n * (n-1) * ... * 1 + } + return result; } int main(void) { @@ -22,7 +25,12 @@ int main(void) { printf("Enter a non-negative integer n: "); scanf("%d", &n); - // TODO: validate input, call function, print result + if (n < 0) { + printf("Error: n must be >= 0\n"); + } else { + long long fact = factorial(n); + printf("%d! = %lld\n", n, fact); + } return 0; } diff --git a/src/lab2_3.c b/src/lab2_3.c index ddc8af6..1f1df93 100644 --- a/src/lab2_3.c +++ b/src/lab2_3.c @@ -1,4 +1,5 @@ #include +#include // for sqrt function /* Task: @@ -13,7 +14,13 @@ int is_prime(int n) { // TODO: check if n is prime using loop up to sqrt(n) - return 0; // placeholder + if (n < 2) return 0; // numbers less than 2 are not prime + for (int i = 2; i <= sqrt(n); i++) { + if (n % i == 0) { + return 0; // found a divisor → not prime + } + } + return 1; // no divisors found → prime } int main(void) { @@ -23,6 +30,18 @@ int main(void) { scanf("%d", &n); // TODO: validate input and print all primes up to n + if (n < 2) { + printf("Error: n must be >= 2\n"); + return 1; + } + + printf("Prime numbers up to %d:\n", n); + for (int i = 2; i <= n; i++) { + if (is_prime(i)) { + printf("%d ", i); + } + } + printf("\n"); return 0; } From 7937a4108de6a718c90f619748b075961cb6b4ab Mon Sep 17 00:00:00 2001 From: Ilham Dosdiyev Date: Mon, 22 Sep 2025 13:50:17 +0000 Subject: [PATCH 5/7] Lab2 solutions updated --- src/lab2_2.c | 30 +++++++++++++++--------------- src/lab2_3.c | 46 +++++++++++++++++++++++----------------------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/lab2_2.c b/src/lab2_2.c index 93622a7..843b2fa 100644 --- a/src/lab2_2.c +++ b/src/lab2_2.c @@ -12,25 +12,25 @@ */ long long factorial(int n) { - long long result = 1; - for (int i = 1; i <= n; i++) { - result *= i; // faktorial: n * (n-1) * ... * 1 - } - return result; + long long result = 1; + for (int i = 1; i <= n; i++) { + result *= i; // faktorial: n * (n-1) * ... * 1 + } + return result; } int main(void) { - int n; + int n; - printf("Enter a non-negative integer n: "); - scanf("%d", &n); + printf("Enter a non-negative integer n: "); + scanf("%d", &n); - if (n < 0) { - printf("Error: n must be >= 0\n"); - } else { - long long fact = factorial(n); - printf("%d! = %lld\n", n, fact); - } + if (n < 0) { + printf("Error: n must be >= 0\n"); + } else { + long long fact = factorial(n); + printf("%d! = %lld\n", n, fact); + } - return 0; + return 0; } diff --git a/src/lab2_3.c b/src/lab2_3.c index 1f1df93..5049da4 100644 --- a/src/lab2_3.c +++ b/src/lab2_3.c @@ -1,5 +1,5 @@ +#include // for sqrt function #include -#include // for sqrt function /* Task: @@ -13,35 +13,35 @@ */ int is_prime(int n) { - // TODO: check if n is prime using loop up to sqrt(n) - if (n < 2) return 0; // numbers less than 2 are not prime - for (int i = 2; i <= sqrt(n); i++) { - if (n % i == 0) { - return 0; // found a divisor → not prime - } + // TODO: check if n is prime using loop up to sqrt(n) + if (n < 2) return 0; // numbers less than 2 are not prime + for (int i = 2; i <= sqrt(n); i++) { + if (n % i == 0) { + return 0; // found a divisor → not prime } - return 1; // no divisors found → prime + } + return 1; // no divisors found → prime } int main(void) { - int n; + int n; - printf("Enter an integer n (>= 2): "); - scanf("%d", &n); + printf("Enter an integer n (>= 2): "); + scanf("%d", &n); - // TODO: validate input and print all primes up to n - if (n < 2) { - printf("Error: n must be >= 2\n"); - return 1; - } + // TODO: validate input and print all primes up to n + if (n < 2) { + printf("Error: n must be >= 2\n"); + return 1; + } - printf("Prime numbers up to %d:\n", n); - for (int i = 2; i <= n; i++) { - if (is_prime(i)) { - printf("%d ", i); - } + printf("Prime numbers up to %d:\n", n); + for (int i = 2; i <= n; i++) { + if (is_prime(i)) { + printf("%d ", i); } - printf("\n"); + } + printf("\n"); - return 0; + return 0; } From 1d3751822ecd57ec4df9e88bbaa00698926c1dd7 Mon Sep 17 00:00:00 2001 From: Ilham Dosdiyev Date: Fri, 3 Oct 2025 22:08:13 +0000 Subject: [PATCH 6/7] Lab 2 solved questions --- src/lab2_1.c | 2 +- src/lab2_2.c | 2 +- src/lab2_3.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lab2_1.c b/src/lab2_1.c index 1b22893..4521019 100644 --- a/src/lab2_1.c +++ b/src/lab2_1.c @@ -8,7 +8,7 @@ In main(): - Ask user for a positive integer n - If n < 1, print an error - - Otherwise, call sum_to_n and print the result + - Otherwise, call sum_to_n and print the result. */ int sum_to_n(int n) { diff --git a/src/lab2_2.c b/src/lab2_2.c index 9910686..51e0f3d 100644 --- a/src/lab2_2.c +++ b/src/lab2_2.c @@ -8,7 +8,7 @@ In main(): - Ask user for an integer n - If n is negative, print an error and exit - - Otherwise, call factorial and print the result + - Otherwise, call factorial and print the result. */ long long factorial(int n) { diff --git a/src/lab2_3.c b/src/lab2_3.c index fe065c1..c0a9fd7 100644 --- a/src/lab2_3.c +++ b/src/lab2_3.c @@ -55,7 +55,7 @@ int main(void) { printf("Enter an integer n (>= 2): "); scanf("%d", &n); - // TODO: validate input and print all primes up to n + // TODO: validate input and print all primes up to n. return 0; } From f0237546770e56897cf7e5d08600b294932631c7 Mon Sep 17 00:00:00 2001 From: Ilham Dosdiyev Date: Sat, 4 Oct 2025 16:42:35 +0000 Subject: [PATCH 7/7] Add Name and Student ID to Lab 3 files --- src/lab3_task1.c | 2 +- src/lab3_task2.c | 2 +- src/lab3_task3.c | 48 ++++++++++++++++++++++++------------------------ 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/lab3_task1.c b/src/lab3_task1.c index 1c4c9ea..318a387 100644 --- a/src/lab3_task1.c +++ b/src/lab3_task1.c @@ -1,6 +1,6 @@ /* * Lab 3, Task 1 - * Student Name, Student ID + * Student Name:Ilham Dosdiyev, Student ID: 241ADB018 * * Implement array algorithms: * - find minimum value diff --git a/src/lab3_task2.c b/src/lab3_task2.c index b61db87..c2799ae 100644 --- a/src/lab3_task2.c +++ b/src/lab3_task2.c @@ -1,6 +1,6 @@ /* * Lab 3, Task 2 - * Student Name, Student ID + * Student Name: Ilham Dosdiyev, Student ID:241ADB018 * * Practice using pointers as function parameters. * Implement: diff --git a/src/lab3_task3.c b/src/lab3_task3.c index 9fe7e9c..6a4b31f 100644 --- a/src/lab3_task3.c +++ b/src/lab3_task3.c @@ -1,6 +1,6 @@ /* * Lab 3, Task 3 - * Student Name, Student ID + * Student Name:Ilham Dosdiyev, Student ID:241ADB018 * * Implement basic string handling functions. * Write your own versions of: @@ -26,37 +26,37 @@ int my_strlen(const char *str); void my_strcpy(char *dest, const char *src); int main(void) { - // TODO: Test your functions here - char test[] = "Programming in C"; - char copy[100]; + // TODO: Test your functions here + char test[] = "Programming in C"; + char copy[100]; - int len = my_strlen(test); - printf("Length: %d\n", len); + int len = my_strlen(test); + printf("Length: %d\n", len); - my_strcpy(copy, test); - printf("Copy: %s\n", copy); + my_strcpy(copy, test); + printf("Copy: %s\n", copy); - return 0; + return 0; } // Implement functions below int my_strlen(const char *str) { - // Count characters using pointer arithmetic - int count = 0; - const char *ptr = str; // point to the first character - while(*ptr != '\0') { // stop at null terminator - count++; - ptr++; // move to next character - } - return count; + // Count characters using pointer arithmetic + int count = 0; + const char *ptr = str; // point to the first character + while (*ptr != '\0') { // stop at null terminator + count++; + ptr++; // move to next character + } + return count; } void my_strcpy(char *dest, const char *src) { - // Copy string using pointers - while(*src != '\0') { // until end of source - *dest = *src; // copy character - dest++; // move dest pointer - src++; // move src pointer - } - *dest = '\0'; // add null terminator + // Copy string using pointers + while (*src != '\0') { // until end of source + *dest = *src; // copy character + dest++; // move dest pointer + src++; // move src pointer + } + *dest = '\0'; // add null terminator }