├── .DS_Store ├── Caesar_cipher.c ├── PairEqualsSum.java ├── README.md ├── Sorting_Algorithms ├── bubble_sort ├── bubble_sort.c ├── counting_sort ├── counting_sort.c ├── heap_sort ├── heap_sort.c ├── insertion_sort ├── insertion_sort.c ├── merge_sort ├── merge_sort.c ├── quick_sort ├── quick_sort.c ├── selection_sort └── selection_sort.c ├── anagram.c ├── average_using_arrays.c ├── binary_search.c ├── binary_to_decimal.c ├── binary_tree_vertical.c ├── bst_delete.c ├── bst_insert.c ├── bst_or_not.c ├── bst_search.c ├── bubble_sort.c ├── build_max_heap.c ├── change_return.c ├── concat.c ├── credit_card_validation.c ├── decimal_to_binary.c ├── factorial_loop.c ├── factorial_recursion.c ├── fibonacci.c ├── first_last_occurence.c ├── half_pyramid_pattern.c ├── hamming_distance.c ├── happy_number.c ├── heap_sort.c ├── initials.c ├── insertion_sort.c ├── linear_search.c ├── max_numeric_value.c ├── merge_sort.c ├── merge_trees.c ├── number_complement.c ├── odd_times.c ├── optimized_prime_factors.c ├── palindrome.c ├── power_of_two.c ├── prime_factor.c ├── quick_sort.c ├── reverse_string.c ├── selection_sort.c ├── sieve_of_eratosthenes.c ├── smallest_missing.c ├── star_pattern.c ├── string_operations.c ├── triplet.c ├── unit_converter.c ├── unsorted_subarray.c └── vigenere_cipher.c /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chhayac/algo_and_data_structures/271ca963fba90fa1632b27a0f39501266bc2bd62/.DS_Store -------------------------------------------------------------------------------- /Caesar_cipher.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Program to encrypt text using Caesar’s cipher, as per below. 3 | * Example: 4 | * $ ./caesar 1 5 | * Input : plaintext: HELLO 6 | * Output : ciphertext: URYYB 7 | * 8 | * $ ./caesar 1 2 3 4 5 9 | * Usage: ./caesar k 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | int main(int argc, char *argv[]) 17 | { 18 | if(argc != 2) 19 | { 20 | printf("Usage: ./caesar k\n"); 21 | return 1; 22 | } 23 | char plain_text[100]; 24 | int k = atoi(argv[1]); 25 | printf("Enter Plaintext: "); 26 | 27 | // reads a line from the specified stream and stores it into the string 28 | // 29 | fgets(plain_text, sizeof(plain_text), stdin); 30 | printf("Ciphertext: "); 31 | for (int i = 0, n = strlen(plain_text); i < n; i++) 32 | { 33 | if(plain_text[i] >= 'a' && plain_text[i] <= 'z') 34 | { 35 | int p = plain_text[i] - 'a'; 36 | int c1 = (k + p) % 26; 37 | plain_text[i] = c1 + 'a'; 38 | } 39 | else if(plain_text[i] >= 'A' && plain_text[i] <= 'Z') 40 | { 41 | int p = plain_text[i] - 'A'; 42 | int c1 = (k + p) % 26; 43 | plain_text[i] = c1 + 'A'; 44 | } 45 | printf("%c",plain_text[i]); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /PairEqualsSum.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Given a collection of numbers, determine whether there exists two distinct elements in the array 3 | * whose sum equals a target value. 4 | * Example: 5 | * elements = [2,4,5,9,1], target = 8 -> False 6 | * elements = [2,4,5,9,1], target = 7 -> true 7 | */ 8 | 9 | import java.util.*; 10 | public class PairEqualsSum 11 | { 12 | static boolean printpairs(int arr[], int target) { 13 | HashSet hset = new HashSet(); 14 | for(int i = 0; i < arr.length; i++) 15 | { 16 | if(hset.contains(target - arr[i])) 17 | { 18 | System.out.println("1st element:" + arr[i] + "," +"2nd element:"+ (target-arr[i])); 19 | return true; 20 | } 21 | else 22 | { 23 | hset.add(arr[i]); 24 | } 25 | } 26 | return false; 27 | } 28 | public static void main(String args[]) { 29 | int A[] = {2,5,4,9,1}; 30 | int target = 10; 31 | boolean ifPrinted = printpairs(A,target); 32 | if (ifPrinted) 33 | { 34 | System.out.println("True"); 35 | } 36 | else 37 | { 38 | System.out.println("False"); 39 | } 40 | } 41 | } 42 | 43 | 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # algo_and_data_structures 2 | This repository contains various algorithms and data structure problems. 3 | 4 | | Program name | Description | 5 | | ------------- | ------------- | 6 | | [fibonacci.c](/fibonacci.c) | Generates fibonacci sequence till nth term | 7 | | [binary_to_decimal.c](/binary_to_decimal.c) | Converts a binary number to its decimal equivalent | 8 | | [decimal_to_binary.c](/decimal_to_binary.c) | Converts a decimal number to its binary equivalent | 9 | | [prime_factor.c](/prime_factor.c) | Printing prime factors of a number entered by user | 10 | | [half_pyramid_pattern.c](/half_pyramid_pattern.c) | Printing a half-pyramid of a specified height | 11 | | [star_pattern.c](/star_pattern.c) | Printing a star pattern of a specified height | 12 | | [change_return.c](/change_return.c) | Calculate the minimum number of coins required to give a user change | 13 | | [sieve_of_eratosthenes.c](/sieve_of_eratosthenes.c) | Implement Sieve of Eratosthenes algorithm | 14 | | [optimized_prime_factors.c](/optimized_prime_factors.c) | Printing prime factors of a number less than 1000 using sieve of eratosthenes | 15 | | [credit_card_validation.c](/credit_card_validation.c) | Check if a credit card number is valid based on Luhn's algorithm | 16 | | [initials.c](/initials.c) | Generate initials of a given name | 17 | | [Caesar_cipher.c](/Caesar_cipher.c) | Encrypts text using Caesar’s cipher | 18 | | [vigenere_cipher.c](/vigenere_cipher.c) | Encrypts text using Vigenere’s cipher | 19 | | [unit_converter.c](/unit_converter.c) | Unit converter (Temperature, Currency and Weight) | 20 | | [factorial_loop.c](/factorial_loop.c) | Calculates factorial of a positive number using loop | 21 | | [factorial_recursion.c](/factorial_recursion.c) | Calculates factorial of a positive number using recursion method | 22 | | [average_using_arrays.c](/average_using_arrays.c) | Calculates average using arrays | 23 | | [power_of_two.c](/power_of_two.c) | Checks if an integer is a power of two | 24 | | [palindrome.c](/palindrome.c) | Checks if an input string is a palindrome or not | 25 | | [happy_number.c](/happy_number.c) | Checks if a given positive number is a happy number or not | 26 | | [reverse_string.c](/reverse_string.c) | Print the reverse of a string | 27 | | [string_operations.c](/string_operations.c) | Perform string operations copy and compare without using string functions | 28 | | [bubble_sort.c](/bubble_sort.c) | Implement Bubble Sort | 29 | | [concat.c](/concat.c) | Concat two strings without using library function | 30 | | [binary_search.c](/binary_search.c) | Search an element using binary search iterative approach | 31 | | [selection_sort.c](/selection_sort.c) | Implement Selection Sort | 32 | | [insertion_sort.c](/insertion_sort.c) | Implement Insertion Sort | 33 | | [linear_search.c](/linear_search.c) | Search element of an array using linear search | 34 | | [merge_sort.c](/merge_sort.c) | Implement Merge Sort | 35 | | [quick_sort.c](/quick_sort.c) | Implement Quick Sort | 36 | | [smallest_missing.c](/smallest_missing.c) | Find smallest missing element in a sorted array of distinct non-negative integers| 37 | | [anagram.c](/anagram.c) | Determine if two strings are anagrams or not | 38 | | [triplet.c](/triplet.c) | Determine if triplet exist with given sum in an unsorted array | 39 | | [unsorted_subarray.c](/unsorted_subarray.c) | Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted | 40 | | [first_last_occurence.c](/first_last_occurence.c) | Determine the first and last occurence of an element in a sorted array | 41 | | [max_numeric_value.c](/max_numeric_value.c) | Extract maximum numeric value from an alphanumeric string | 42 | | [odd_times.c](/odd_times.c) | Find the number occuring odd number of times | 43 | | [missing_number.c](/missing_number.c) | Find the missing number from a list of n-1 integers | 44 | | [build_max_heap.c](/build_max_heap.c) | Program to build max heap with an input array | 45 | | [heap_sort.c](/heap_sort.c) | Program to sort an array using Heap Sort algorithm | 46 | | [bst_insert.c](/bst_insert.c) | Program to insert nodes with given keys in a Binary Search Tree. | 47 | | [bst_search.c](/bst_search.c) | Program to search node with given key in a Binary Search Tree | 48 | | [bst_delete.c](/bst_delete.c) | Program to delete a node with a key in Binary Search Tree | 49 | | [bst_or_not.c](/bst_or_not.c) | Program to check if a binary tree is BST or not | 50 | | [counting_sort.c](/counting_sort.c) | Program to sort numbers using Counting sort | 51 | | [merge_trees.c](/merge_trees.c) | Program to merge two binary trees | 52 | | [hamming_distance.c](/hamming_distance.c) | Program to calculate Hamming distance between two integers | 53 | | [binary_tree_vertical.c](/binary_tree_vertical.c) | Program to print a vertical binary tree | 54 | | [number_complement.c](/number_complement.c) | Program to output complement of a positive integer | 55 | | [PairEqualsSum.java](/PairEqualsSum.java) | Program to check whether there exists two distinct elements in the array whose sum equals a target value | 56 | 57 | -------------------------------------------------------------------------------- /Sorting_Algorithms/bubble_sort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chhayac/algo_and_data_structures/271ca963fba90fa1632b27a0f39501266bc2bd62/Sorting_Algorithms/bubble_sort -------------------------------------------------------------------------------- /Sorting_Algorithms/bubble_sort.c: -------------------------------------------------------------------------------- 1 | /* Program to implement Bubble Sort 2 | * Bubble Sort is a sorting algorithm that works by repeatedly 3 | * swapping the adjacent elements if they are in wrong order. 4 | * 5 | * Example: 6 | * Enter the size of the array:4 7 | * Enter 4 elements in the array: 10 2 1 4 8 | * Sorted array is: 1 2 4 10 9 | * 10 | * Enter the size of the array: 1000 11 | * Max size of array is 100. Please enter value less than 100 12 | * Enter the size of the array: 13 | */ 14 | 15 | #include 16 | #define MAX_SIZE 100 // maximum size of the array 17 | 18 | #define bool int 19 | #define true 1 20 | #define false 0 21 | 22 | void bubble_sort(int arr[], int); 23 | void swap(int *a, int *b); 24 | void printArray(int arr[], int); 25 | 26 | int main(void) 27 | { 28 | int arr[MAX_SIZE]; 29 | int size_array; 30 | do 31 | { 32 | printf("Enter the size of the array: "); 33 | scanf("%d",&size_array); 34 | if(size_array > MAX_SIZE) 35 | { 36 | printf("Max size of array is 100. Please enter value less than 100.\n"); 37 | } 38 | }while(size_array > MAX_SIZE); 39 | printf("Enter %d elements in the array: ", size_array); 40 | for(int i = 0; i < size_array; i++) 41 | { 42 | scanf("%d",&arr[i]); 43 | } 44 | 45 | bubble_sort(arr, size_array); 46 | printf("Sorted array is: "); 47 | printArray(arr, size_array); 48 | return 0; 49 | } 50 | 51 | /* Function to sort the array using bubble sort */ 52 | void bubble_sort(int arr[], int n) 53 | { 54 | int i, j; 55 | bool swapped; 56 | for (i = 0; i < n-1 ; i++) 57 | { 58 | swapped = false; 59 | for(j = 0; j < n-i-1; j++) 60 | { 61 | if(arr[j] > arr[j+1]) 62 | { 63 | swap(&arr[j],&arr[j+1]); 64 | swapped = true; 65 | } 66 | } 67 | // If no two elements are swapped by inner loop, then break 68 | if(swapped == false) 69 | break; 70 | } 71 | } 72 | 73 | /* Function to swap 2 values */ 74 | void swap(int *a, int *b) 75 | { 76 | int temp = *a; 77 | *a = *b; 78 | *b = temp; 79 | } 80 | 81 | /* Function to print the sorted array */ 82 | void printArray(int arr[], int size) 83 | { 84 | int i; 85 | for (i = 0; i < size; i++) 86 | { 87 | printf("%d ", arr[i]); 88 | } 89 | printf("\n"); 90 | } 91 | -------------------------------------------------------------------------------- /Sorting_Algorithms/counting_sort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chhayac/algo_and_data_structures/271ca963fba90fa1632b27a0f39501266bc2bd62/Sorting_Algorithms/counting_sort -------------------------------------------------------------------------------- /Sorting_Algorithms/counting_sort.c: -------------------------------------------------------------------------------- 1 | /* Program to sort numbers using Counting sort 2 | * Example: 3 | * Input array: 9 4 10 8 2 4 4 | * Output: 5 | * Sorted Array is: 2 4 4 8 9 10 6 | */ 7 | 8 | #include 9 | #include 10 | #define RANGE 255 11 | 12 | void countingSort(int arr[]); 13 | void printArray(int arr[], int); 14 | 15 | int main() 16 | { 17 | int arr[] = {9, 4, 10, 8, 2, 4}; 18 | int n = sizeof(arr)/sizeof(arr[0]); 19 | countingSort(arr); 20 | printf("Sorted array is: "); 21 | printArray(arr, n); 22 | return 0; 23 | } 24 | 25 | void countingSort(int arr[]) 26 | { 27 | int count[RANGE + 1], i; 28 | memset(count, 0, sizeof(count)); 29 | int Output[RANGE + 1]; 30 | 31 | for(i = 0; arr[i]; i++) 32 | { 33 | count[arr[i]]++; 34 | printf("Array = %d, count= %d\n", arr[i], count[arr[i]]); 35 | } 36 | printf("range= %d", RANGE); 37 | for(i = 1; i <= RANGE; i++) 38 | { 39 | count[i] += count[i-1]; 40 | printf("Changed count = %d\n", count[i]); 41 | } 42 | 43 | for (int i = 0; arr[i]; i++) 44 | { 45 | Output[count[arr[i]]] = arr[i]; 46 | count[arr[i]] -= 1; 47 | printf("Output = %d", Output[i]); 48 | } 49 | 50 | for(i = 0; arr[i]; i++) 51 | { 52 | arr[i] = Output[i]; 53 | } 54 | } 55 | 56 | void printArray(int arr[], int n) 57 | { 58 | for(int i = 0; i < n; i++) 59 | { 60 | printf("%d ", arr[i]); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Sorting_Algorithms/heap_sort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chhayac/algo_and_data_structures/271ca963fba90fa1632b27a0f39501266bc2bd62/Sorting_Algorithms/heap_sort -------------------------------------------------------------------------------- /Sorting_Algorithms/heap_sort.c: -------------------------------------------------------------------------------- 1 | /* Program to sort an array using Heap Sort. 2 | * 3 | * Input : 5,3,17,10,84,19,6,22,9 4 | * Output : 3 5 6 9 10 17 19 22 84 5 | * 6 | */ 7 | 8 | #include 9 | #include 10 | #define left(x) (2*x + 1) 11 | #define right(x) (2*x + 2) 12 | 13 | void heapSort(int Arr[], int); 14 | void heapify(int Arr[], int, int); 15 | void printArray(int Arr[], int); 16 | 17 | int main(void) 18 | { 19 | int Arr[] = {5,3,17,10,84,19,6,22,9}; 20 | int n = sizeof(Arr)/sizeof(Arr[0]); // n is heap length / length of the array 21 | heapSort(Arr,n); 22 | printArray(Arr,n); 23 | return 0; 24 | } 25 | 26 | void heapSort(int Arr[], int n) 27 | { 28 | int i; 29 | 30 | // Build heap i.e rearrange the array 31 | for(i = n/2 - 1; i >= 0 ; i--) 32 | { 33 | heapify(Arr, i, n); 34 | } 35 | 36 | // Extract an element from heap one by one 37 | for(i = n-1; i >= 0 ; i--) 38 | { 39 | int temp = Arr[i]; 40 | Arr[i] = Arr[0]; 41 | Arr[0] = temp; 42 | // call heapify on the reduced heap 43 | heapify(Arr, 0, i); 44 | } 45 | } 46 | 47 | void heapify(int Arr[], int i, int n) 48 | { 49 | int l = left(i); 50 | int r = right(i); 51 | int largest = i; // Initialize largest as root 52 | 53 | // If left child is greater than root 54 | if(l < n && Arr[l] > Arr[i]) 55 | { 56 | largest = l; 57 | } 58 | 59 | // If right child is greater than largest 60 | if(r < n && Arr[r] > Arr[largest]) 61 | { 62 | largest = r; 63 | } 64 | 65 | // If largest is not root 66 | if(largest != i) 67 | { 68 | int temp = Arr[i]; 69 | Arr[i] = Arr[largest]; 70 | Arr[largest] = temp; 71 | heapify(Arr, largest, n); 72 | } 73 | 74 | } 75 | 76 | void printArray(int Arr[], int n) 77 | { 78 | for (int i = 0; i < n ; i++) 79 | { 80 | printf("%d ",Arr[i]); 81 | } 82 | printf("\n"); 83 | } 84 | -------------------------------------------------------------------------------- /Sorting_Algorithms/insertion_sort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chhayac/algo_and_data_structures/271ca963fba90fa1632b27a0f39501266bc2bd62/Sorting_Algorithms/insertion_sort -------------------------------------------------------------------------------- /Sorting_Algorithms/insertion_sort.c: -------------------------------------------------------------------------------- 1 | /* Program to implement Insertion Sort 2 | * The insertion sort algorithm is a simple sorting algorithm which sorts the array by shifting elements one by one. 3 | * 4 | * Example: 5 | * Enter 4 elements of the array: 12 11 13 5 6 | * Sorted array: 7 | * 5 11 12 13 8 | * 9 | * Enter the number of elements in the array: 120 10 | * Please enter value less than 100 11 | * Enter the number of elements in the array: 12 | */ 13 | 14 | #include 15 | #define MAX_SIZE 100 16 | 17 | void insertion_sort(int arr[], int); 18 | void printArray(int arr[], int); 19 | 20 | int main(void) 21 | { 22 | int arr[MAX_SIZE]; 23 | int size_array; 24 | do 25 | { 26 | printf("Enter the number of elements in the array: "); 27 | scanf("%d", &size_array); 28 | if(size_array > MAX_SIZE) 29 | { 30 | printf("Please enter value less than 100\n"); 31 | } 32 | } while(size_array > MAX_SIZE); 33 | 34 | printf("Enter %d elements of the array: ", size_array); 35 | for(int i = 0; i < size_array; i++) 36 | { 37 | scanf("%d", &arr[i]); 38 | } 39 | insertion_sort(arr, size_array); 40 | printf("Sorted array:\n"); 41 | printArray(arr, size_array); 42 | return 0; 43 | } 44 | 45 | void insertion_sort(int arr[], int n) 46 | { 47 | int i, j, key; 48 | for(i = 1; i < n; i++) 49 | { 50 | key = arr[i]; 51 | j = i - 1; 52 | while(j >= 0 && arr[j] > key) 53 | { 54 | arr[j+1] = arr[j]; 55 | j = j - 1; 56 | } 57 | arr[j+1] = key; 58 | } 59 | } 60 | 61 | void printArray(int arr[], int n) 62 | { 63 | for(int i = 0; i < n; i++) 64 | { 65 | printf("%d ", arr[i]); 66 | } 67 | printf("\n"); 68 | } 69 | -------------------------------------------------------------------------------- /Sorting_Algorithms/merge_sort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chhayac/algo_and_data_structures/271ca963fba90fa1632b27a0f39501266bc2bd62/Sorting_Algorithms/merge_sort -------------------------------------------------------------------------------- /Sorting_Algorithms/merge_sort.c: -------------------------------------------------------------------------------- 1 | /* Program to implement merge sort. 2 | * Merge Sort is a Divide and Conquer algorithm. It divides input array in two halves, 3 | * calls itself for the two halves and then merges the two sorted halves. 4 | * The merge() function is used for merging two halves. 5 | * 6 | * Example: 7 | * Enter the number of elements: 5 8 | * Enter 5 elements of the array: 3 2 1 5 4 9 | * Sorted array: 10 | * 1 2 3 4 5 11 | */ 12 | 13 | #include 14 | #define MAX_SIZE 100 15 | 16 | void mergeSort(int arr[], int , int); 17 | void merge(int arr[], int , int, int); 18 | void printArray(int arr[], int); 19 | 20 | int main(void) 21 | { 22 | int arr[MAX_SIZE]; 23 | int size_array; 24 | do 25 | { 26 | printf("Enter the number of elements: "); 27 | scanf("%d", &size_array); 28 | if(size_array > MAX_SIZE) 29 | { 30 | printf("Please enter value less than %d\n", MAX_SIZE); 31 | } 32 | }while(size_array > MAX_SIZE); 33 | printf("Enter %d elements of the array: ", size_array); 34 | for(int i = 0; i < size_array; i++) 35 | { 36 | scanf("%d",&arr[i]); 37 | } 38 | mergeSort(arr, 0, size_array - 1); 39 | printf("Sorted array:\n"); 40 | printArray(arr, size_array); 41 | return 0; 42 | } 43 | 44 | void mergeSort(int arr[], int l, int r) 45 | { 46 | if(r > l) 47 | { 48 | int mid = l + (r-l)/2; 49 | mergeSort(arr, l, mid); 50 | mergeSort(arr, mid+1, r); 51 | merge(arr, l, mid, r); 52 | } 53 | } 54 | 55 | //l is left index and r is right index 56 | void merge(int arr[], int l, int mid, int r) 57 | { 58 | int i = 0, j = 0, k = 0; 59 | int n1 = mid-l+1; 60 | int n2 = r-mid; 61 | 62 | // create two temporary arrays L and R of size n1 and n2 63 | int L[n1], R[n2]; 64 | 65 | for(i = 0; i < n1; i++) 66 | { 67 | L[i] = arr[l+i]; 68 | } 69 | for(j = 0; j< n2; j++) 70 | { 71 | R[j] = arr[mid+j+1]; 72 | } 73 | 74 | /* merge temp arrays back to arr[l..r] */ 75 | i = 0; // initial index of 1st subarray 76 | j = 0; // initial index of 2nd subarray 77 | k = l; // initial index of merge subarray 78 | 79 | while( i < n1 && j < n2) 80 | { 81 | if(L[i] <= R[j]) 82 | { 83 | arr[k] = L[i]; 84 | i++; 85 | } 86 | else 87 | { 88 | arr[k] = R[j]; 89 | j++; 90 | } 91 | k++; 92 | } 93 | 94 | while(i < n1) 95 | { 96 | arr[k] = L[i]; 97 | i++; 98 | k++; 99 | } 100 | 101 | while(j < n2) 102 | { 103 | arr[k] = R[j]; 104 | j++; 105 | k++; 106 | } 107 | } 108 | 109 | 110 | void printArray(int arr[], int n) 111 | { 112 | for(int i = 0; i < n; i++) 113 | { 114 | printf("%d ",arr[i]); 115 | } 116 | printf("\n"); 117 | } 118 | -------------------------------------------------------------------------------- /Sorting_Algorithms/quick_sort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chhayac/algo_and_data_structures/271ca963fba90fa1632b27a0f39501266bc2bd62/Sorting_Algorithms/quick_sort -------------------------------------------------------------------------------- /Sorting_Algorithms/quick_sort.c: -------------------------------------------------------------------------------- 1 | /* Program to implement Quick Sort. 2 | * QuickSort is a Divide and Conquer algorithm. It picks an 3 | * element as pivot and partitions the given array around the picked pivot. 4 | * We have taken laste element as pivot in the function partition. 5 | * The key process in quickSort is partition(). Target of partitions is, 6 | * given an array and an element x of array as pivot, put x at its correct position in sorted array 7 | * and put all smaller elements (smaller than x) before x, and put all greater elements (greater than x) after x. 8 | * 9 | * Example: 10 | * Enter 5 elements of the array: 10 30 80 20 70 11 | * Sorted array: 12 | * 10 20 30 70 80 13 | */ 14 | 15 | #include 16 | #define MAX_SIZE 100 17 | 18 | int partition(int arr[], int, int); 19 | void QuickSort(int arr[], int, int); 20 | void swap(int *, int *); 21 | void printArray(int arr[], int); 22 | 23 | int main(void) 24 | { 25 | int arr[MAX_SIZE]; 26 | int size_array; 27 | do 28 | { 29 | printf("Enter the number of elements: "); 30 | scanf("%d", &size_array); 31 | if(size_array > MAX_SIZE) 32 | { 33 | printf("Please enter value less than %d\n", MAX_SIZE); 34 | } 35 | }while(size_array > MAX_SIZE); 36 | printf("Enter %d elements of the array: ", size_array); 37 | for(int i = 0; i < size_array; i++) 38 | { 39 | scanf("%d",&arr[i]); 40 | } 41 | QuickSort(arr, 0, size_array - 1); 42 | printf("Sorted array:\n"); 43 | printArray(arr, size_array); 44 | return 0; 45 | } 46 | 47 | void QuickSort(int arr[], int left_index, int right_index) 48 | { 49 | int pi; 50 | if(right_index > left_index) 51 | { 52 | // pi is partitioning index 53 | pi = partition(arr, left_index, right_index); 54 | QuickSort(arr, left_index, pi-1); // before pi 55 | QuickSort(arr, pi+1, right_index); // After pi 56 | } 57 | } 58 | 59 | /* This function takes last element as pivot, places the pivot element at its correct position in sorted 60 | array, and places all elements smaller than pivot to the left of pivot and all elements greater than 61 | pivot to the right of pivot */ 62 | int partition(int arr[], int low, int high) 63 | { 64 | int i,j; 65 | int pivot = arr[high]; 66 | i = low-1; // index of smaller element 67 | for(j = low; j <= high-1; j++) 68 | { 69 | if(arr[j] <= pivot) 70 | { 71 | i++; // increment index of smaller element 72 | swap(&arr[i], &arr[j]); 73 | } 74 | } 75 | swap(&arr[i+1], &arr[high]); 76 | return i+1; 77 | } 78 | 79 | // function to swap two values 80 | void swap(int *a, int *b) 81 | { 82 | int temp; 83 | temp = *a; 84 | *a = *b; 85 | *b = temp; 86 | } 87 | 88 | void printArray(int arr[], int n) 89 | { 90 | for(int i = 0; i < n; i++) 91 | { 92 | printf("%d ",arr[i]); 93 | } 94 | printf("\n"); 95 | } 96 | -------------------------------------------------------------------------------- /Sorting_Algorithms/selection_sort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chhayac/algo_and_data_structures/271ca963fba90fa1632b27a0f39501266bc2bd62/Sorting_Algorithms/selection_sort -------------------------------------------------------------------------------- /Sorting_Algorithms/selection_sort.c: -------------------------------------------------------------------------------- 1 | /* Program to implement Selection Sort 2 | * The selection sort algorithm sorts an array by repeatedly 3 | * finding the minimum element (considering ascending order) from 4 | * unsorted part and putting it at the beginning. 5 | * 6 | * Example: 7 | * Enter 4 elements of the array: 3 2 1 6 8 | * Sorted array: 9 | * 1 2 3 6 10 | * 11 | * Enter the number of elements in the array: 101 12 | * Please enter value less than 100 13 | * Enter the number of elements in the array: 14 | */ 15 | 16 | #include 17 | #define MAX_SIZE 100 18 | 19 | void swap(int *a, int *b); 20 | void selection_sort(int arr[], int); 21 | void printArray(int arr[], int); 22 | 23 | int main(void) 24 | { 25 | int arr[MAX_SIZE]; 26 | int size_array; 27 | do 28 | { 29 | printf("Enter the number of elements in the array: "); 30 | scanf("%d", &size_array); 31 | if(size_array > MAX_SIZE) 32 | { 33 | printf("Please enter value less than 100\n"); 34 | } 35 | } while(size_array > MAX_SIZE); 36 | 37 | printf("Enter %d elements of the array: ", size_array); 38 | for(int i = 0; i < size_array; i++) 39 | { 40 | scanf("%d", &arr[i]); 41 | } 42 | selection_sort(arr, size_array); 43 | printf("Sorted array:\n"); 44 | printArray(arr, size_array); 45 | return 0; 46 | } 47 | 48 | void selection_sort(int arr[], int n) 49 | { 50 | int i,j , min_index; 51 | for(i = 0; i < n-1; i++) 52 | { 53 | min_index = i; 54 | for(j = i+1; j < n; j++) 55 | { 56 | if(arr[j] < arr[min_index]) 57 | { 58 | min_index = j; 59 | } 60 | } 61 | swap(&arr[min_index], &arr[i]); 62 | } 63 | } 64 | 65 | void swap(int *a, int *b) 66 | { 67 | int temp = *a; 68 | *a = *b; 69 | *b = temp; 70 | } 71 | 72 | void printArray(int arr[], int n) 73 | { 74 | for(int i = 0; i < n; i++) 75 | { 76 | printf("%d ", arr[i]); 77 | } 78 | printf("\n"); 79 | } 80 | -------------------------------------------------------------------------------- /anagram.c: -------------------------------------------------------------------------------- 1 | /* Program to determine if two strings are anagram or not. 2 | * Example: 3 | * Enter 1st string: listen 4 | * Enter 2nd string: silent 5 | * The two strings are anagrams. 6 | * 7 | * Enter 1st string: tommarvoloriddle 8 | * Enter 2nd string: iamlordvoldemort 9 | * The two strings are anagrams. 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | void remove_newline(char str[]); 17 | 18 | int main(void) 19 | { 20 | char str1[26], str2[26]; 21 | char count[26] = {0}; 22 | printf("Enter 1st string: "); 23 | fgets(str1, 26, stdin); 24 | remove_newline(str1); 25 | 26 | printf("Enter 2nd string: "); 27 | fgets(str2, 26, stdin); 28 | remove_newline(str2); 29 | 30 | if(strlen(str1) != strlen(str2)) 31 | { 32 | printf("The two strings are not anagrams.\n"); 33 | return 0; 34 | } 35 | int len1 = strlen(str1); 36 | int len2 = strlen(str2); 37 | int i,j; 38 | for(i = 0; i < len1; i++) 39 | { 40 | str1[i] = toupper(str1[i]); 41 | count[str1[i]-'A']++; 42 | } 43 | for(j = 0; j < len2; j++) 44 | { 45 | str2[j] = toupper(str2[j]); 46 | count[str2[j]-'A']--; 47 | } 48 | for(i = 0; i < len2; i++) 49 | { 50 | if(count[str2[i]-'A'] != 0) 51 | { 52 | break; 53 | } 54 | } 55 | if(i == len2) 56 | { 57 | printf("The two strings are anagrams.\n"); 58 | } 59 | else 60 | { 61 | printf("The two strings are not anagrams.\n"); 62 | } 63 | return 0; 64 | } 65 | 66 | void remove_newline(char* str) 67 | { 68 | size_t len_str = strlen(str); 69 | len_str--; 70 | if(*str && str[len_str] == '\n') 71 | { 72 | str[len_str] ='\0'; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /average_using_arrays.c: -------------------------------------------------------------------------------- 1 | /* Program to calculate average of numbers using arrays 2 | * Example: 3 | * Enter the number of elements: 6 4 | * Enter number1: 1 5 | * Enter number2: 2 6 | * Enter number3: 3 7 | * Enter number4: 4 8 | * Enter number5: 5 9 | * Enter number6: 6 10 | * Sum of 6 numbers is 21.00 11 | * Average of 6 numbers is 3.50 12 | */ 13 | 14 | #include 15 | 16 | int main(void) 17 | { 18 | float num[100], sum = 0.0, average = 0.0; 19 | int i, n; 20 | printf("Enter the number of elements: "); 21 | scanf("%d", &n); 22 | while(n > 100 || n <= 0) 23 | { 24 | printf("Please enter value between 1 and 100.\n"); 25 | printf("Enter the number of elements again: "); 26 | scanf("%d", &n); 27 | } 28 | for (i = 0; i < n; i++) 29 | { 30 | printf("Enter number%d: ",i+1); 31 | scanf("%f",&num[i]); 32 | sum += num[i]; 33 | } 34 | average = sum / n; 35 | printf("Sum of the %d numbers is %0.2f\n",n, sum); 36 | printf("Average of the %d numbers is %0.2f\n", n, average); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /binary_search.c: -------------------------------------------------------------------------------- 1 | /* Program to search an element using binary search. 2 | * Binary Search: An algorithm which search a sorted array by repeatedly dividing the search 3 | * interval in half. Begin with an interval covering the whole array. 4 | * If the value of the search key is less than the item in the middle of the 5 | * interval, narrow the interval to the lower half. Otherwise narrow it to the 6 | * upper half. Repeatedly check until the value is found or the interval is empty. 7 | * 8 | * Example: 9 | * Enter the size of the array: 7 10 | * Enter 7 elements of the array: 1 2 3 4 5 6 7 11 | * Enter the element to search: 10 12 | * Element is not present in the array 13 | * 14 | * Example: Enter the size of the array: 2000 15 | * Please enter value less than 1000 16 | * Enter the size of the array: 17 | */ 18 | 19 | #include 20 | #define MAX_SIZE 1000 21 | 22 | int binary_search(int arr[], int , int, int); 23 | 24 | int main(void) 25 | { 26 | int arr[MAX_SIZE]; 27 | int size_array; 28 | int index; 29 | int element; 30 | do 31 | { 32 | printf("Enter the size of the array: "); 33 | scanf("%d", &size_array); 34 | if(size_array > MAX_SIZE) 35 | { 36 | printf("Please enter value less than 1000\n"); 37 | } 38 | } while(size_array > MAX_SIZE); 39 | printf("Enter %d elements of the array: ", size_array); 40 | for(int i = 0; i < size_array; i++) 41 | { 42 | scanf("%d", &arr[i]); 43 | } 44 | printf("Enter the element to search: "); 45 | scanf("%d",&element); 46 | index = binary_search(arr, 0, size_array - 1, element); 47 | (index == -1)? printf("Element is not present in the array\n"): 48 | printf("Element is present at index %d\n", index); 49 | return 0; 50 | } 51 | 52 | 53 | int binary_search(int arr[], int l, int r, int element) 54 | { 55 | while(r >= l) 56 | { 57 | int mid = l + (r-l)/2; 58 | 59 | // check if element is present at mid position 60 | if(element == arr[mid]) 61 | { 62 | return mid; 63 | } 64 | 65 | // check if element is greater than the mid element, ignore left half 66 | if(element > arr[mid]) 67 | { 68 | l = mid + 1; 69 | } 70 | 71 | // if element is less than the mid element, ignore right half 72 | else 73 | { 74 | r = mid - 1; 75 | } 76 | } 77 | // if we reach here, element is not present 78 | return -1; 79 | } 80 | -------------------------------------------------------------------------------- /binary_to_decimal.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Program in C to develop a converter to convert a binary number to its decimal equivalent. 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | int binary_to_decimal(int); 9 | 10 | int main(void) 11 | { 12 | int n, bin; 13 | printf("Enter a binary number (only 1s and 0s) :"); 14 | scanf("%d", &n); 15 | bin = n; 16 | printf("Binary value is %d\n", bin); 17 | 18 | // function to convert binary to decimal 19 | binary_to_decimal(n); 20 | } 21 | 22 | 23 | int binary_to_decimal(int n) 24 | { 25 | int rem; 26 | int dec = 0, base = 1; 27 | while(n > 0) 28 | { 29 | rem = n % 10; 30 | dec = dec + rem * base; 31 | n = n / 10; 32 | base = base * 2; 33 | } 34 | 35 | printf("Decimal equivalent is %d\n", dec); 36 | return 0; 37 | } -------------------------------------------------------------------------------- /binary_tree_vertical.c: -------------------------------------------------------------------------------- 1 | /* Print a binary tree in vertical order. 2 | * Input : 3 | * 1 4 | * / \ 5 | * 2 3 6 | * / \ / \ 7 | * 4 5 6 7 8 | * \ \ 9 | * 8 9 10 | * 11 | * Output : The output of printing a binary tree vertically will be: 12 | * 4 13 | * 2 14 | * 1 5 6 15 | * 3 8 16 | * 7 17 | * 9 18 | */ 19 | 20 | #include 21 | #include 22 | 23 | // A node of Binary tree 24 | struct Node 25 | { 26 | int data; 27 | struct Node *left, *right; 28 | }; 29 | 30 | // Function to create a new binary tree node 31 | struct Node* newNode(int data) 32 | { 33 | struct Node* temp = (struct Node *)malloc(sizeof(struct Node)); 34 | temp->data = data; 35 | temp->left = temp->right = NULL; 36 | return temp; 37 | } 38 | 39 | // Function to find the minimum and maximum horizontal distance of node 40 | // from the root. 41 | // hd is horizontal distance of current node from the root 42 | void findMinMax(struct Node *node, int *min, int *max, int hd) 43 | { 44 | // Base case 45 | if(node == NULL) 46 | { 47 | return; 48 | } 49 | 50 | // Update minimum and maximum 51 | if (hd < *min) 52 | { 53 | *min = hd; 54 | } 55 | else if(hd > *max) 56 | { 57 | *max = hd; 58 | } 59 | 60 | // Recur for left and right subtrees 61 | findMinMax(node->left, min, max, hd-1); 62 | findMinMax(node->right, min, max, hd+1); 63 | } 64 | 65 | // Function to print all nodes on a given line number 66 | void printVerticalLine(struct Node *node, int line_no, int hd) 67 | { 68 | // Base case 69 | if(node == NULL) 70 | { 71 | return; 72 | } 73 | 74 | // If the node is on the given line number 75 | if(hd == line_no) 76 | { 77 | printf("%d ", node->data); 78 | } 79 | 80 | // Recur for left and right subtrees 81 | printVerticalLine(node->left, line_no, hd-1); 82 | printVerticalLine(node->right, line_no, hd+1); 83 | } 84 | 85 | // Main function that prints a given binary tree in vertical order 86 | void verticalOrder(struct Node* root) 87 | { 88 | int min = 0, max = 0; 89 | findMinMax(root, &min, &max, 0); 90 | 91 | // Iterate through all possible vertical lines from the 92 | // leftmost line to the rightmost line and prints nodes line by line 93 | for(int line_no = min; line_no <= max; line_no++) 94 | { 95 | printVerticalLine(root, line_no, 0); 96 | printf("\n"); 97 | } 98 | } 99 | 100 | int main() 101 | { 102 | // Create binary tree 103 | struct Node* root = newNode(1); 104 | root->left = newNode(2); 105 | root->right = newNode(3); 106 | root->left->left = newNode(4); 107 | root->left->right = newNode(5); 108 | root->right->left = newNode(6); 109 | root->right->right = newNode(7); 110 | root->right->left->right = newNode(8); 111 | root->right->right->right = newNode(9); 112 | 113 | printf("Vertical order Traversal: \n"); 114 | verticalOrder(root); 115 | return 0; 116 | } 117 | -------------------------------------------------------------------------------- /bst_delete.c: -------------------------------------------------------------------------------- 1 | /* Program to delete a node with a key from Binary Search Tree. 2 | * 3 | * Input: 50 30 20 40 70 60 80 4 | * Output: Delete 50 5 | * Inorder Traversal of BST: 20 30 40 50 60 70 80 6 | * Inorder Traversal of modified BST: 20 30 40 60 70 80 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | struct node 13 | { 14 | int key; 15 | struct node *left; 16 | struct node *right; 17 | }; 18 | 19 | struct node* newNode(int item) 20 | { 21 | struct node* temp = (struct node*)malloc(sizeof(struct node)); 22 | temp->key = item; 23 | temp->right = temp->left = NULL; 24 | return temp; 25 | } 26 | 27 | struct node* insert(struct node* node, int key) 28 | { 29 | if(node == NULL) 30 | { 31 | return newNode(key); 32 | } 33 | if(key < node->key) 34 | { 35 | node->left = insert(node->left, key); 36 | } 37 | else 38 | { 39 | node->right = insert(node->right, key); 40 | } 41 | return node; 42 | } 43 | 44 | void inorder(struct node* root) 45 | { 46 | if(root != NULL) 47 | { 48 | inorder(root->left); 49 | printf("%d ", root->key); 50 | inorder(root->right); 51 | } 52 | } 53 | 54 | struct node* minValueNode(struct node* node) 55 | { 56 | struct node* current = node; 57 | while(current->left != NULL) 58 | { 59 | current = current->left; 60 | } 61 | return current; 62 | } 63 | 64 | // function deleted the key and returns the new root 65 | struct node* deleteNode(struct node* root, int key) 66 | { 67 | // base case 68 | if (root == NULL) 69 | { 70 | return root; 71 | } 72 | if(key < root->key) 73 | { 74 | root->left = deleteNode(root->left, key); 75 | } 76 | else if(key > root->key) 77 | { 78 | root->right = deleteNode(root->right, key); 79 | } 80 | else 81 | { 82 | if (root->left == NULL) 83 | { 84 | struct node* temp = root->right; 85 | free(root); 86 | return temp; 87 | } 88 | else if(root->right == NULL) 89 | { 90 | struct node* temp = root->left; 91 | free(root); 92 | return temp; 93 | } 94 | struct node* temp = minValueNode(root->right); 95 | root->key = temp->key; 96 | root->right = deleteNode(root->right, temp->key); 97 | } 98 | return root; 99 | } 100 | 101 | int main() 102 | { 103 | struct node* root = NULL; 104 | root = insert(root, 50); 105 | insert(root, 30); 106 | insert(root, 20); 107 | insert(root, 40); 108 | insert(root, 70); 109 | insert(root, 60); 110 | insert(root, 80); 111 | 112 | printf("Inorder Traversal of BST: "); 113 | inorder(root); 114 | root = deleteNode(root, 30); 115 | printf("\nInorder Traversal of modified BST: "); 116 | inorder(root); 117 | printf("\n"); 118 | return 0; 119 | } 120 | -------------------------------------------------------------------------------- /bst_insert.c: -------------------------------------------------------------------------------- 1 | /* Program to insert nodes with given keys in binary search trees. 2 | * 3 | * Input: 50 30 20 40 70 60 80 4 | * Output: 20 30 40 50 60 70 80 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | struct node 11 | { 12 | int key; 13 | struct node *left; 14 | struct node *right; 15 | }; 16 | 17 | // function to create new Binary search tree node 18 | struct node* newNode(int item) 19 | { 20 | struct node* temp = (struct node *) malloc(sizeof(struct node)); 21 | temp -> key = item; 22 | temp -> left = temp -> right = NULL; 23 | return temp; 24 | } 25 | 26 | // function for inorder traversal of binary search tree 27 | void inorder(struct node* root) 28 | { 29 | if(root != NULL) 30 | { 31 | inorder(root->left); 32 | printf("%d ", root->key); 33 | inorder(root->right); 34 | } 35 | } 36 | 37 | // function to insert new node with given key in binary search tree 38 | struct node* insert(struct node* node, int key) 39 | { 40 | //if tree is empty, return new node 41 | if(node == NULL) 42 | { 43 | return newNode(key); 44 | } 45 | if(key < node->key) 46 | { 47 | node->left = insert(node->left, key); 48 | } 49 | else if(key > node->key) 50 | { 51 | node->right = insert(node->right, key); 52 | } 53 | return node; 54 | } 55 | 56 | int main() 57 | { 58 | struct node* root = NULL; 59 | root = insert(root, 50); 60 | insert(root, 30); 61 | insert(root, 20); 62 | insert(root, 40); 63 | insert(root, 70); 64 | insert(root, 60); 65 | insert(root, 80); 66 | 67 | inorder(root); 68 | printf("\n"); 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /bst_or_not.c: -------------------------------------------------------------------------------- 1 | /* Program to check if a binary tree is BST or not. 2 | * Example: 3 | * Input: 4 2 5 1 3 4 | * Min = 1 , Max = 5 5 | * Output: Binary Tree is a Binary Search Tree 6 | */ 7 | 8 | #include 9 | #include 10 | #define true 1 11 | #define false 0 12 | struct node 13 | { 14 | int key; 15 | struct node *left; 16 | struct node *right; 17 | }; 18 | 19 | int isBSTUtil(struct node* node, int min, int max); 20 | 21 | int isBST(struct node* node) 22 | { 23 | return (isBSTUtil(node, 1, 5)); 24 | } 25 | 26 | int isBSTUtil(struct node *node, int min, int max) 27 | { 28 | if (node == NULL) 29 | { 30 | return 1; 31 | } 32 | if(node->key < min || node->key > max) 33 | { 34 | return 0; 35 | } 36 | return isBSTUtil(node->left, min, node->key-1) && isBSTUtil(node->right, node->key+1, max); 37 | } 38 | 39 | struct node* newNode(int item) 40 | { 41 | struct node* temp = (struct node*)malloc(sizeof(struct node)); 42 | temp->key = item; 43 | temp->left = temp->right = NULL; 44 | return temp; 45 | } 46 | 47 | int main() 48 | { 49 | struct node* root = newNode(4); 50 | root->left = newNode(2); 51 | root->right = newNode(5); 52 | root->left->left = newNode(1); 53 | root->left->right = newNode(3); 54 | 55 | if(isBST(root)) 56 | { 57 | printf("Binary Tree is a Binary Search Tree.\n"); 58 | } 59 | else 60 | { 61 | printf("Binary tree is not a Binary Search Tree\n"); 62 | } 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /bst_search.c: -------------------------------------------------------------------------------- 1 | /* Program to search a node with a given key in a binary search tree(BST). 2 | * 3 | * Input: 50 30 20 40 70 60 80 4 | * Output: Inorder traversal of the tree is: 20 30 40 50 60 70 80 5 | * Searched node 40 is found 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | struct node 12 | { 13 | int key; 14 | struct node *left; 15 | struct node *right; 16 | }; 17 | 18 | // function to create new Binary search tree node 19 | struct node* newNode(int item) 20 | { 21 | struct node* temp = (struct node *) malloc(sizeof(struct node)); 22 | temp -> key = item; 23 | temp -> left = temp -> right = NULL; 24 | return temp; 25 | } 26 | 27 | // function for inorder traversal of binary search tree 28 | void inorder(struct node* root) 29 | { 30 | if(root != NULL) 31 | { 32 | inorder(root->left); 33 | printf("%d ", root->key); 34 | inorder(root->right); 35 | } 36 | } 37 | 38 | // function to insert new node with given key in binary search tree 39 | struct node* insert(struct node* node, int key) 40 | { 41 | //if tree is empty, return new node 42 | if(node == NULL) 43 | { 44 | return newNode(key); 45 | } 46 | if(key < node->key) 47 | { 48 | node->left = insert(node->left, key); 49 | } 50 | else if(key > node->key) 51 | { 52 | node->right = insert(node->right, key); 53 | } 54 | return node; 55 | } 56 | 57 | // function for searching a node with a given key in BST 58 | struct node* search(struct node* root, int key) 59 | { 60 | // base case: if root is null or key is present at root 61 | if(root == NULL || root->key == key) 62 | { 63 | return root; 64 | } 65 | // if key is smaller than root's key 66 | if(key < root->key) 67 | { 68 | return search(root->left, key); 69 | } 70 | // if key is larger than root's key 71 | return search(root->right, key); 72 | } 73 | 74 | int main() 75 | { 76 | struct node* root = NULL; 77 | root = insert(root, 50); 78 | insert(root, 30); 79 | insert(root, 20); 80 | insert(root, 40); 81 | insert(root, 70); 82 | insert(root, 60); 83 | insert(root, 80); 84 | 85 | printf("Inorder traversal of the tree is: "); 86 | inorder(root); 87 | printf("\n"); 88 | 89 | struct node* temp = search(root, 40); 90 | if(temp) 91 | { 92 | printf("Searched node %d is found\n", temp->key); 93 | } 94 | else 95 | { 96 | printf("Key not found in the BST\n"); 97 | } 98 | return 0; 99 | } 100 | -------------------------------------------------------------------------------- /bubble_sort.c: -------------------------------------------------------------------------------- 1 | /* Program to implement Bubble Sort 2 | * Bubble Sort is a sorting algorithm that works by repeatedly 3 | * swapping the adjacent elements if they are in wrong order. 4 | * 5 | * Example: 6 | * Enter the size of the array:4 7 | * Enter 4 elements in the array: 10 2 1 4 8 | * Sorted array is: 1 2 4 10 9 | * 10 | * Enter the size of the array: 1000 11 | * Max size of array is 100. Please enter value less than 100 12 | * Enter the size of the array: 13 | */ 14 | 15 | #include 16 | #define MAX_SIZE 100 // maximum size of the array 17 | 18 | #define bool int 19 | #define true 1 20 | #define false 0 21 | 22 | void bubble_sort(int arr[], int); 23 | void swap(int *a, int *b); 24 | void printArray(int arr[], int); 25 | 26 | int main(void) 27 | { 28 | int arr[MAX_SIZE]; 29 | int size_array; 30 | do 31 | { 32 | printf("Enter the size of the array: "); 33 | scanf("%d",&size_array); 34 | if(size_array > MAX_SIZE) 35 | { 36 | printf("Max size of array is 100. Please enter value less than 100.\n"); 37 | } 38 | }while(size_array > MAX_SIZE); 39 | printf("Enter %d elements in the array: ", size_array); 40 | for(int i = 0; i < size_array; i++) 41 | { 42 | scanf("%d",&arr[i]); 43 | } 44 | 45 | bubble_sort(arr, size_array); 46 | printf("Sorted array is: "); 47 | printArray(arr, size_array); 48 | return 0; 49 | } 50 | 51 | /* Function to sort the array using bubble sort */ 52 | void bubble_sort(int arr[], int n) 53 | { 54 | int i, j; 55 | bool swapped; 56 | for (i = 0; i < n-1 ; i++) 57 | { 58 | swapped = false; 59 | for(j = 0; j < n-i-1; j++) 60 | { 61 | if(arr[j] > arr[j+1]) 62 | { 63 | swap(&arr[j],&arr[j+1]); 64 | swapped = true; 65 | } 66 | } 67 | // If no two elements are swapped by inner loop, then break 68 | if(swapped == false) 69 | break; 70 | } 71 | } 72 | 73 | /* Function to swap 2 values */ 74 | void swap(int *a, int *b) 75 | { 76 | int temp = *a; 77 | *a = *b; 78 | *b = temp; 79 | } 80 | 81 | /* Function to print the sorted array */ 82 | void printArray(int arr[], int size) 83 | { 84 | int i; 85 | for (i = 0; i < size; i++) 86 | { 87 | printf("%d ", arr[i]); 88 | } 89 | printf("\n"); 90 | } 91 | -------------------------------------------------------------------------------- /build_max_heap.c: -------------------------------------------------------------------------------- 1 | /* Program to build max heap with an input array 2 | * 3 | * Input : 5,3,17,10,84,19,6,22,9 4 | * Output : 84 22 19 10 3 17 6 5 9 5 | * 6 | */ 7 | 8 | #include 9 | #include 10 | #define left(x) (2*x + 1) 11 | #define right(x) (2*x + 2) 12 | 13 | void build_maxheap(int Arr[], int); 14 | void max_heapify(int Arr[], int, int); 15 | void printArray(int Arr[], int); 16 | 17 | int main(void) 18 | { 19 | int Arr[] = {5,3,17,10,84,19,6,22,9}; 20 | int n = sizeof(Arr)/sizeof(Arr[0]); // n is heap length / length of the array 21 | build_maxheap(Arr,n); 22 | printArray(Arr,n); 23 | return 0; 24 | } 25 | 26 | void build_maxheap(int Arr[], int n) 27 | { 28 | int i; 29 | for(i = n/2 - 1; i >= 0 ; i--) 30 | { 31 | max_heapify(Arr, i, n); 32 | } 33 | } 34 | 35 | void max_heapify(int Arr[], int i, int n) 36 | { 37 | int l = left(i); 38 | int r = right(i); 39 | int largest ; 40 | if(l < n && Arr[l] > Arr[i]) 41 | { 42 | largest = l; 43 | } 44 | else 45 | { 46 | largest = i; 47 | } 48 | if(r < n && Arr[r] > Arr[largest]) 49 | { 50 | largest = r; 51 | } 52 | if(largest != i) 53 | { 54 | int temp = Arr[i]; 55 | Arr[i] = Arr[largest]; 56 | Arr[largest] = temp; 57 | max_heapify(Arr, largest, n); 58 | } 59 | 60 | } 61 | 62 | void printArray(int Arr[], int n) 63 | { 64 | for (int i = 0; i < n ; i++) 65 | { 66 | printf("%d ",Arr[i]); 67 | } 68 | printf("\n"); 69 | } 70 | -------------------------------------------------------------------------------- /change_return.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Program to calculate the minimum number of coins required to give a user change. 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | int main(void) 9 | { 10 | float m = 0.0; 11 | int min_coins = 0, n = 0; 12 | int quarters = 0, dimes = 0, nickels = 0, pennies = 0; 13 | printf("Hi! How much change is owed in dollars?\n"); 14 | do 15 | { 16 | scanf("%f", &m); 17 | } while (m < 0); 18 | 19 | m = m * 100; 20 | n = round(m); 21 | 22 | if (n >= 1) 23 | { 24 | quarters = n / 25; 25 | dimes = (n - (25 * quarters))/10; 26 | nickels = (n -(25 * quarters + 10 * dimes))/5; 27 | pennies= (n -(25 * quarters + 10 * dimes + 5 * nickels))/1; 28 | } 29 | 30 | min_coins = quarters + dimes + nickels + pennies; 31 | printf("Minimum number of coins required for change are: %d", min_coins); 32 | printf("\n"); 33 | 34 | } -------------------------------------------------------------------------------- /concat.c: -------------------------------------------------------------------------------- 1 | /* Program to concat two strings without using library function 2 | * Enter 1st string: Today is a wonderful 3 | * Enter 2nd string: day 4 | * Concated string is "Today is a wonderfulday" 5 | */ 6 | 7 | #include 8 | 9 | void concat(char [], char []); 10 | void remove_newline(char str[]); 11 | size_t string_length(char str[]); 12 | 13 | int main(void) 14 | { 15 | char str1[100], str2[100]; 16 | printf("Enter 1st string: "); 17 | fgets(str1, 100, stdin); 18 | remove_newline(str1); 19 | 20 | printf("Enter 2nd string: "); 21 | fgets(str2, 100, stdin); 22 | remove_newline(str2); 23 | 24 | concat(str1, str2); 25 | printf("Concated string is \"%s\"\n", str1); 26 | return 0; 27 | } 28 | 29 | void concat(char str1[], char str2[]) 30 | { 31 | int i,j; 32 | i = 0; 33 | while (str1[i] != '\0') 34 | { 35 | ++i; 36 | } 37 | for(j = 0; str2[j] != '\0'; i++,j++) 38 | { 39 | str1[i] = str2[j]; 40 | } 41 | str1[i] = '\0'; 42 | } 43 | 44 | void remove_newline(char* str) 45 | { 46 | size_t len_str = string_length(str); 47 | len_str--; 48 | if (*str && str[len_str] == '\n') 49 | str[len_str] = '\0'; 50 | } 51 | 52 | size_t string_length(char * str) 53 | { 54 | size_t len_str = 0; 55 | for (size_t i = 0; str[i] != '\0'; i++) 56 | { 57 | len_str++; 58 | } 59 | return len_str; 60 | } 61 | -------------------------------------------------------------------------------- /credit_card_validation.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Program to determine whether a provided credit card number is valid according to Luhn’s algorithm. 3 | * According to Luhn’s algorithm, you can determine if a credit card number is (syntactically) valid as follows: 4 | * 1. Multiply every other digit by 2, starting with the number’s second-to-last digit, 5 | * and then add those products' digits together. 6 | * 2. Add the sum to the sum of the digits that weren’t multiplied by 2. 7 | * 3. If the total’s last digit is 0 (or, put more formally, if the total modulo 10 is 8 | * congruent to 0), the number is valid! 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #define bool int 16 | #define true 1 17 | #define false 0 18 | 19 | int cleanStdin(); 20 | int countCreditCardDigits(long long); 21 | int get_first_two_digits(long long, int); 22 | bool credit_card_type(int, int); 23 | void validate_credit_card(long long); 24 | 25 | int main(void) 26 | { 27 | long long cc_num = 0; 28 | char n; 29 | int c = 0; 30 | bool firstTime = true; 31 | do 32 | { 33 | if (firstTime) 34 | { 35 | firstTime = false; 36 | } 37 | else 38 | { 39 | printf("Retry: "); 40 | } 41 | 42 | printf("Enter the credit card number without any dashes or space: "); 43 | } while ((scanf("%llu%c", &cc_num, &n) != 2 || n != '\n') && cleanStdin()); 44 | 45 | c = countCreditCardDigits(cc_num); 46 | int start_digits = get_first_two_digits(cc_num, c); 47 | if (credit_card_type(c, start_digits)) 48 | { 49 | validate_credit_card(cc_num); 50 | } 51 | return 0; 52 | } 53 | 54 | int cleanStdin() 55 | { 56 | while(getchar() != '\n') { } 57 | return 1; 58 | } 59 | 60 | // function to count the number of digits in a credit card number 61 | // 62 | int countCreditCardDigits(long long n) 63 | { 64 | int c = 0; 65 | while (n) 66 | { 67 | n /= 10; 68 | c++; 69 | } 70 | return c; 71 | } 72 | 73 | // function to return first two digits of a credit card number 74 | // 75 | int get_first_two_digits(long long n, int c) 76 | { 77 | return n / pow(10, (c - 2)); 78 | } 79 | 80 | // function to find type of card based on 81 | // on number of digits and first two digits 82 | // 83 | bool credit_card_type(int c, int start_digits) 84 | { 85 | bool valid_credit_card = true; 86 | 87 | if(c == 15 && (start_digits == 34 || start_digits == 37)) 88 | { 89 | printf("It's an American Express Credit Card\n"); 90 | } 91 | else if(c == 16 && (start_digits >= 51 && start_digits <=55)) 92 | { 93 | printf("It's a MasterCard\n"); 94 | } 95 | else if((c == 13 || c == 16) && (start_digits >= 40 && start_digits <=50)) 96 | { 97 | printf("It's a Visa Card\n"); 98 | } 99 | else 100 | { 101 | printf("Invalid Card\n" ); 102 | valid_credit_card = false; 103 | } 104 | return valid_credit_card; 105 | } 106 | 107 | // function to check checksum of a credit card number. 108 | // 109 | void validate_credit_card(long long n) 110 | { 111 | int rem = 0, total = 0, checksum = 0; 112 | int even_place = 0; 113 | int odds = 0, evens = 0; 114 | while(n) 115 | { 116 | odds += ( n % 10 ); 117 | n = n / 10; 118 | even_place = n % 10; 119 | even_place *= 2; 120 | if(even_place > 9) // checking if number is 2 or more digits, then add individual digits 121 | { 122 | rem = even_place % 10; 123 | evens += (rem + 1); 124 | } 125 | else 126 | { 127 | evens += even_place; 128 | } 129 | n = n / 10; 130 | } 131 | 132 | total = odds + evens; 133 | checksum = total % 10; 134 | if(checksum != 0) 135 | { 136 | printf("The credit card number is invalid\n"); 137 | } 138 | else 139 | { 140 | printf("Success!! Credit card number is valid\n"); 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /decimal_to_binary.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Develop a converter to convert a decimal number to its binary equivalent. 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | int decimal_to_binary(int); 9 | 10 | int main(void) 11 | { 12 | int n, dec; 13 | printf("Enter a decimal value: "); 14 | scanf("%d", &n); 15 | dec = n; 16 | printf("The decimal value is %d\n", dec); 17 | 18 | // function to convert decimal to binary 19 | decimal_to_binary(n); 20 | } 21 | 22 | int decimal_to_binary(int n) 23 | { 24 | int rem; 25 | int bin = 0, base = 1; 26 | while(n != 0) 27 | { 28 | rem = n % 2; 29 | bin = bin + rem * base; 30 | n = n / 2; 31 | base = base * 10; 32 | } 33 | printf("Binary equivalent is %d\n", bin); 34 | return 0; 35 | } -------------------------------------------------------------------------------- /factorial_loop.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Program to calculate factorial of a number using loop 3 | * Example: 4 | * Enter a number whose factorial you wish to calculate: 10 5 | * Factorial of 10 is 3628800 6 | */ 7 | 8 | #include 9 | 10 | unsigned long long factorial(int); 11 | 12 | int main(void) 13 | { 14 | int num; 15 | printf("Enter a number whose factorial you wish to calculate: "); 16 | scanf("%d", &num); 17 | unsigned long long number = factorial(num); 18 | printf("Factorial of %d is %llu\n", num, number); 19 | return 0; 20 | } 21 | 22 | // Function to find factorial of a number using for loop 23 | // 24 | unsigned long long factorial(int num) 25 | { 26 | unsigned long long fact = 1; 27 | int n = num; 28 | for(int i = 1; i < num; i++) 29 | { 30 | fact = fact * n; 31 | n-= 1; 32 | } 33 | return fact; 34 | } 35 | -------------------------------------------------------------------------------- /factorial_recursion.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Program to calculate factorial of a number using recursion method 3 | * Example: 4 | * Enter a number whose factorial you wish to calculate: 20 5 | * Factorial of 20 is 2432902008176640000 6 | */ 7 | 8 | #include 9 | 10 | unsigned long long factorial(int); 11 | 12 | int main(void) 13 | { 14 | int num; 15 | printf("Enter a number whose factorial you wish to calculate: "); 16 | scanf("%d", &num); 17 | printf("Factorial of %d is %llu\n", num, factorial(num)); 18 | return 0; 19 | } 20 | 21 | unsigned long long factorial(int num) 22 | { 23 | if(num < 2) 24 | { 25 | return 1; 26 | } 27 | else 28 | { 29 | return(num * factorial(num - 1)); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /fibonacci.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | This program generates a fibonacci sequence till nth term. 4 | Fibonacci Sequence - Enter a number and have the program generate the Fibonacci 5 | sequence (1,1,2,3,5,8,..) to that number or to the Nth number. 6 | */ 7 | 8 | #include 9 | 10 | void fib(int); 11 | 12 | int main(void) 13 | { 14 | int n; 15 | printf("Enter the value of n:"); 16 | scanf("%d",&n); 17 | fib(n); 18 | return 0; 19 | } 20 | 21 | // function to print fibonacci series 22 | void fib(int n) 23 | { 24 | int a=0, b=1, c=1; 25 | 26 | for (int i = 1; i <= n ; i++) 27 | { 28 | if (i == n) 29 | { 30 | printf("%d\n", c); 31 | } 32 | else 33 | { 34 | printf("%d, ", c); 35 | } 36 | c = a + b; 37 | a = b; 38 | b = c; 39 | } 40 | } -------------------------------------------------------------------------------- /first_last_occurence.c: -------------------------------------------------------------------------------- 1 | /* Given a sorted array with possibly duplicate elements, the task 2 | * is to find indexes of first and last occurrences of an element x in the given array. 3 | * Example: 4 | * Enter number of elements: 10 5 | * Enter the elements of the array: 1 3 5 5 5 5 6 8 10 11 6 | * Enter the element whose occurence you want to find: 5 7 | * First occurence = 2 8 | * Last occurence = 5 9 | * 10 | * Enter number of elements: 10 11 | * Enter the elements of the array: 1 3 5 5 5 5 6 8 10 11 12 | * Enter the element whose occurence you want to find: 8 13 | * First occurence = 7 14 | * Last occurence = 7 15 | */ 16 | 17 | #include 18 | #define MAX_SIZE 30 19 | 20 | void occurence(int arr[], int, int); 21 | 22 | int main(void) 23 | { 24 | int arr[MAX_SIZE]; 25 | int n; // n being size of the array 26 | int element; 27 | do 28 | { 29 | printf("Enter number of elements: "); 30 | scanf("%d", &n); 31 | if(n > MAX_SIZE) 32 | { 33 | printf("Please enter value less than %d\n", MAX_SIZE); 34 | } 35 | }while(n > MAX_SIZE); 36 | printf("Enter the elements of the array: "); 37 | for(int i = 0; i < n; i++) 38 | { 39 | scanf("%d", &arr[i]); 40 | } 41 | printf("Enter the element whose occurence you want to find: "); 42 | scanf("%d", &element); 43 | occurence(arr,n, element); 44 | return 0; 45 | } 46 | 47 | void occurence(int arr[], int n, int element) 48 | { 49 | int i, j, s, e =0; 50 | int flag = 0; 51 | for (i = 0; i < n; i++) 52 | { 53 | if(arr[i] == element) 54 | { 55 | flag = 1; 56 | s = i; 57 | break; 58 | } 59 | } 60 | for (j = n - 1; j >= 0; j--) 61 | { 62 | if(arr[j] == element) 63 | { 64 | flag = 1; 65 | e = j; 66 | break; 67 | } 68 | } 69 | 70 | if (flag == 1) 71 | { 72 | printf("First occurence = %d\n", s); 73 | printf("Last occurence = %d\n", e); 74 | } 75 | else 76 | { 77 | printf("Element not found\n"); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /half_pyramid_pattern.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Program to print out a half-pyramid of a specified height. 3 | 4 | ## 5 | ### 6 | #### 7 | ##### 8 | ###### 9 | 10 | */ 11 | 12 | #include 13 | 14 | int main(void) 15 | { 16 | int n = 0; 17 | do{ 18 | printf(" Enter the height of half-pyramid: "); 19 | scanf("%d", &n); 20 | } while(n < 0 || n > 23); 21 | 22 | for(int i=1; i<=n; i++) 23 | { 24 | for(int j=1; j<= (n-i); j++) 25 | { 26 | printf(" "); 27 | } 28 | for(int k=0; k<=i; k++) 29 | { 30 | printf("#"); 31 | } 32 | printf("\n"); 33 | } 34 | } 35 | 36 | 37 | -------------------------------------------------------------------------------- /hamming_distance.c: -------------------------------------------------------------------------------- 1 | /* The Hamming distance between two integers is the number of positions 2 | * at which the corresponding bits are different. 3 | * 4 | * Given two integers x and y, calculate the Hamming distance. 5 | * 6 | * Note: 7 | * 0 ≤ x, y < 231. 8 | * 9 | * Example: 10 | * Input: x = 1, y = 4 11 | * Output: 2 12 | * Explanation: 13 | * 1 (0 0 0 1) 14 | * 4 (0 1 0 0) 15 | ↑ ↑ 16 | * The above arrows point to positions where the corresponding bits are different. 17 | */ 18 | 19 | #include 20 | 21 | int hammingDistance(int, int); 22 | 23 | int main() 24 | { 25 | int x; 26 | int y; 27 | printf("Enter the 1st integer: "); 28 | scanf("%d", &x); 29 | printf("Enter the 2nd integer: "); 30 | scanf("%d", &y); 31 | int ham_distance = hammingDistance(x , y); 32 | printf("Hamming distance between x & y is %d\n", ham_distance); 33 | } 34 | 35 | int hammingDistance(int x, int y) 36 | { 37 | int z = x ^ y; 38 | int count = 0; 39 | while(z) 40 | { 41 | count += z % 2; 42 | z >>= 1; 43 | } 44 | return count; 45 | } 46 | -------------------------------------------------------------------------------- /happy_number.c: -------------------------------------------------------------------------------- 1 | /* Program to check if a given postive number is a happy number or not. 2 | * 3 | * A happy number is a number defined by the following process: Starting with any 4 | * positive integer, replace the number by the sum of the squares of its digits, 5 | * and repeat the process until the number equals 1 (where it will stay), or it loops 6 | * endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers. 7 | * Example: 8 | * Enter a positive number: 82 9 | * Number entered is a happy number. 10 | */ 11 | 12 | #include 13 | void isHappy(int); 14 | 15 | int main(void) 16 | { 17 | int num; 18 | printf("Enter a positive number: "); 19 | scanf("%d", &num); 20 | isHappy(num); 21 | return 0; 22 | } 23 | 24 | void isHappy(int num) 25 | { 26 | int digit = 0; 27 | while(num!=1 && num!=4) 28 | { 29 | int sum = 0; 30 | while(num > 0) 31 | { 32 | digit = num % 10; 33 | num = num / 10; 34 | sum += digit * digit; 35 | } 36 | num = sum; 37 | } 38 | if(num == 1) 39 | { 40 | printf("Number entered is a happy number.\n"); 41 | } 42 | else 43 | { 44 | printf("Number entered is not a happy number.\n"); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /heap_sort.c: -------------------------------------------------------------------------------- 1 | /* Program to sort an array using Heap Sort. 2 | * 3 | * Input : 5,3,17,10,84,19,6,22,9 4 | * Output : 3 5 6 9 10 17 19 22 84 5 | * 6 | */ 7 | 8 | #include 9 | #include 10 | #define left(x) (2*x + 1) 11 | #define right(x) (2*x + 2) 12 | 13 | void heapSort(int Arr[], int); 14 | void heapify(int Arr[], int, int); 15 | void printArray(int Arr[], int); 16 | 17 | int main(void) 18 | { 19 | int Arr[] = {5,3,17,10,84,19,6,22,9}; 20 | int n = sizeof(Arr)/sizeof(Arr[0]); // n is heap length / length of the array 21 | heapSort(Arr,n); 22 | printArray(Arr,n); 23 | return 0; 24 | } 25 | 26 | void heapSort(int Arr[], int n) 27 | { 28 | int i; 29 | 30 | // Build heap i.e rearrange the array 31 | for(i = n/2 - 1; i >= 0 ; i--) 32 | { 33 | heapify(Arr, i, n); 34 | } 35 | 36 | // Extract an element from heap one by one 37 | for(i = n-1; i >= 0 ; i--) 38 | { 39 | int temp = Arr[i]; 40 | Arr[i] = Arr[0]; 41 | Arr[0] = temp; 42 | // call heapify on the reduced heap 43 | heapify(Arr, 0, i); 44 | } 45 | } 46 | 47 | void heapify(int Arr[], int i, int n) 48 | { 49 | int l = left(i); 50 | int r = right(i); 51 | int largest = i; // Initialize largest as root 52 | 53 | // If left child is greater than root 54 | if(l < n && Arr[l] > Arr[i]) 55 | { 56 | largest = l; 57 | } 58 | 59 | // If right child is greater than largest 60 | if(r < n && Arr[r] > Arr[largest]) 61 | { 62 | largest = r; 63 | } 64 | 65 | // If largest is not root 66 | if(largest != i) 67 | { 68 | int temp = Arr[i]; 69 | Arr[i] = Arr[largest]; 70 | Arr[largest] = temp; 71 | heapify(Arr, largest, n); 72 | } 73 | 74 | } 75 | 76 | void printArray(int Arr[], int n) 77 | { 78 | for (int i = 0; i < n ; i++) 79 | { 80 | printf("%d ",Arr[i]); 81 | } 82 | printf("\n"); 83 | } 84 | -------------------------------------------------------------------------------- /initials.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Generate initials of a given name. 3 | * Example: 4 | * Input : John Alison Doe 5 | * Output : JAD 6 | * 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | int main(void) 14 | { 15 | char name[100]; 16 | int i = 0; 17 | 18 | // pretend there's a space before the string 19 | // 20 | char prev = ' ' ; 21 | printf("Enter the name: "); 22 | 23 | // reads a line from the specified stream and stores it into the string 24 | // 25 | fgets(name, sizeof(name), stdin); 26 | 27 | while (name[i]!='\0') 28 | { 29 | if ((name[i] != ' ' || name[i] != '\t')) 30 | { 31 | if ((name[i] >= 'A' && name[i] <='Z') || (name[i] >= 'a' && name[i] <= 'z')) 32 | { 33 | if(prev == ' ') 34 | { 35 | putchar(toupper(name[i])); 36 | } 37 | } 38 | } 39 | prev = name[i]; 40 | i++; 41 | } 42 | putchar('\n'); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /insertion_sort.c: -------------------------------------------------------------------------------- 1 | /* Program to implement Insertion Sort 2 | * The insertion sort algorithm is a simple sorting algorithm which sorts the array by shifting elements one by one. 3 | * 4 | * Example: 5 | * Enter 4 elements of the array: 12 11 13 5 6 | * Sorted array: 7 | * 5 11 12 13 8 | * 9 | * Enter the number of elements in the array: 120 10 | * Please enter value less than 100 11 | * Enter the number of elements in the array: 12 | */ 13 | 14 | #include 15 | #define MAX_SIZE 100 16 | 17 | void insertion_sort(int arr[], int); 18 | void printArray(int arr[], int); 19 | 20 | int main(void) 21 | { 22 | int arr[MAX_SIZE]; 23 | int size_array; 24 | do 25 | { 26 | printf("Enter the number of elements in the array: "); 27 | scanf("%d", &size_array); 28 | if(size_array > MAX_SIZE) 29 | { 30 | printf("Please enter value less than 100\n"); 31 | } 32 | } while(size_array > MAX_SIZE); 33 | 34 | printf("Enter %d elements of the array: ", size_array); 35 | for(int i = 0; i < size_array; i++) 36 | { 37 | scanf("%d", &arr[i]); 38 | } 39 | insertion_sort(arr, size_array); 40 | printf("Sorted array:\n"); 41 | printArray(arr, size_array); 42 | return 0; 43 | } 44 | 45 | void insertion_sort(int arr[], int n) 46 | { 47 | int i, j, key; 48 | for(i = 1; i < n; i++) 49 | { 50 | key = arr[i]; 51 | j = i - 1; 52 | while(j >= 0 && arr[j] > key) 53 | { 54 | arr[j+1] = arr[j]; 55 | j = j - 1; 56 | } 57 | arr[j+1] = key; 58 | } 59 | } 60 | 61 | void printArray(int arr[], int n) 62 | { 63 | for(int i = 0; i < n; i++) 64 | { 65 | printf("%d ", arr[i]); 66 | } 67 | printf("\n"); 68 | } 69 | -------------------------------------------------------------------------------- /linear_search.c: -------------------------------------------------------------------------------- 1 | /* Program to search element of an array using linear search. 2 | * Example: 3 | * Enter the size of the array: 4 4 | * Enter 4 elements of the array: 2 4 1 8 5 | * Enter the element to search: 8 6 | * Element is found at index 3 7 | */ 8 | 9 | #include 10 | #define MAX_SIZE 50 11 | 12 | #define bool int 13 | #define true 1 14 | #define false 0 15 | 16 | int linear_search(int arr[], int, int); 17 | 18 | int main(void) 19 | { 20 | int arr[MAX_SIZE]; 21 | int n, element; 22 | int index; 23 | do 24 | { 25 | printf("Enter the size of the array: "); 26 | scanf("%d", &n); 27 | if(n > MAX_SIZE) 28 | { 29 | printf("Please enter value less than 50\n"); 30 | } 31 | }while(n > MAX_SIZE); 32 | printf("Enter %d elements of the array: ", n); 33 | for(int i = 0; i < n; i++) 34 | { 35 | scanf("%d", &arr[i]); 36 | } 37 | printf("Enter the element to search: "); 38 | scanf("%d",&element); 39 | index = linear_search(arr, n, element); 40 | if(index == -1) 41 | { 42 | printf("Element not found\n"); 43 | } 44 | else 45 | { 46 | printf("Element is found at index %d\n", index); 47 | } 48 | return 0; 49 | } 50 | 51 | int linear_search(int arr[], int n, int element) 52 | { 53 | int i; 54 | for(i = 0; i < n ; i++) 55 | { 56 | if(element == arr[i]) 57 | { 58 | return i; 59 | } 60 | } 61 | return -1; 62 | } 63 | -------------------------------------------------------------------------------- /max_numeric_value.c: -------------------------------------------------------------------------------- 1 | /* Program to extract maximum numeric value from a given alphanumeric string. 2 | * For example: 3 | * Enter a string: 100klh564abc365bg 4 | * Maximum numeric value = 564 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | void remove_newline(char str[]); 12 | long long max_numeric_value(char str[]); 13 | 14 | int main(void) 15 | { 16 | char str[26]; 17 | printf("Enter a string: "); 18 | fgets(str, 26, stdin); 19 | remove_newline(str); 20 | long long maximum = max_numeric_value(str); 21 | printf("Maximum numeric value = %lld\n", maximum); 22 | return 0; 23 | } 24 | 25 | long long max_numeric_value(char str[]) 26 | { 27 | long long num = 0; 28 | long long max = 0; 29 | int len_str = strlen(str); 30 | for(int i = 0; i < len_str; i++) 31 | { 32 | if(str[i] >= '0' && str[i] <= '9') 33 | { 34 | num = num * 10 + (str[i] - '0'); 35 | if(num > max) 36 | { 37 | max = num; 38 | } 39 | } 40 | else 41 | { 42 | num = 0; 43 | } 44 | } 45 | return max; 46 | } 47 | 48 | void remove_newline(char* str) 49 | { 50 | size_t len_str = strlen(str); 51 | len_str--; 52 | if(*str && str[len_str] == '\n') 53 | { 54 | str[len_str] ='\0'; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /merge_sort.c: -------------------------------------------------------------------------------- 1 | /* Program to implement merge sort. 2 | * Merge Sort is a Divide and Conquer algorithm. It divides input array in two halves, 3 | * calls itself for the two halves and then merges the two sorted halves. 4 | * The merge() function is used for merging two halves. 5 | * 6 | * Example: 7 | * Enter the number of elements: 5 8 | * Enter 5 elements of the array: 3 2 1 5 4 9 | * Sorted array: 10 | * 1 2 3 4 5 11 | */ 12 | 13 | #include 14 | #define MAX_SIZE 100 15 | 16 | void mergeSort(int arr[], int , int); 17 | void merge(int arr[], int , int, int); 18 | void printArray(int arr[], int); 19 | 20 | int main(void) 21 | { 22 | int arr[MAX_SIZE]; 23 | int size_array; 24 | do 25 | { 26 | printf("Enter the number of elements: "); 27 | scanf("%d", &size_array); 28 | if(size_array > MAX_SIZE) 29 | { 30 | printf("Please enter value less than %d\n", MAX_SIZE); 31 | } 32 | }while(size_array > MAX_SIZE); 33 | printf("Enter %d elements of the array: ", size_array); 34 | for(int i = 0; i < size_array; i++) 35 | { 36 | scanf("%d",&arr[i]); 37 | } 38 | mergeSort(arr, 0, size_array - 1); 39 | printf("Sorted array:\n"); 40 | printArray(arr, size_array); 41 | return 0; 42 | } 43 | 44 | void mergeSort(int arr[], int l, int r) 45 | { 46 | if(r > l) 47 | { 48 | int mid = l + (r-l)/2; 49 | mergeSort(arr, l, mid); 50 | mergeSort(arr, mid+1, r); 51 | merge(arr, l, mid, r); 52 | } 53 | } 54 | 55 | //l is left index and r is right index 56 | void merge(int arr[], int l, int mid, int r) 57 | { 58 | int i = 0, j = 0, k = 0; 59 | int n1 = mid-l+1; 60 | int n2 = r-mid; 61 | 62 | // create two temporary arrays L and R of size n1 and n2 63 | int L[n1], R[n2]; 64 | 65 | for(i = 0; i < n1; i++) 66 | { 67 | L[i] = arr[l+i]; 68 | } 69 | for(j = 0; j< n2; j++) 70 | { 71 | R[j] = arr[mid+j+1]; 72 | } 73 | 74 | /* merge temp arrays back to arr[l..r] */ 75 | i = 0; // initial index of 1st subarray 76 | j = 0; // initial index of 2nd subarray 77 | k = l; // initial index of merge subarray 78 | 79 | while( i < n1 && j < n2) 80 | { 81 | if(L[i] <= R[j]) 82 | { 83 | arr[k] = L[i]; 84 | i++; 85 | } 86 | else 87 | { 88 | arr[k] = R[j]; 89 | j++; 90 | } 91 | k++; 92 | } 93 | 94 | while(i < n1) 95 | { 96 | arr[k] = L[i]; 97 | i++; 98 | k++; 99 | } 100 | 101 | while(j < n2) 102 | { 103 | arr[k] = R[j]; 104 | j++; 105 | k++; 106 | } 107 | } 108 | 109 | 110 | void printArray(int arr[], int n) 111 | { 112 | for(int i = 0; i < n; i++) 113 | { 114 | printf("%d ",arr[i]); 115 | } 116 | printf("\n"); 117 | } 118 | -------------------------------------------------------------------------------- /merge_trees.c: -------------------------------------------------------------------------------- 1 | /* Given two binary trees and imagine that when you put 2 | * one of them to cover the other, some nodes of the two trees are overlapped while 3 | * the others are not. 4 | * 5 | * You need to merge them into a new binary tree. The merge rule is that if 6 | * two nodes overlap, then sum node values up as the new value of the merged node. 7 | * Otherwise, the NOT null node will be used as the node of new tree. 8 | * 9 | * Input: 10 | Tree 1 Tree 2 11 | 1 2 12 | / \ / \ 13 | 3 2 1 3 14 | / \ \ 15 | 5 4 7 16 | * Output: 17 | * Merged tree: 18 | 3 19 | / \ 20 | 4 5 21 | / \ \ 22 | 5 4 7 23 | * Note: The merging process must start from the root nodes of both trees. 24 | * Output: 25 | * Inorder traversal of the merged tree is: 5 4 4 3 5 7 26 | */ 27 | 28 | #include 29 | #include 30 | 31 | // definition of a Binary tree node 32 | struct TreeNode 33 | { 34 | int val; 35 | struct TreeNode *left; 36 | struct TreeNode *right; 37 | }; 38 | typedef struct TreeNode TreeNode; 39 | 40 | // function to create new node 41 | struct TreeNode* newNode(int data) 42 | { 43 | struct TreeNode* temp = (struct TreeNode*)malloc(sizeof(struct TreeNode)); 44 | temp->val = data; 45 | temp->left = temp->right = NULL; 46 | return temp; 47 | } 48 | 49 | // function to merge trees 50 | struct TreeNode* mergeTrees(struct TreeNode* t1, struct TreeNode* t2) { 51 | if (t1 == NULL) { 52 | return t2; 53 | } 54 | 55 | if (t2 == NULL) { 56 | return t1; 57 | } 58 | t1->val += t2->val; 59 | t1->left = mergeTrees(t1 ? t1->left : NULL, t2 ? t2->left:NULL); 60 | t1->right = mergeTrees(t1 ? t1->right : NULL, t2 ? t2->right : NULL); 61 | return t1; 62 | } 63 | 64 | void inorder(struct TreeNode* root) 65 | { 66 | if(root!= NULL) 67 | { 68 | inorder(root->left); 69 | printf("%d ", root->val); 70 | inorder(root->right); 71 | } 72 | } 73 | 74 | int main() 75 | { 76 | // create 1st Binary Tree 77 | struct TreeNode* root1 = newNode(1); 78 | root1->left = newNode(3); 79 | root1->right = newNode(2); 80 | root1->left->left = newNode(5); 81 | 82 | // create 2nd Binary Tree 83 | struct TreeNode* root2 = newNode(2); 84 | root2->left = newNode(1); 85 | root2->right = newNode(3); 86 | root2->left->right = newNode(4); 87 | root2->right->right = newNode(7); 88 | 89 | struct TreeNode* mergedTree = mergeTrees(root1, root2); 90 | printf("Inorder traversal of the merged tree is: "); 91 | inorder(mergedTree); 92 | printf("\n"); 93 | return 0; 94 | } 95 | -------------------------------------------------------------------------------- /number_complement.c: -------------------------------------------------------------------------------- 1 | /* Given a positive integer, output its complement number. 2 | * The complement strategy is to flip the bits of its binary representation. 3 | * 4 | * Note: 5 | * The given integer is guaranteed to fit within the range of a 32-bit signed integer. 6 | * You could assume no leading zero bit in the integer’s binary representation. 7 | * 8 | * Example 1: 9 | * Input: 5 10 | * Output: 2 11 | * Explanation: The binary representation of 5 is 101 (no leading zero bits), 12 | * and its complement is 010. So you need to output 2. 13 | * 14 | * Example 2: 15 | * Input: 1 16 | * Output: 0 17 | * Explanation: The binary representation of 1 is 1 (no leading zero bits), 18 | * and its complement is 0. So you need to output 0. 19 | */ 20 | 21 | #include 22 | #include 23 | 24 | int findComplement(int); 25 | 26 | int main() 27 | { 28 | int num; 29 | printf("Enter a number:"); 30 | scanf("%d", &num); 31 | int number_complement = findComplement(num); 32 | printf("Number Complement:%d\n", number_complement); 33 | return 0; 34 | } 35 | 36 | int findComplement(int num) 37 | { 38 | return ~num & (1 << ((int)log2(num)+ 1)) - 1; 39 | } 40 | -------------------------------------------------------------------------------- /odd_times.c: -------------------------------------------------------------------------------- 1 | /* Program to find the number Occurring Odd Number of Times in O(n) time & constant space. 2 | * Given an array of positive integers. All numbers occur even number of times 3 | * except one number which occurs odd number of times. 4 | * 5 | * Example: 6 | * Enter the number of elements: 11 7 | * Enter 11 elements of the array: 2 3 5 4 2 4 3 5 2 4 4 8 | * Number occuring odd number of times = 2 9 | */ 10 | 11 | #include 12 | #include 13 | #define MAX_SIZE 30 14 | 15 | int odd_times(int arr[], int); 16 | 17 | int main(void) 18 | { 19 | int arr[MAX_SIZE]; 20 | int size_array, odd_number_times; 21 | do 22 | { 23 | printf("Enter the number of elements: "); 24 | scanf("%d", &size_array); 25 | if(size_array > MAX_SIZE) 26 | { 27 | printf("Please enter value less than %d\n", MAX_SIZE); 28 | } 29 | }while(size_array > MAX_SIZE); 30 | printf("Enter %d elements of the array: ", size_array); 31 | for(int i = 0; i < size_array; i++) 32 | { 33 | scanf("%d",&arr[i]); 34 | } 35 | odd_number_times = odd_times(arr,size_array); 36 | printf("Number occuring odd number of times = %d\n", odd_number_times); 37 | return 0; 38 | } 39 | 40 | int odd_times(int arr[], int len) 41 | { 42 | int i; 43 | int result = 0; 44 | for(i = 0; i < len; i++) 45 | { 46 | result = result ^ arr[i]; 47 | } 48 | return result; 49 | } 50 | -------------------------------------------------------------------------------- /optimized_prime_factors.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Program to find all prime factors of a number less than 1000 using sieve of eratosthenes. 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | void sieve (int *, int); 9 | void printPrimeFactors(int); 10 | 11 | int main() 12 | { 13 | int n; 14 | printf("Enter the value of n: "); 15 | scanf("%d", &n); 16 | printPrimeFactors(n); 17 | return 0; 18 | } 19 | 20 | void printPrimeFactors(int n) 21 | { 22 | if (n >= 1000) 23 | { 24 | printf("Your value is greater than 1000, program works for values less than 1000\n"); 25 | return; 26 | } 27 | 28 | printf("The prime factors of %d are: ", n); 29 | 30 | int arr[1000] = { 0 }; 31 | 32 | // Mark all prime numbers till n 33 | // 34 | sieve(arr, n); 35 | 36 | // Check if any prime is factor of n, if it is, print it. 37 | // 38 | for ( int i = 0; i < n; ++i) 39 | { 40 | if (arr[i] == 0) 41 | { 42 | if (n % i == 0) 43 | { 44 | printf("%d ", i); 45 | } 46 | } 47 | } 48 | 49 | printf("\n"); 50 | } 51 | 52 | void sieve (int *arr, int n) 53 | { 54 | if (n >= 1000) 55 | { 56 | printf("Your value is greater than 1000, program works for values less than 1000\n"); 57 | return; 58 | } 59 | 60 | arr[0] = 1; 61 | arr[1] = 1; 62 | 63 | for (int i = 2; i <= sqrt(n); i++) 64 | { 65 | for (int j = 2; i * j <= n; j++) 66 | { 67 | if (arr[i * j] == 0) 68 | { 69 | arr[i * j] = 1; 70 | } 71 | } 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /palindrome.c: -------------------------------------------------------------------------------- 1 | /* Program to check if a given string is a palindrome. 2 | * Example: 3 | * Enter the string: ALA 4 | * String is a palindrome. 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | int main() 11 | { 12 | char c[20], reverse_string[20] = {'\0'}; 13 | int i, len = 0, flag = 0; 14 | printf("Enter the string: "); 15 | gets(c); 16 | len = strlen(c); 17 | 18 | for(i = len - 1; i >= 0; i--) 19 | { 20 | reverse_string[len - i - 1] = c[i]; 21 | } 22 | 23 | for(i = 0; i < len; i++) 24 | { 25 | if(reverse_string[i] == c[i]) 26 | { 27 | flag = 1; 28 | } 29 | else 30 | { 31 | flag = 0; 32 | } 33 | } 34 | if(flag == 1) 35 | { 36 | printf("String is a palindrome.\n"); 37 | } 38 | else if(flag == 0) 39 | { 40 | printf("String is not a palindrome.\n"); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /power_of_two.c: -------------------------------------------------------------------------------- 1 | /* Program to check if an integer is a power of two. 2 | * Example: 3 | * Enter the number to check if it is power of 2: 64 4 | * Number is a power of 2 5 | */ 6 | 7 | #include 8 | 9 | int isPoweroftwo(int); 10 | 11 | int main(void) 12 | { 13 | int n; 14 | printf("Enter the number to check if it is power of 2: "); 15 | scanf("%d", &n); 16 | int istrue = isPoweroftwo(n); 17 | if(istrue == 1) 18 | { 19 | printf("Number is a power of 2\n"); 20 | } 21 | else 22 | { 23 | printf("Number is not a power of 2\n"); 24 | } 25 | } 26 | 27 | int isPoweroftwo(int n) 28 | { 29 | return((n > 0) && (n & (n-1)) == 0); 30 | } 31 | -------------------------------------------------------------------------------- /prime_factor.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Prime Factorization- Have the user enter a number and find all 3 | * prime factors (if there are any) and display them. 4 | */ 5 | 6 | #include 7 | #include 8 | 9 | #define bool int 10 | #define true 1 11 | #define false 0 12 | 13 | bool isPrime(int); 14 | 15 | int main(void) 16 | { 17 | int i, n; 18 | printf("Enter the value of n : "); 19 | scanf("%d", &n); 20 | printf("Prime factors of %d are ", n); 21 | for (i = 2; i <= n; i++) 22 | { 23 | if(n % i == 0) 24 | { 25 | if(isPrime(i)) 26 | { 27 | printf("%d ", i); 28 | } 29 | } 30 | } 31 | printf("\n"); 32 | } 33 | 34 | bool isPrime(int n) 35 | { 36 | if (n == 2) 37 | { 38 | return true; 39 | } 40 | 41 | if (n < 2 || n % 2 == 0) 42 | { 43 | return false; 44 | } 45 | 46 | for (int i = 2; i <= sqrt(n) ; i++) 47 | { 48 | if(n % i == 0) 49 | { 50 | return false; 51 | } 52 | } 53 | return true; 54 | } -------------------------------------------------------------------------------- /quick_sort.c: -------------------------------------------------------------------------------- 1 | /* Program to implement Quick Sort. 2 | * QuickSort is a Divide and Conquer algorithm. It picks an 3 | * element as pivot and partitions the given array around the picked pivot. 4 | * We have taken laste element as pivot in the function partition. 5 | * The key process in quickSort is partition(). Target of partitions is, 6 | * given an array and an element x of array as pivot, put x at its correct position in sorted array 7 | * and put all smaller elements (smaller than x) before x, and put all greater elements (greater than x) after x. 8 | * 9 | * Example: 10 | * Enter 5 elements of the array: 10 30 80 20 70 11 | * Sorted array: 12 | * 10 20 30 70 80 13 | */ 14 | 15 | #include 16 | #define MAX_SIZE 100 17 | 18 | int partition(int arr[], int, int); 19 | void QuickSort(int arr[], int, int); 20 | void swap(int *, int *); 21 | void printArray(int arr[], int); 22 | 23 | int main(void) 24 | { 25 | int arr[MAX_SIZE]; 26 | int size_array; 27 | do 28 | { 29 | printf("Enter the number of elements: "); 30 | scanf("%d", &size_array); 31 | if(size_array > MAX_SIZE) 32 | { 33 | printf("Please enter value less than %d\n", MAX_SIZE); 34 | } 35 | }while(size_array > MAX_SIZE); 36 | printf("Enter %d elements of the array: ", size_array); 37 | for(int i = 0; i < size_array; i++) 38 | { 39 | scanf("%d",&arr[i]); 40 | } 41 | QuickSort(arr, 0, size_array - 1); 42 | printf("Sorted array:\n"); 43 | printArray(arr, size_array); 44 | return 0; 45 | } 46 | 47 | void QuickSort(int arr[], int left_index, int right_index) 48 | { 49 | int pi; 50 | if(right_index > left_index) 51 | { 52 | // pi is partitioning index 53 | pi = partition(arr, left_index, right_index); 54 | QuickSort(arr, left_index, pi-1); // before pi 55 | QuickSort(arr, pi+1, right_index); // After pi 56 | } 57 | } 58 | 59 | /* This function takes last element as pivot, places the pivot element at its correct position in sorted 60 | array, and places all elements smaller than pivot to the left of pivot and all elements greater than 61 | pivot to the right of pivot */ 62 | int partition(int arr[], int low, int high) 63 | { 64 | int i,j; 65 | int pivot = arr[high]; 66 | i = low-1; // index of smaller element 67 | for(j = low; j <= high-1; j++) 68 | { 69 | if(arr[j] <= pivot) 70 | { 71 | i++; // increment index of smaller element 72 | swap(&arr[i], &arr[j]); 73 | } 74 | } 75 | swap(&arr[i+1], &arr[high]); 76 | return i+1; 77 | } 78 | 79 | // function to swap two values 80 | void swap(int *a, int *b) 81 | { 82 | int temp; 83 | temp = *a; 84 | *a = *b; 85 | *b = temp; 86 | } 87 | 88 | void printArray(int arr[], int n) 89 | { 90 | for(int i = 0; i < n; i++) 91 | { 92 | printf("%d ",arr[i]); 93 | } 94 | printf("\n"); 95 | } 96 | -------------------------------------------------------------------------------- /reverse_string.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Program to print the reverse of a string 3 | */ 4 | 5 | #include 6 | 7 | int main(void) 8 | { 9 | char str[20]; 10 | char reverse_string[20] = {'\0'}; 11 | unsigned int len = 0; 12 | printf("Enter the string: "); 13 | fgets(str, 20, stdin); 14 | int i; 15 | for (i = 0; i < str[i] != 0; i++) 16 | { 17 | len++; 18 | } 19 | printf("Reverse of the string is "); 20 | for(i = len - 1; i >= 0; i--) 21 | { 22 | reverse_string[len - i - 1] = str[i]; 23 | printf("%c", reverse_string[len-i-1]); 24 | } 25 | printf("\n"); 26 | } 27 | -------------------------------------------------------------------------------- /selection_sort.c: -------------------------------------------------------------------------------- 1 | /* Program to implement Selection Sort 2 | * The selection sort algorithm sorts an array by repeatedly 3 | * finding the minimum element (considering ascending order) from 4 | * unsorted part and putting it at the beginning. 5 | * 6 | * Example: 7 | * Enter 4 elements of the array: 3 2 1 6 8 | * Sorted array: 9 | * 1 2 3 6 10 | * 11 | * Enter the number of elements in the array: 101 12 | * Please enter value less than 100 13 | * Enter the number of elements in the array: 14 | */ 15 | 16 | #include 17 | #define MAX_SIZE 100 18 | 19 | void swap(int *a, int *b); 20 | void selection_sort(int arr[], int); 21 | void printArray(int arr[], int); 22 | 23 | int main(void) 24 | { 25 | int arr[MAX_SIZE]; 26 | int size_array; 27 | do 28 | { 29 | printf("Enter the number of elements in the array: "); 30 | scanf("%d", &size_array); 31 | if(size_array > MAX_SIZE) 32 | { 33 | printf("Please enter value less than 100\n"); 34 | } 35 | } while(size_array > MAX_SIZE); 36 | 37 | printf("Enter %d elements of the array: ", size_array); 38 | for(int i = 0; i < size_array; i++) 39 | { 40 | scanf("%d", &arr[i]); 41 | } 42 | selection_sort(arr, size_array); 43 | printf("Sorted array:\n"); 44 | printArray(arr, size_array); 45 | return 0; 46 | } 47 | 48 | void selection_sort(int arr[], int n) 49 | { 50 | int i,j , min_index; 51 | for(i = 0; i < n-1; i++) 52 | { 53 | min_index = i; 54 | for(j = i+1; j < n; j++) 55 | { 56 | if(arr[j] < arr[min_index]) 57 | { 58 | min_index = j; 59 | } 60 | } 61 | swap(&arr[min_index], &arr[i]); 62 | } 63 | } 64 | 65 | void swap(int *a, int *b) 66 | { 67 | int temp = *a; 68 | *a = *b; 69 | *b = temp; 70 | } 71 | 72 | void printArray(int arr[], int n) 73 | { 74 | for(int i = 0; i < n; i++) 75 | { 76 | printf("%d ", arr[i]); 77 | } 78 | printf("\n"); 79 | } 80 | -------------------------------------------------------------------------------- /sieve_of_eratosthenes.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Program to implement Sieve of Eratosthenes problem - Have the user enter any number till 1000 3 | * and the display all prime numbers up to the number entered by the user. 4 | */ 5 | 6 | #include 7 | #include 8 | 9 | void sieve(int n); 10 | 11 | int main(void) 12 | { 13 | int n; 14 | printf("Enter the value of n: "); 15 | scanf("%d", &n); 16 | printf("Prime numbers till %d are ", n); 17 | sieve(n); 18 | return 0; 19 | } 20 | 21 | void sieve (int n) 22 | { 23 | if (n >= 1000) 24 | { 25 | printf("Please enter a value less than 1000.\n"); 26 | return; 27 | } 28 | int arr[1000] = {0}; 29 | arr[0] = 1; 30 | arr[1] = 1; 31 | for (int i = 2; i <= sqrt(n); i++) 32 | { 33 | for (int j = 2; i * j <= n; j++) 34 | { 35 | arr[i * j] = 1; 36 | } 37 | } 38 | for (int i = 0; i < n; i++) 39 | { 40 | if (arr[i] == 0) 41 | { 42 | printf("%d ", i); 43 | } 44 | } 45 | printf("\n"); 46 | } 47 | 48 | 49 | -------------------------------------------------------------------------------- /smallest_missing.c: -------------------------------------------------------------------------------- 1 | /* Program to find smallest missing element in a sorted array of distinct non-negative integers. 2 | * Example 3 | * Enter number of elements: 5 4 | * Enter 5 elements of the array: 0 1 2 3 6 5 | * Smallest missing element in the sorted array: 4 6 | */ 7 | 8 | #include 9 | #define MAX_SIZE 20 10 | 11 | int smallestMissing(int arr[], int, int); 12 | 13 | int main(void) 14 | { 15 | int arr[MAX_SIZE]; 16 | int n, low, high; 17 | do 18 | { 19 | printf("Enter number of elements: "); 20 | scanf("%d", &n); 21 | if(n > MAX_SIZE) 22 | { 23 | printf("Please enter value less than %d\n", MAX_SIZE); 24 | } 25 | }while(n > MAX_SIZE); 26 | printf("Enter %d elements of the array: ", n); 27 | for(int i = 0; i < n-1; i++) 28 | { 29 | scanf("%d", &arr[i]); 30 | } 31 | low = 0; 32 | high = n-1; 33 | int missing_element = smallestMissing(arr, low, high); 34 | printf("Smallest missing element in the sorted array: %d\n", missing_element); 35 | return 0; 36 | } 37 | 38 | // Function to find smallest missing element in sorted array 39 | int smallestMissing(int arr[], int low, int high) 40 | { 41 | // low is for leftmost index and high is for rightmost index 42 | if(low > high) 43 | { 44 | return low; 45 | } 46 | int mid = low + (high - low)/2; 47 | 48 | // if mid index matches the mid element, then mismatch lies on the right half of the array 49 | if(arr[mid] == mid) 50 | { 51 | return smallestMissing(arr, mid+1, high); 52 | } 53 | else 54 | { 55 | // mismatch lies on the left half 56 | return smallestMissing(arr, low, mid-1); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /star_pattern.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Program to print the below star pattern of a specified height. 3 | 4 | * 5 | * * 6 | * * * 7 | * * * * 8 | * * * * * 9 | 10 | */ 11 | 12 | #include 13 | 14 | int main(void) 15 | { 16 | int n = 0; 17 | do{ 18 | printf(" Enter the height of star pattern: "); 19 | scanf("%d", &n); 20 | } while(n < 0 || n > 23); 21 | 22 | for (int i = 1; i <= n; i++) 23 | { 24 | for (int j = 1; j <= i; j++) 25 | { 26 | printf("* "); 27 | } 28 | printf("\n"); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /string_operations.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Program to perform string operations copy and compare without using string functions. 3 | * Example: 4 | * 1.Copy String 5 | * 2.Compare String 6 | * 3.Quit 7 | * 8 | * Enter your choice:1 9 | * Enter a string: My name is Derek 10 | * Copied string is: My name is Derek 11 | * 12 | * Enter your choice:2 13 | * Enter 1st string: Fish 14 | * Enter 2nd string: Food 15 | * Compare result from program is -6 16 | * String function strcmp result is -6 17 | * String2 > String1 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | 24 | void copy(char str2[], char str1[]); 25 | int compare(char str1[], char str2[]); 26 | int smaller(int, int); 27 | void remove_newline(char str[]); 28 | size_t string_length(char str[]); 29 | 30 | int main(void) 31 | { 32 | int option, result; 33 | char str1[100], str2[100]; 34 | printf("1.Copy String\n"); 35 | printf("2.Compare String\n"); 36 | printf("3.Quit\n"); 37 | printf("Enter your choice:"); 38 | scanf("%d", &option); 39 | int c; 40 | while((c = getchar()) != '\n' && c != EOF); 41 | switch(option) 42 | { 43 | case 1: 44 | printf("Enter a string: "); 45 | fgets(str1, 100 , stdin); 46 | copy(str2, str1); 47 | printf("Copied string is: %s",str2); 48 | break; 49 | 50 | case 2: 51 | printf("Enter 1st string: "); 52 | fgets(str1,100, stdin); 53 | remove_newline(str1); 54 | 55 | printf("Enter 2nd string: "); 56 | fgets(str2, 100, stdin); 57 | remove_newline(str2); 58 | 59 | result = compare(str1, str2); 60 | printf("Compare result from program is %d\n", result); 61 | printf("String function strcmp result is %d\n", strcmp(str1,str2)); 62 | if(result > 0) 63 | { 64 | printf("String1 > String2\n"); 65 | } 66 | else if(result < 0) 67 | { 68 | printf("String2 > String1\n"); 69 | } 70 | else if(result == 0) 71 | { 72 | printf("String1 and String2 are same\n"); 73 | } 74 | break; 75 | 76 | default: 77 | printf("Invalid choice\n"); 78 | break; 79 | } 80 | } 81 | 82 | void copy(char str2[], char str1[]) 83 | { 84 | int i = 0; 85 | while(str1[i] != '\0') 86 | { 87 | str2[i] = str1[i]; 88 | i++; 89 | } 90 | str2[i] = '\0'; 91 | } 92 | 93 | int compare(char str1[], char str2[]) 94 | { 95 | size_t i = 0; 96 | size_t len_str1 = string_length(str1); 97 | size_t len_str2 = string_length(str2); 98 | size_t smallerLength = smaller(len_str1, len_str2); 99 | 100 | char * smallerstring; 101 | if(len_str1 == smallerLength) 102 | { 103 | smallerstring = str1; 104 | } 105 | else 106 | { 107 | smallerstring = str2; 108 | } 109 | i = 0; 110 | while(smallerstring[i] != '\0') 111 | { 112 | if(str1[i] != str2[i]) 113 | { 114 | return str1[i] - str2[i]; 115 | } 116 | i++; 117 | } 118 | if (i < len_str1) 119 | { 120 | return str1[i]; 121 | } 122 | else if (i < len_str2) 123 | { 124 | return -str2[i]; 125 | } 126 | return 0; 127 | } 128 | 129 | int smaller(int len_str1, int len_str2) 130 | { 131 | return len_str1 < len_str2 ? len_str1 : len_str2; 132 | } 133 | 134 | size_t string_length(char * str) 135 | { 136 | size_t len_str = 0; 137 | for (size_t i = 0; str[i] != '\0'; i++) 138 | { 139 | len_str++; 140 | } 141 | return len_str; 142 | } 143 | 144 | void remove_newline(char* str) 145 | { 146 | size_t len_str = string_length(str); 147 | len_str--; 148 | if (*str && str[len_str] == '\n') 149 | str[len_str] = '\0'; 150 | } 151 | -------------------------------------------------------------------------------- /triplet.c: -------------------------------------------------------------------------------- 1 | /* Program to find if triplet exist for a given unsorted array with given sum 2 | * Example: 3 | * Enter the number of elements: 4 4 | * Enter 4 elements of the array: 2 7 4 0 5 | * Enter the sum to find for the triplet: 6 6 | * Triplet Exists 7 | */ 8 | 9 | #include 10 | #define MAX_SIZE 50 11 | 12 | #define bool int 13 | #define true 1 14 | #define false 0 15 | 16 | bool tripletExists(int arr[], int, int , int); 17 | 18 | int main(void) 19 | { 20 | int arr[MAX_SIZE]; 21 | int size_array, sum; 22 | int count = 0; 23 | do 24 | { 25 | printf("Enter the number of elements: "); 26 | scanf("%d", &size_array); 27 | if(size_array > MAX_SIZE) 28 | { 29 | printf("Please enter value less than %d", MAX_SIZE); 30 | } 31 | }while(size_array > MAX_SIZE); 32 | printf("Enter %d elements of the array: ", size_array); 33 | for(int i = 0; i < size_array; i++) 34 | { 35 | scanf("%d",&arr[i]); 36 | } 37 | printf("Enter the sum to find for the triplet: "); 38 | scanf("%d", &sum); 39 | tripletExists(arr, size_array, sum, count)? printf("Triplet Exists\n") : printf("Triplet Don't Exist\n"); 40 | return 0; 41 | } 42 | 43 | // Recursive function to check if triplet exists in array with given sum 44 | bool tripletExists(int arr[], int n, int sum, int count) 45 | { 46 | // return true if triplet has provided sum 47 | if(count == 3 && sum == 0) 48 | { 49 | return true; 50 | } 51 | 52 | // return false if sum is not possible with current configuration 53 | if(count == 3 || n == 0 || sum < 0) 54 | { 55 | return false; 56 | } 57 | 58 | // Recursion with 59 | //1. Including current element 60 | //2. Excluding current element 61 | return tripletExists(arr, n - 1, sum - arr[n - 1], count + 1) || 62 | tripletExists(arr, n - 1, sum, count); 63 | } 64 | -------------------------------------------------------------------------------- /unit_converter.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Program to convert various units(temperature, currency and weight) from one type to other. 3 | * The user enters the type of unit, the type of unit they want to convert 4 | * to and then the value. The program will then make the conversion. 5 | * 6 | * Example: 7 | * 1. Temperature 8 | * 2. Currency 9 | * 3. Weight 10 | * Enter the unit for conversion: 1 11 | * 1. Celsius to Fahrenheit 12 | * 2. Fahrenheit to Celsius 13 | * Enter the type of conversion you wish to do: 2 14 | * Enter the temperature in Fahrenheit: 200 15 | * 200.000000 degree celsius to degree fahrenheit is 93.3 16 | */ 17 | 18 | #include 19 | #include 20 | 21 | void convert_temp(void); 22 | void convert_currency(void); 23 | void convert_weight(void); 24 | void celsius_to_fahrenheit(float); 25 | void fahrenheit_to_celsius(float); 26 | void indian_rupee_to_usd(float); 27 | void usd_to_indian_rupee(float); 28 | void kilogram_to_pound(float); 29 | void pound_to_kilogram(float); 30 | 31 | int main(void) 32 | { 33 | int unit; 34 | printf("1. Temperature\n"); 35 | printf("2. Currency\n"); 36 | printf("3. Weight\n"); 37 | printf("Enter the unit for conversion: "); 38 | scanf("%d",&unit); 39 | switch(unit) 40 | { 41 | case 1: 42 | convert_temp(); 43 | break; 44 | case 2: 45 | convert_currency(); 46 | break; 47 | case 3: 48 | convert_weight(); 49 | break; 50 | default: 51 | printf("Invalid option"); 52 | } 53 | return 0; 54 | } 55 | 56 | void convert_temp(void) 57 | { 58 | int index; 59 | float celsius, fahrenheit; 60 | printf("1. Celsius to Fahrenheit\n"); 61 | printf("2. Fahrenheit to Celsius\n"); 62 | printf("Enter the type of conversion you wish to do: "); 63 | scanf("%d", &index); 64 | switch(index) 65 | { 66 | case 1: 67 | printf("Enter the temperature in Celsius: "); 68 | scanf("%f", &celsius); 69 | celsius_to_fahrenheit(celsius); 70 | break; 71 | case 2: 72 | printf("Enter the temperature in Fahrenheit: "); 73 | scanf("%f", &fahrenheit); 74 | fahrenheit_to_celsius(fahrenheit); 75 | break; 76 | } 77 | } 78 | 79 | // function to convert degree celsius to degree fahrenheit 80 | // 81 | void celsius_to_fahrenheit(float celsius) 82 | { 83 | float fahren; 84 | fahren = (celsius * 9/5) + 32; 85 | printf("%f degree celsius to degree fahrenheit is %0.1f\n", celsius, fahren); 86 | } 87 | 88 | // function to convert degree fahrenheit to degree celsius 89 | // 90 | void fahrenheit_to_celsius(float fahrenheit) 91 | { 92 | float celsius; 93 | celsius = (fahrenheit - 32) * 5/9; 94 | printf("%f degree celsius to degree fahrenheit is %0.1f\n", fahrenheit, celsius); 95 | } 96 | 97 | void convert_currency(void) 98 | { 99 | int index; 100 | float inr, usd; 101 | printf("1. Indian Rupee to US Dollar\n"); 102 | printf("2. US Dollar to Indian Rupee\n"); 103 | printf("Enter the type of conversion you wish to do: "); 104 | scanf("%d", &index); 105 | switch(index) 106 | { 107 | case 1: 108 | printf("Enter the value in Indian rupee: "); 109 | scanf("%f", &inr); 110 | indian_rupee_to_usd(inr); 111 | break; 112 | case 2: 113 | printf("Enter the value in US dollar: "); 114 | scanf("%f", &usd); 115 | usd_to_indian_rupee(usd); 116 | break; 117 | } 118 | } 119 | 120 | // function to convert Indian Rupee to US dollar 121 | // 122 | void indian_rupee_to_usd(float inr) 123 | { 124 | float usd; 125 | usd = inr * 0.015; 126 | printf("%f Indian rupee is %0.1f US dollar\n", inr, usd); 127 | } 128 | 129 | // function to convert US dollar to Indian Rupee 130 | // 131 | void usd_to_indian_rupee(float usd) 132 | { 133 | float inr; 134 | inr = usd * 64.56; 135 | printf("%f US dollar is %0.1f Indian rupee\n", usd, inr); 136 | } 137 | 138 | void convert_weight(void) 139 | { 140 | int index; 141 | float kg, pound; 142 | printf("1. Kilogram to Pound\n"); 143 | printf("2. Pound to Kilogram\n"); 144 | printf("Enter the type of conversion you wish to do: "); 145 | scanf("%d", &index); 146 | switch(index) 147 | { 148 | case 1: 149 | printf("Enter the weight in kilogram: "); 150 | scanf("%f", &kg); 151 | kilogram_to_pound(kg); 152 | break; 153 | case 2: 154 | printf("Enter the weight in pound: "); 155 | scanf("%f", £); 156 | pound_to_kilogram(pound); 157 | break; 158 | } 159 | } 160 | 161 | // function to convert kilogram to pound 162 | // 163 | void kilogram_to_pound(float kg) 164 | { 165 | float pound; 166 | pound = kg * 2.2046; 167 | printf("%f kilogram is %0.1f pound\n", kg, pound); 168 | } 169 | 170 | // function to convert pound to kilogram 171 | // 172 | void pound_to_kilogram(float pound) 173 | { 174 | float kg; 175 | kg = pound * 0.453592; 176 | printf("%f pound is %0.1f kilogram\n", pound, kg); 177 | } 178 | -------------------------------------------------------------------------------- /unsorted_subarray.c: -------------------------------------------------------------------------------- 1 | /* Program to find the minimum length subarray arr[s..e] such that sorting this subarray makes the whole array sorted. 2 | * Example: 3 | * Enter the number of elements: 11 4 | * Enter 11 elements of the array: 10 12 20 30 25 40 32 31 35 50 60 5 | * The unsorted subarray which makes the given array sorted lies between the indices 3 and 8 6 | */ 7 | 8 | #include 9 | #define MAX_SIZE 20 10 | 11 | void unsorted_subarray(int arr[], int); 12 | 13 | int main(void) 14 | { 15 | int arr[MAX_SIZE]; 16 | int size_array; 17 | do 18 | { 19 | printf("Enter the number of elements: "); 20 | scanf("%d", &size_array); 21 | if(size_array > MAX_SIZE) 22 | { 23 | printf("Please enter values less than %d", MAX_SIZE); 24 | } 25 | }while(size_array > MAX_SIZE); 26 | printf("Enter %d elements of the array: ", size_array); 27 | for(int i = 0; i < size_array; i++) 28 | { 29 | scanf("%d", &arr[i]); 30 | } 31 | unsorted_subarray(arr, size_array); 32 | return 0; 33 | } 34 | 35 | void unsorted_subarray(int arr[], int n) 36 | { 37 | int s = 0; 38 | int e = n-1; 39 | int i , max, min; 40 | 41 | // find the first element which is greater than the next element by scanning from left to right order. 42 | for (s = 0; s < n - 1; s++) 43 | { 44 | if(arr[s] > arr[s+1]) 45 | { 46 | break; 47 | } 48 | } 49 | if(s == n - 1) 50 | { 51 | printf("The complete array is sorted\n"); 52 | return; 53 | } 54 | 55 | // find the first element which is smaller than the next element by scanning from right to left order. 56 | for(e = n-1; e > 0; e--) 57 | { 58 | if(arr[e] < arr[e-1]) 59 | { 60 | break; 61 | } 62 | } 63 | 64 | max = arr[s]; 65 | min = arr[s]; 66 | 67 | // find out the maximum and minimum element in the array arr[s..e] 68 | for (i = s + 1; i <= e; i++) 69 | { 70 | if(arr[i] < min) 71 | { 72 | min = arr[i]; 73 | } 74 | if(arr[i] > max) 75 | { 76 | max = arr[i]; 77 | } 78 | } 79 | 80 | // find the first element in array [0...s-1] which is greater than min, if found change s to index of this element 81 | for(i = 0; i < s; i++) 82 | { 83 | if(arr[i] > min) 84 | { 85 | s = i; 86 | break; 87 | } 88 | } 89 | 90 | // find the last element in array [e+1..n-1] which is smaller than the max, if found change e to index of this element 91 | for(i = e+1; i < n; i++) 92 | { 93 | if(arr[i] < max) 94 | { 95 | e = i; 96 | break; 97 | } 98 | } 99 | 100 | // prints the index of the unsorted subarray 101 | printf("The unsorted subarray which makes the given array " 102 | " sorted lies between the indices %d and %d\n", s, e); 103 | } 104 | -------------------------------------------------------------------------------- /vigenere_cipher.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Program to encrypt text using Vigenère’s cipher, per the below. 3 | * Example: 4 | * $ ./vigenere ABC 5 | * plaintext: HELLO 6 | * ciphertext: HFNLP 7 | * 8 | * For more Information: https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher 9 | * 10 | * Example: 11 | * $ ./vigenere bacon 12 | * plaintext: Meet me at the park at eleven am 13 | * ciphertext: Negh zf av huf pcfx bt gzrwep oz 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #define bool int 22 | #define true 1 23 | #define false 0 24 | 25 | bool is_key_valid(char * key) 26 | { 27 | unsigned int len = strlen(key); 28 | for (unsigned int i = 0; i < len; i++) 29 | { 30 | if ((key[i] < 'a' || key[i] > 'z') && (key[i] < 'A' || key[i] > 'Z')) 31 | { 32 | return false; 33 | } 34 | } 35 | return true; 36 | } 37 | 38 | int main(int argc, char *argv[]) 39 | { 40 | if (argc != 2 || (argc == 2 && !is_key_valid(argv[1]))) 41 | { 42 | printf("Usage: ./vigenere k\n"); 43 | return 1; 44 | } 45 | char plain_text[100]; 46 | char *key = argv[1]; 47 | printf("Plaintext: "); 48 | 49 | // reads a line from the specified stream and stores it into the string 50 | // 51 | fgets(plain_text, sizeof(plain_text), stdin); 52 | printf("Ciphertext: "); 53 | int n = strlen(plain_text); 54 | int key_len = strlen(key); 55 | int k1; 56 | int j = 0; 57 | for (int i = 0; i < n; i++) 58 | { 59 | if((plain_text[i] >='A' && plain_text[i] <= 'Z') || (plain_text[i] >='a' && plain_text[i] <= 'z')) 60 | { 61 | int key_index = (j % key_len); 62 | if((key[key_index] >= 'A' && key[key_index] <= 'Z')) 63 | { 64 | k1 = key[key_index] - 'A'; 65 | } 66 | else if (key[key_index] >= 'a' && key[key_index] <= 'z') 67 | { 68 | k1 = key[key_index] - 'a'; 69 | } 70 | ++j; 71 | } 72 | if(plain_text[i] >='A' && plain_text[i] <= 'Z') 73 | { 74 | int p = plain_text[i] - 'A'; 75 | int c = (p + k1) % 26; 76 | plain_text[i] = c + 'A'; 77 | } 78 | else if(plain_text[i] >='a' && plain_text[i] <= 'z') 79 | { 80 | int p = plain_text[i] - 'a'; 81 | int c = (p + k1) % 26; 82 | plain_text[i] = c + 'a'; 83 | } 84 | printf("%c",plain_text[i]); 85 | } 86 | } 87 | --------------------------------------------------------------------------------