├── .gitattributes ├── README.md ├── Practice ├── integers.c ├── firstprog.c ├── Addition of Two numbers.c ├── float numbers.c ├── Looping.c ├── do-while Loop.c ├── if_statement.c ├── Fah_Cel_Converter.c ├── Investment Program.c └── Average_Of_n_Values.c ├── Stack_Example.c ├── Selection_sort_Alg.c ├── .gitignore └── LeetCode ├── 3Sum Closest.c ├── Letter Combinations of a Phone Number.c └── 3Sum.c /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C Programming 2 | These are programs that i wrote in C for practice. 3 | -------------------------------------------------------------------------------- /Practice/integers.c: -------------------------------------------------------------------------------- 1 | void main (void) 2 | { 3 | int a =2; 4 | int b = 3; 5 | int c =a + b; 6 | 7 | 8 | printf ("The sum of adding %d and %d is %d\n", a, b, c); 9 | } 10 | -------------------------------------------------------------------------------- /Practice/firstprog.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int i; 6 | for(i=0; i<10; i++) 7 | { 8 | puts("Hello, World!\n"); 9 | } 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /Stack_Example.c: -------------------------------------------------------------------------------- 1 | void test_function (int a, int b, int c, int d) { 2 | int flag; 3 | char buffer[10]; 4 | 5 | flag = 31337; 6 | buffer[0] = 'A'; 7 | } 8 | int main() { 9 | test_function(1, 2, 3, 4); 10 | } 11 | -------------------------------------------------------------------------------- /Practice/Addition of Two numbers.c: -------------------------------------------------------------------------------- 1 | main () 2 | { 3 | int number; 4 | float amount; 5 | 6 | number = 100; 7 | 8 | amount = 30.75 + 75.35; 9 | printf("%d\n",number); 10 | printf("%5.2f",amount); 11 | } 12 | -------------------------------------------------------------------------------- /Practice/float numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main (void) 4 | { 5 | float a; 6 | float b = 3.641; 7 | float c; 8 | 9 | a = 2.897; 10 | c = a + b; 11 | printf ("The sum of adding %f and %f is %f\n", a, b, c); 12 | } 13 | -------------------------------------------------------------------------------- /Practice/Looping.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main (void) 4 | { 5 | int a = 0; 6 | 7 | while (a < 5) 8 | { 9 | printf ("a is equal to %d\n", a); 10 | a++; 11 | } 12 | printf ("a is equal to %d and i have finished\n", a); 13 | } 14 | -------------------------------------------------------------------------------- /Practice/do-while Loop.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main (void) 4 | { 5 | int a = 0; 6 | 7 | do 8 | { 9 | printf ("a is equal to %d\n", a); 10 | a++; 11 | } while (a < 5); 12 | printf ("a is equal %d and i have finished\n", a); 13 | } 14 | -------------------------------------------------------------------------------- /Practice/if_statement.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main (void) 4 | { 5 | int a = 0; //Change the value of a to check how the if statements work 6 | 7 | if (a == 0) 8 | { 9 | printf ("a is equal to 0\n"); 10 | } 11 | else 12 | { 13 | printf ("a is not equal to 0\n"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Selection_sort_Alg.c: -------------------------------------------------------------------------------- 1 | selection_sort(int s[], int n) 2 | { 3 | int i,j; /* counters */ 4 | int min; /* index of minimum */ 5 | 6 | for (i=0; i 2 | #include 3 | #include 4 | 5 | int compare(const void *a, const void *b) { 6 | return (*(int*)a - *(int*)b); 7 | } 8 | 9 | int threeSumClosest(int* nums, int numsSize, int target) { 10 | qsort(nums, numsSize, sizeof(int), compare); 11 | 12 | int closestSum = INT_MAX; 13 | int minDiff = INT_MAX; 14 | 15 | for (int i = 0; i < numsSize - 2; i++) { 16 | int left = i + 1; 17 | int right = numsSize - 1; 18 | 19 | while (left < right) { 20 | int sum = nums[i] + nums[left] + nums[right]; 21 | int diff = abs(sum - target); 22 | 23 | if (diff < minDiff) { 24 | minDiff = diff; 25 | closestSum = sum; 26 | } 27 | 28 | if (sum < target) { 29 | left++; 30 | } else if (sum > target) { 31 | right--; 32 | } else { 33 | return sum; // Exact match found 34 | } 35 | } 36 | } 37 | 38 | return closestSum; 39 | } 40 | 41 | int main(int argc, char *argv[]) { 42 | int nums[] = {-1, 2, 1, -4}; 43 | int target = 1; 44 | int numsSize = sizeof(nums) / sizeof(nums[0]); 45 | 46 | int result = threeSumClosest(nums, numsSize, target); 47 | printf("The sum that is closest to the target is %d.\n", result); 48 | 49 | return 0; 50 | } -------------------------------------------------------------------------------- /LeetCode/Letter Combinations of a Phone Number.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | const char* digitToLetters[] = { 6 | "", // 0 7 | "", // 1 8 | "abc", // 2 9 | "def", // 3 10 | "ghi", // 4 11 | "jkl", // 5 12 | "mno", // 6 13 | "pqrs", // 7 14 | "tuv", // 8 15 | "wxyz" // 9 16 | }; 17 | 18 | void backtrack(char** result, int* returnSize, char* combination, const char* digits, int index) { 19 | if (index == strlen(digits)) { 20 | result[*returnSize] = strdup(combination); 21 | (*returnSize)++; 22 | return; 23 | } 24 | 25 | int digit = digits[index] - '0'; 26 | const char* letters = digitToLetters[digit]; 27 | 28 | for (int i = 0; i < strlen(letters); i++) { 29 | combination[index] = letters[i]; 30 | backtrack(result, returnSize, combination, digits, index + 1); 31 | } 32 | } 33 | 34 | char** letterCombinations(char* digits, int* returnSize) { 35 | *returnSize = 0; 36 | if (digits == NULL || strlen(digits) == 0) { 37 | return NULL; 38 | } 39 | 40 | int maxCombinations = 1; 41 | for (int i = 0; i < strlen(digits); i++) { 42 | int digit = digits[i] - '0'; 43 | maxCombinations *= strlen(digitToLetters[digit]); 44 | } 45 | 46 | char** result = (char**)malloc(maxCombinations * sizeof(char*)); 47 | char* combination = (char*)malloc((strlen(digits) + 1) * sizeof(char)); 48 | combination[strlen(digits)] = '\0'; 49 | 50 | backtrack(result, returnSize, combination, digits, 0); 51 | 52 | free(combination); 53 | return result; 54 | } -------------------------------------------------------------------------------- /LeetCode/3Sum.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int compare(const void *a, const void *b) { 5 | return (*(int*)a - *(int*)b); 6 | } 7 | 8 | int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) { 9 | qsort(nums, numsSize, sizeof(int), compare); 10 | 11 | int** result = (int**)malloc(sizeof(int*) * numsSize * numsSize); 12 | *returnSize = 0; 13 | *returnColumnSizes = (int*)malloc(sizeof(int) * numsSize * numsSize); 14 | 15 | for (int i = 0; i < numsSize - 2; i++) { 16 | if (i > 0 && nums[i] == nums[i - 1]) continue; // Skip duplicates 17 | 18 | int left = i + 1; 19 | int right = numsSize - 1; 20 | 21 | while (left < right) { 22 | int sum = nums[i] + nums[left] + nums[right]; 23 | if (sum == 0) { 24 | result[*returnSize] = (int*)malloc(sizeof(int) * 3); 25 | result[*returnSize][0] = nums[i]; 26 | result[*returnSize][1] = nums[left]; 27 | result[*returnSize][2] = nums[right]; 28 | (*returnColumnSizes)[*returnSize] = 3; 29 | (*returnSize)++; 30 | 31 | while (left < right && nums[left] == nums[left + 1]) left++; // Skip duplicates 32 | while (left < right && nums[right] == nums[right - 1]) right--; // Skip duplicates 33 | 34 | left++; 35 | right--; 36 | } else if (sum < 0) { 37 | left++; 38 | } else { 39 | right--; 40 | } 41 | } 42 | } 43 | 44 | return result; 45 | } --------------------------------------------------------------------------------