# shanazari_davit — Homework Problem 1: Control Flow & Loops Calculate the sum of all multiples of 3 or 5 below N ( where N in in the range [1-1000] ). #include int main() { int N; int sum = 0; printf("Enter N (1-1000): "); if (scanf("%d", &N) != 1 || N < 1 || N > 1000) { printf("Error: Invalid range input.\n"); return 1; } for (int i = 1; i < N; i++) { if (i % 3 == 0 || i % 5 == 0) { sum += i; } } printf("The sum of multiples below %d is: %d\n", N, sum); return 0; } * How to solve it: We read an integer value N from the user. We then run a loop from 1 up to (but not including) N. For every number, we use the modulus operator (%) to check if the remainder when divided by 3 or divided by 5 is 0. If either condition is true, we add that number to a running total variable (sum). * We read an integer value N from the user. We then run a loop from 1 up to (but not including) N. For every number, we use the modulus operator (%) to check if the remainder when divided by 3 or divided by 5 is 0. If either condition is true, we add that number to a running total variable (sum). *To ensure robust fault tolerance, I included an initial bounds check verifying that the user input sits safely within the assigned [1-1000] matrix before running calculations. Problem 2 : Problem 2: Bitwise Operations — "Read K-th Bit" Write a C program to read the given K-th bit of a number. If the K-th bit is 1, return 1; if it is 0, return 0. Assume 0-indexed bits starting from the least significant bit. #include int read_kth_bit(int number, int k) { return (number >> k) & 1; } int main() { int number = 13; int k = 2; int bit_status = read_kth_bit(number, k); printf("The bit at index %d of number %d is: %d\n", k, number, bit_status); return 0; } * How to solve it: To isolate a specific bit, we can use the bitwise right-shift operator (>>) to move the target K-th bit all the way to the 0th position (the far right). Then, we perform a bitwise AND operation (&) with the number 1 (binary 00000001). Since all other bits in 1 are zero, this effectively clears everything else out, leaving us with exactly a 1 or a 0 depending on the state of that original bit. * Instead of performing slower division or string conversions, this solution leverages pure bitwise manipulation. It scales down execution overhead to a single CPU clock cycle instruction. * By combining a dynamic right-shift (>>) with a static logical bitmask evaluation (& 1), we extract structural binary data cleanly without mutating the original variable value. Problem 3: Pointers & Arrays — "Sum Array Elements via Pointer Arithmetic" Calculate the sum of all elements in an integer array using pointer arithmetic rather than standard array indexing brackets( arr[i] ) #include int sum_array(int* arr, int size) { int total_sum = 0; int* ptr = arr; for (int i = 0; i < size; i++) { total_sum += *ptr; ptr++; } return total_sum; } int main() { int arr[] = {3, 5, 7}; int size = sizeof(arr) / sizeof(arr[0]); int result = sum_array(arr, size); printf("The sum of array elements is: %d\n", result); return 0; } * How to sove it: An array name acts as a constant pointer to its first element. We can declare a pointer variable pointing to the start of the array. Inside a loop, we dereference the current pointer using the asterisk operator (*ptr) to get the integer value and add it to our sum. Then, we increment the pointer itself (ptr++) to advance the memory address directlyto the next sequential integer slot. * This problem demonstrates pointer arithmetic capabilities by abandoning high-level bracket notations (arr[i]). It interfaces with structural memory array layouts directly. * The pointer tracker (ptr++) automatically shifts forward by the exact byte size of the underlying standard data type (e.g., 4 bytes for an integer), showcasing standard low-level address calculation mechanisms. Problem 4: Strings — "Remove Consecutive Duplicate Characters" Write a function that parses a null-terminated string and removes all consecutive duplicate characters in-place. #include void remove_consecutive_duplicates(char* str) { if (str == NULL || str[0] == '\0') return; int write_index = 1; for (int read_index = 1; str[read_index] != '\0'; read_index++) { if (str[read_index] != str[write_index - 1]) { str[write_index] = str[read_index]; write_index++; } } str[write_index] = '\0'; } int main() { char test_string[] = "aaabb"; printf("Original: %s\n", test_string); remove_consecutive_duplicates(test_string); printf("Processed: %s\n", test_string); return 0; } * How to solve it: We can solve this efficiently in-place using a "two-pointer" or "read-write index" strategy. We traverse the string with a read index. At each position, we compare the current character with the character right before it. If it's different, it means we've encountered a fresh character; we write it to our write pointer location and advance the write index. If it's a duplicate, we skip it. At the end, we append a null terminator (\0) to seal the shortened string correctly. * The string algorithm runs with O(N) time complexity while maintaining O(1) auxiliary space complexity. It directly repurposes the existing string memory allocations rather than declaring secondary buffer arrays. * The final assignment of str[write_index] = '\0'; is a crucial safety step. It clips the trailing junk data from the string buffer, preventing downstream memory overrun bugs during display outputs.