├── 2D_Array_task1.2.c ├── Age_Calculator.c ├── ArmstrongNumber.c ├── Array_Sorting.c ├── Assigning_if.c ├── BST.c ├── BankersAlgorithm.c ├── Basic_Calculator.c ├── BearBro.c ├── BinarySearch.c ├── Binary_Sort ├── Binary.c └── README.md ├── Caesar-Cipher.c ├── Celcius to Fehrenheit.c ├── Celebrity_Problem.cpp ├── CompoundInterest.c ├── Concatination.c ├── Dijikstra’s_algorithm.c ├── Directed_Graph.c ├── Disarium.c ├── Duplicate_indexArr.c ├── EvenOdd_dowhile.c ├── Exponents.c ├── Factorial.c ├── Fibonacci_Recursion.cpp ├── Fibonacci_Series.c ├── Geometric.c ├── Grades.c ├── GraphColoring.c ├── HeronsFormula.c ├── Insertion&Searching_BST.c ├── Insertion_sort.c ├── Interestingpatter.c ├── Kadane's_Algorithm.c ├── KruskalAlgorithm.c ├── LCS.c ├── LinearSearch.c ├── Link List DS.c ├── Linked_List.c ├── MagicNumber.c ├── Matrix Multiplication.c ├── Matrix_Chain_Multiplication.c ├── Matrix_Creation.c ├── Minimum_jumps.cpp ├── Pascal_Triangle.C ├── Program Files ├── :small_or_capital_letter.c ├── README.md └── number_of_digits.c ├── Quadratic.c ├── Queue DS.c ├── Queue.c ├── README.md ├── ReportCardGeneration.c ├── ReverseInt.c ├── ReverseLinkedList.c ├── ShortestJobFirstScheduling.c ├── SpacedArrays.c ├── Sphere_in_tetrahadron_problem.c ├── Stack DS.c ├── SwitchCases.c ├── TicTacToe.c ├── Tower_of_Hanoi.c ├── Travelling_SalesmanProblem.c ├── TriangleFibonacci.c ├── Tribonacci.c ├── UniqueElements.c ├── VowelConsonentCheck.c ├── Window shutdown in C .C ├── area_of_triangle.c ├── areaoftriangle.c ├── arfunction.c ├── array_2d_sort.c ├── array_2d_sum.c ├── array_2d_transposition.c ├── bankers_algorithm.c ├── bubble_sort.c ├── checlArrow └── checkArrow.c ├── coinsCS50.c ├── conditionals.c ├── date.c ├── deathyear.c ├── decimal_to_binary.c ├── duplicateElement.c ├── duplicateIndex.c ├── expressions.c ├── factorial.c ├── factorial_recursion.c ├── fcfs.c ├── gcd.c ├── harmonic_sum.c ├── heap_sort.c ├── helloworld.c ├── joinString.c ├── krishnamurthy.c ├── kth_largest_element.c ├── largestnum.c ├── lcm_of_two.c ├── left shift.c ├── linkedlist.c ├── linkedlist.exe ├── luckyNumber.c ├── matrix_multiplication.c ├── mergeSort.c ├── merge_sort.c ├── missing element.c ├── most_frequent_character.c ├── n_queens.c ├── no_main.c ├── no_semicolon.c ├── oddpairs.c ├── ordinal.c ├── palindrome.c ├── patterns ├── Diamond.c ├── Diamond_Hollow.c ├── Pattern_EQI_T_1.c ├── Pattern_EQI_T_2.c ├── Pattern_EQI_T_3.c ├── Pattern_EQI_T_4.c ├── Pattern_EQI_T_with_Space_5.c ├── Pattern_EQI_T_with_Space_6.c ├── Pattern_EQI_T_with_Space_7.c └── Pattern_EQI_T_with_Space_8.c ├── post increment.c ├── precedence.c ├── priority.c ├── producerconsumer.c ├── product.c ├── quick_sort.c ├── right shift.c ├── roundrobin.c ├── searching.c ├── selection_sort.c ├── snake.c ├── sorting.c ├── sorting1.c ├── sourceocode.c ├── sqrt.c ├── stack.c ├── stackUsingLinkedList.c ├── star.c ├── string.c ├── sudokuSolver.c ├── sum of digit.c ├── swap.c ├── time-in.c ├── time.c └── vowel.c /2D_Array_task1.2.c: -------------------------------------------------------------------------------- 1 | #include 2 | void ReadMatrix(int,int,int mat[][10]); 3 | void FindMaxSum(int,int,int mat[][10]); 4 | int main() 5 | { 6 | int rows,columns; 7 | printf("Enter the row: "); 8 | scanf("%d",&rows); 9 | printf("Enter the column: "); 10 | scanf("%d",&columns); 11 | int mat[10][10]; 12 | ReadMatrix(rows,columns,mat); 13 | FindMaxSum(rows,columns,mat); 14 | return(0); 15 | } 16 | 17 | void ReadMatrix(int rows,int columns,int mat[10][10]) 18 | { 19 | for(int i=0;imaxSum) 47 | { 48 | maxSum=sum; 49 | } 50 | index=i+1; 51 | } 52 | printf("\nMax sum is %d at row %d",maxSum,index); 53 | } 54 | -------------------------------------------------------------------------------- /Age_Calculator.c: -------------------------------------------------------------------------------- 1 | // C program to calculate age or Age Calculator 2 | #include 3 | #include 4 | 5 | /*check given year is leap year or not*/ 6 | int isLeapYear(int year, int mon) 7 | { 8 | int flag = 0; 9 | if (year % 100 == 0) 10 | { 11 | if (year % 400 == 0) 12 | { 13 | if (mon == 2) 14 | { 15 | flag = 1; 16 | } 17 | } 18 | } 19 | else if (year % 4 == 0) 20 | { 21 | if (mon == 2) 22 | { 23 | flag = 1; 24 | } 25 | } 26 | return (flag); 27 | } 28 | 29 | 30 | int main() 31 | { 32 | 33 | int DaysInMon[] = {31, 28, 31, 30, 31, 30, 34 | 31, 31, 30, 31, 30, 31}; 35 | int days, month, year; 36 | char dob[100]; 37 | time_t ts; 38 | struct tm *ct; 39 | 40 | /* enter date of birth */ 41 | printf("Enter your date of birth (DD/MM/YYYY): "); 42 | scanf("%d/%d/%d",&days,&month, &year); 43 | 44 | /*get current date.*/ 45 | ts = time(NULL); 46 | ct = localtime(&ts); 47 | 48 | printf("Current Date: %d/%d/%d\n", 49 | ct->tm_mday, ct->tm_mon + 1, ct->tm_year + 1900); 50 | 51 | days = DaysInMon[month - 1] - days + 1; 52 | 53 | /* leap year checking*/ 54 | if (isLeapYear(year, month)) 55 | { 56 | days = days + 1; 57 | } 58 | 59 | /* calculating age in no of days, years and months */ 60 | days = days + ct->tm_mday; 61 | month = (12 - month) + (ct->tm_mon); 62 | year = (ct->tm_year + 1900) - year - 1; 63 | 64 | /* checking for leap year feb - 29 days */ 65 | if (isLeapYear((ct->tm_year + 1900), (ct->tm_mon + 1))) 66 | { 67 | if (days >= (DaysInMon[ct->tm_mon] + 1)) 68 | { 69 | days = days - (DaysInMon[ct->tm_mon] + 1); 70 | month = month + 1; 71 | } 72 | } 73 | else if (days >= DaysInMon[ct->tm_mon]) 74 | { 75 | days = days - (DaysInMon[ct->tm_mon]); 76 | month = month + 1; 77 | } 78 | 79 | if (month >= 12) 80 | { 81 | year = year + 1; 82 | month = month - 12; 83 | } 84 | 85 | /* print age */ 86 | printf("\n## Hey you are %d years %d months and %d days old. ##\n", year, month, days); 87 | 88 | return 0; 89 | } 90 | /* 91 | Output 92 | Enter your date of birth (DD/MM/YYYY): 20/06/2002 93 | Current Date: 19/10/2022 94 | 95 | ## Hey you are 20 years 3 months and 30 days old. ## 96 | */ 97 | -------------------------------------------------------------------------------- /ArmstrongNumber.c: -------------------------------------------------------------------------------- 1 | /*Armstrong number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers. 2 | Take an example of 153 3 | sum of individual digit's cube is equal to 153. 4 | */ 5 | #include 6 | int main() 7 | { 8 | int n,rem,sum=0,temp; 9 | printf("Enter a number to cheack for Armstrong Number = "); 10 | scanf("%d",&n); 11 | temp=n; 12 | while(n>0) 13 | { 14 | r=n%10; 15 | sum=sum+(r*r*r); 16 | n=n/10; 17 | } 18 | if(temp==sum) 19 | printf("\n%d is an Armstrong Number.",temp); 20 | else 21 | printf("\n%d is not an Armstrong Number.",temp); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Array_Sorting.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | //Initialize array 6 | int arr[] = {5, 2, 8, 7, 1}; 7 | int temp = 0; 8 | 9 | //Calculate length of array arr 10 | int length = sizeof(arr)/sizeof(arr[0]); 11 | 12 | //Displaying elements of original array 13 | printf("Elements of original array: \n"); 14 | for (int i = 0; i < length; i++) { 15 | printf("%d ", arr[i]); 16 | } 17 | 18 | //Sort the array in ascending order 19 | for (int i = 0; i < length; i++) { 20 | for (int j = i+1; j < length; j++) { 21 | if(arr[i] > arr[j]) { 22 | temp = arr[i]; 23 | arr[i] = arr[j]; 24 | arr[j] = temp; 25 | } 26 | } 27 | } 28 | 29 | printf("\n"); 30 | 31 | //Displaying elements of array after sorting 32 | printf("Elements of array sorted in ascending order: \n"); 33 | for (int i = 0; i < length; i++) { 34 | printf("%d ", arr[i]); 35 | } 36 | return 0; 37 | } 38 | 39 | /* 40 | Output 41 | 42 | Elements of original array: 43 | 5 2 8 7 1 44 | Elements of array sorted in ascending order: 45 | 1 2 5 7 8 46 | */ 47 | -------------------------------------------------------------------------------- /Assigning_if.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int i; 4 | if (i=0,2,3) 5 | printf("Rani Lakshmi Bhai\n"); 6 | else{ printf("Nivedita\n");} 7 | printf("%d\n",i); 8 | } 9 | -------------------------------------------------------------------------------- /BST.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct btnode 5 | { 6 | int value; 7 | struct btnode *l; 8 | struct btnode *r; 9 | }*root = NULL, *temp = NULL, *t2, *t1; 10 | 11 | void delete1(); 12 | void insert(); 13 | void delete(); 14 | void inorder(struct btnode *t); 15 | void create(); 16 | void search(struct btnode *t); 17 | void preorder(struct btnode *t); 18 | void postorder(struct btnode *t); 19 | void search1(struct btnode *t,int data); 20 | int smallest(struct btnode *t); 21 | int largest(struct btnode *t); 22 | void dealocate(struct btnode *t); 23 | //void searchNode(struct btnode *t, int value); 24 | 25 | 26 | int flag = 1; 27 | 28 | 29 | // create a node 30 | void create() 31 | { 32 | int data; 33 | 34 | printf("Enter data of node to be inserted : "); 35 | scanf("%d", &data); 36 | temp = (struct btnode *)malloc(sizeof(struct btnode)); 37 | temp->value = data; 38 | temp->l = temp->r = NULL; 39 | } 40 | 41 | 42 | // insert node 43 | void insert() 44 | { 45 | create(); 46 | if (root == NULL) 47 | root = temp; 48 | else 49 | search(root); 50 | } 51 | 52 | // position to insert the new node 53 | void search(struct btnode *t) 54 | { 55 | if ((temp->value > t->value) && (t->r != NULL)) //value more- insert at right 56 | search(t->r); 57 | else if ((temp->value > t->value) && (t->r == NULL)) 58 | t->r = temp; 59 | else if ((temp->value < t->value) && (t->l != NULL)) // value less- insert at left 60 | search(t->l); 61 | else if ((temp->value < t->value) && (t->l == NULL)) 62 | t->l = temp; 63 | 64 | else if(temp->value== t->value) 65 | printf("duplicate value of node not permitted."); 66 | } 67 | 68 | 69 | void preorder(struct btnode *t) 70 | { 71 | if (root == NULL) 72 | { 73 | printf("No elements in a tree to display"); 74 | return; 75 | } 76 | printf("%d -> ", t->value); 77 | if (t->l != NULL) 78 | preorder(t->l); 79 | if (t->r != NULL) 80 | preorder(t->r); 81 | } 82 | 83 | // inorder 84 | void inorder(struct btnode *t) 85 | { 86 | if (root == NULL) 87 | { 88 | printf("No elements in a tree to display"); 89 | return; 90 | } 91 | if (t->l != NULL) 92 | inorder(t->l); 93 | printf("%d -> ", t->value); 94 | if (t->r != NULL) 95 | inorder(t->r); 96 | } 97 | 98 | /*search 99 | void searchnode(struct btnode *t, data) 100 | { 101 | 102 | if ((data>t->value)) 103 | { 104 | t1 = t; 105 | searchnode(t->r, data); 106 | } 107 | else if ((data < t->value)) 108 | { 109 | t1 = t; 110 | searchnode(t->l, data); 111 | } 112 | else if ((data==t->value)) 113 | { 114 | printf("%d present", data); 115 | } 116 | else{ 117 | printf("%d not present", data); 118 | } 119 | } 120 | */ 121 | 122 | 123 | 124 | /*searchNode() will search for the particular node in the binary tree 125 | void searchNode(struct btnode *t, int value){ 126 | //Check whether tree is empty 127 | boolean flag = false; 128 | if(root == NULL){ 129 | printf("Tree is empty\n"); 130 | } 131 | else{ 132 | //If value is found in the given binary tree then, set the flag to true 133 | if(t->value == value){ 134 | flag = true; 135 | return; 136 | } 137 | //Search in left subtree 138 | if(flag == false && t->l != NULL){ 139 | searchNode(t->l, value); 140 | } 141 | //Search in right subtree 142 | if(flag == false && t->r != NULL){ 143 | searchNode(t->r, value); 144 | } 145 | } 146 | } */ 147 | 148 | 149 | 150 | 151 | 152 | /* postorder */ 153 | void postorder(struct btnode *t) 154 | { 155 | if (root == NULL) 156 | { 157 | printf("No elements in a tree to display "); 158 | return; 159 | } 160 | if (t->l != NULL) 161 | postorder(t->l); 162 | if (t->r != NULL) 163 | postorder(t->r); 164 | printf("%d -> ", t->value); 165 | } 166 | 167 | 168 | /* delete node */ 169 | void delete() 170 | { 171 | int data; 172 | 173 | if (root == NULL) 174 | { 175 | printf("No elements in a tree to delete"); 176 | return; 177 | } 178 | printf("Enter the data to be deleted : "); 179 | scanf("%d", &data); 180 | t1 = root; 181 | t2 = root; 182 | search1(root, data); 183 | } 184 | 185 | 186 | 187 | /* position of the node that is to be deleted */ 188 | void search1(struct btnode *t, int data) 189 | { 190 | if ((data>t->value)) 191 | { 192 | t1 = t; 193 | search1(t->r, data); 194 | } 195 | else if ((data < t->value)) 196 | { 197 | t1 = t; 198 | search1(t->l, data); 199 | } 200 | else if ((data==t->value)) 201 | { 202 | delete1(t); 203 | } 204 | } 205 | 206 | 207 | // To delete a node 208 | void delete1(struct btnode *t) 209 | { 210 | int k; 211 | 212 | // To delete leaf node 213 | if ((t->l == NULL) && (t->r == NULL)) 214 | { 215 | if (t1->l == t) 216 | { 217 | t1->l = NULL; 218 | } 219 | else 220 | { 221 | t1->r = NULL; 222 | } 223 | t = NULL; 224 | free(t); 225 | return; 226 | } 227 | 228 | // To delete node having one left hand child 229 | else if ((t->r == NULL)) 230 | { 231 | if (t1 == t) 232 | { 233 | root = t->l; 234 | t1 = root; 235 | } 236 | else if (t1->l == t) 237 | { 238 | t1->l = t->l; 239 | 240 | } 241 | else 242 | { 243 | t1->r = t->l; 244 | } 245 | t = NULL; 246 | free(t); 247 | return; 248 | } 249 | 250 | /* To delete node having right hand child */ 251 | else if (t->l == NULL) 252 | { 253 | if (t1 == t) 254 | { 255 | root = t->r; 256 | t1 = root; 257 | } 258 | else if (t1->r == t) 259 | t1->r = t->r; 260 | else 261 | t1->l = t->r; 262 | t == NULL; 263 | free(t); 264 | return; 265 | } 266 | 267 | /* To delete node having two child */ 268 | else if ((t->l != NULL) && (t->r != NULL)) 269 | { 270 | t2 = root; 271 | if (t->r != NULL) 272 | { 273 | k = smallest(t->r); 274 | flag = 1; 275 | } 276 | else 277 | { 278 | k =largest(t->l); 279 | flag = 2; 280 | } 281 | search1(root, k); 282 | t->value = k; 283 | } 284 | 285 | } 286 | 287 | 288 | 289 | 290 | 291 | /* find smallest */ 292 | int smallest(struct btnode *t) 293 | { 294 | t2 = t; 295 | if (t->l != NULL) 296 | { 297 | t2 = t; 298 | return(smallest(t->l)); 299 | } 300 | else 301 | return (t->value); 302 | } 303 | 304 | /* find largest */ 305 | int largest(struct btnode *t) 306 | { 307 | if (t->r != NULL) 308 | { 309 | t2 = t; 310 | return(largest(t->r)); 311 | } 312 | else 313 | return(t->value); 314 | } 315 | 316 | 317 | /* Dealocate the BST */ 318 | void dealocate(struct btnode *t) 319 | { 320 | if (t == NULL) return; 321 | 322 | 323 | dealocate(t->l); 324 | dealocate(t->r); 325 | free(t); 326 | } 327 | 328 | 329 | 330 | void main() 331 | { 332 | int ch; 333 | 334 | printf("\nOPERATIONS ---"); 335 | printf("\n1 - Insert an element into tree\n"); 336 | printf("2 - Delete an element from the tree\n"); 337 | printf("3 - Inorder Traversal\n"); 338 | printf("4 - Preorder Traversal\n"); 339 | printf("5 - Postorder Traversal\n"); 340 | // printf("6 - search\n"); 341 | printf("6 - Exit\n"); 342 | while(1) 343 | { 344 | printf("\nEnter your choice : "); 345 | scanf("%d", &ch); 346 | switch (ch) 347 | { 348 | case 1: 349 | insert(); 350 | break; 351 | case 2: 352 | delete(); 353 | break; 354 | case 3: 355 | inorder(root); 356 | break; 357 | case 4: 358 | preorder(root); 359 | break; 360 | case 5: 361 | postorder(root); 362 | break; 363 | 364 | /* case 6: 365 | { 366 | int val; 367 | printf("enter value to search"); 368 | scanf("%d", &val); 369 | 370 | searchNode(root, val ); 371 | if(flag) 372 | printf("Element is present in the binary tree"); 373 | else 374 | printf("Element is not present in the binary tree"); 375 | return 0; 376 | } 377 | 378 | 379 | break;*/ 380 | case 6: 381 | { 382 | dealocate(root); 383 | printf("BST has been de allocated "); 384 | exit(0); 385 | } 386 | default : 387 | printf("Wrong choice, Please enter correct choice "); 388 | break; 389 | } 390 | } 391 | } 392 | -------------------------------------------------------------------------------- /BankersAlgorithm.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10], safeSequence[10]; 5 | int p, r, i, j, process, count; 6 | count = 0; 7 | 8 | // read no of processes(p0,p1,p2,p3) 9 | printf("Enter the no of processes : "); 10 | scanf("%d", &p); 11 | 12 | //read no of resources (A,B,C,D) 13 | printf("\n\nEnter the no of resources : "); 14 | scanf("%d", &r); 15 | 16 | //read alloc (for p processes and r resources) 17 | printf("\n\nEnter the allocation for each process : "); 18 | for(i = 0; i < p; i++) 19 | { 20 | printf("\nFor process %d : ",i + 1); 21 | for(j = 0; j < r; j++) 22 | scanf("%d", &alloc[i][j]); 23 | } 24 | 25 | //read max (for p processes and r resources) 26 | printf("\n\nEnter the Max Matrix for each process : "); 27 | for(i = 0; i < p; i++) 28 | { 29 | printf("\nFor process %d : ", i + 1); 30 | for(j = 0; j < r; j++) 31 | scanf("%d", &Max[i][j]); 32 | } 33 | 34 | //read avail (for p processes and r resources) 35 | printf("\n\nEnter the Available Resources : "); 36 | for(i = 0; i < r; i++) 37 | scanf("%d", &avail[i]); 38 | 39 | //calc need (for p processes and r resources) 40 | for(i = 0; i < p; i++) 41 | for(j = 0; j < r; j++) 42 | need[i][j] = Max[i][j] - alloc[i][j]; 43 | 44 | // set all completed proccesses as 0 45 | for(i = 0; i< p; i++) 46 | completed[i] = 0; 47 | 48 | // do while count !=process & process!=-1 49 | do 50 | { 51 | // process no i will be assigned to 'process' variable .So initialized as -1 52 | process = -1; 53 | 54 | // for each process p , if completion of p is 0 ,set process to i 55 | for(i = 0; i < p; i++) 56 | { 57 | if(completed[i] == 0) 58 | { 59 | process = i ; 60 | 61 | //for each resource r , if need > avail (process not selected),set process -1 62 | for(j = 0; j < r; j++) 63 | { 64 | 65 | if(need[i][j] > avail[j]) 66 | { 67 | process = -1; 68 | break; 69 | } 70 | } 71 | } 72 | // break loop if selected (process!=-1) 73 | if(process != -1) 74 | break; 75 | } 76 | 77 | // if selected 78 | if(process != -1) 79 | { 80 | //print 'process +1 selected' (if p=3 ; i=0,1,2,3 ; if i = 2 selected ,3rd process is selected) 81 | printf("\nProcess %d runs to completion!", process + 1); 82 | 83 | // process stored to safe sequence matrix 84 | safeSequence[count] = process + 1; 85 | count++; 86 | 87 | //for each resource r , 88 | for(j = 0; j < r; j++) 89 | { 90 | //new alloc = alloc +avail (not i but selected i which is 'process') 91 | avail[j] += alloc[process][j]; 92 | // alloc & max = 0 ,completion of 'process' =1 93 | alloc[process][j] = 0; 94 | Max[process][j] = 0; 95 | completed[process] = 1; 96 | } 97 | } 98 | }while(count != p && process != -1); 99 | 100 | // if all process selected ,ie count ==p 101 | if(count == p) 102 | { 103 | // print safe state 104 | printf("\nThe system is in a safe state!!\n"); 105 | // print safe sequence [i] 106 | printf("Safe Sequence : < "); 107 | for( i = 0; i < p; i++) 108 | printf("%d ", safeSequence[i]); 109 | printf(">\n"); 110 | } 111 | 112 | // else print unsafe 113 | else 114 | printf("\nThe system is in an unsafe state!!"); 115 | } 116 | -------------------------------------------------------------------------------- /Basic_Calculator.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | 5 | char op; 6 | double first, second; 7 | printf("Enter an operator (+, -, *, /): "); 8 | scanf("%c", &op); 9 | printf("Enter two operands: "); 10 | scanf("%lf %lf", &first, &second); 11 | 12 | switch (op) { 13 | case '+': 14 | printf("%.1lf + %.1lf = %.1lf", first, second, first + second); 15 | break; 16 | case '-': 17 | printf("%.1lf - %.1lf = %.1lf", first, second, first - second); 18 | break; 19 | case '*': 20 | printf("%.1lf * %.1lf = %.1lf", first, second, first * second); 21 | break; 22 | case '/': 23 | printf("%.1lf / %.1lf = %.1lf", first, second, first / second); 24 | break; 25 | // operator doesn't match any case constant 26 | default: 27 | printf("Error! operator is not correct"); 28 | } 29 | 30 | return 0; 31 | } 32 | 33 | 34 | /* 35 | Output 36 | 37 | Enter an operator (+, -, *,): * 38 | Enter two operands: 1.5 39 | 4.5 40 | 1.5 * 4.5 = 6.8 41 | */ 42 | -------------------------------------------------------------------------------- /BearBro.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a,b,i=0; 6 | scanf("%d\t%d",&a,&b); 7 | while(a<=b) 8 | { 9 | a*=3; 10 | b*=2; 11 | i++; 12 | } 13 | printf("%d",i); 14 | return(0); 15 | } 16 | -------------------------------------------------------------------------------- /BinarySearch.c: -------------------------------------------------------------------------------- 1 | // Binary Search in C 2 | 3 | #include 4 | 5 | int binarySearch(int array[], int x, int low, int high) { 6 | if (high >= low) { 7 | int mid = low + (high - low) / 2; 8 | 9 | // If found at mid, then return it 10 | if (array[mid] == x) 11 | return mid; 12 | 13 | // Search the left half 14 | if (array[mid] > x) 15 | return binarySearch(array, x, low, mid - 1); 16 | 17 | // Search the right half 18 | return binarySearch(array, x, mid + 1, high); 19 | } 20 | 21 | return -1; 22 | } 23 | 24 | int main(void) { 25 | int array[] = {3, 4, 5, 6, 7, 8, 9}; 26 | int n = sizeof(array) / sizeof(array[0]); 27 | int x = 4; 28 | int result = binarySearch(array, x, 0, n - 1); 29 | if (result == -1) 30 | printf("Not found"); 31 | else 32 | printf("Element is found at index %d", result); 33 | } 34 | 35 | /* 36 | Element is found at index 1 37 | Time Complexities 38 | Best case complexity: O(1) 39 | Average case complexity: O(log n) 40 | Worst case complexity: O(log n) 41 | */ 42 | -------------------------------------------------------------------------------- /Binary_Sort/Binary.c: -------------------------------------------------------------------------------- 1 | #include 2 | int binarySearch(int a[], int start, int end, int val) 3 | { 4 | int mid; 5 | if (end >= start) 6 | { 7 | mid = (start + end) / 2; 8 | if (a[mid] == val) 9 | return mid + 1; 10 | else if (a[mid] < val) 11 | return binarySearch(a, mid + 1, end, val); 12 | else 13 | return binarySearch(a, start, mid - 1, val); 14 | } 15 | return -1; 16 | } 17 | int main() 18 | { 19 | int length; 20 | printf("Enter the size of the array: "); 21 | scanf("%d", &length); 22 | int a[length]; 23 | printf("enter the elements of the array: "); 24 | for (int i = 0; i < length; i++) 25 | scanf("%d", &a[i]); 26 | int val; 27 | printf("Enter the element to be found: "); 28 | scanf("%d", &val); 29 | int n = sizeof(a) / sizeof(a[0]); 30 | int res = binarySearch(a, 0, n - 1, val); 31 | if (res == -1) 32 | printf("Element is not present in the array"); 33 | else 34 | printf("Element is present at %d index of array", res - 1); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Binary_Sort/README.md: -------------------------------------------------------------------------------- 1 | ## Binary Sorting 2 | 3 | - Binary insertion sort is a sorting algorithm which is similar to the insertion sort, but instead of using linear search to find the location where an element should be inserted, we use binary search. Thus, we reduce the comparative value of inserting a single element from O (N) to O (log N). 4 | 5 | - It is a flexible algorithm, which means it works faster when the same given members are already heavily sorted, i.e., the current location of the feature is closer to its actual location in the sorted list. 6 | 7 | - It is a stable filtering algorithm – elements with the same values ​​appear in the same sequence in the last order as they were in the first list. 8 | -------------------------------------------------------------------------------- /Caesar-Cipher.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(){ 4 | int N,K; 5 | scanf("%d",&N); 6 | unsigned char* S = (char *)malloc(10240 * sizeof(char)); //malloc () is used for dynamic memory allocation. 7 | scanf("%s",S); 8 | scanf("%d",&K); 9 | for(int i=0;i= 97 && S[i] <= 122) //For small letters 12 | { 13 | S[i]=S[i]+K; //ASCII value increasing. 14 | while(S[i]>122) 15 | S[i]=(S[i]%122)+97-1; //after z it will start from a agin. 16 | 17 | } 18 | else if(S[i] >= 65 && S[i] <= 90) //For capital letters 19 | { 20 | S[i]=S[i]+K; 21 | while(S[i]>90) 22 | S[i]=(S[i]%90)+65-1; // after Z it will start from A again. 23 | } 24 | } printf("%s",S); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Celcius to Fehrenheit.c: -------------------------------------------------------------------------------- 1 | //Write a program to input temperature in Celsius & to print its Fahrenheit equivalent. F = C * (9/5) + 32. 2 | #include 3 | int main() 4 | {float F,C; 5 | printf("The temperature in Celsius is\t"); 6 | scanf("%f",&C); 7 | F = (C *9/5) + 32; 8 | printf("The temperature in Fahrenheit is\t%f",F); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /Celebrity_Problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | int n; 6 | scanf("%d", &n); 7 | 8 | int arr[n]; 9 | 10 | for(int i=0; i 2 | #include 3 | 4 | int main() 5 | { 6 | float principle, rate, time, CI; 7 | 8 | /* Input principle, time and rate */ 9 | printf("Enter principle (amount): "); 10 | scanf("%f", &principle); 11 | 12 | printf("Enter time: "); 13 | scanf("%f", &time); 14 | 15 | printf("Enter rate: "); 16 | scanf("%f", &rate); 17 | 18 | /* Calculate compound interest */ 19 | CI = principle* (pow((1 + rate / 100), time)); 20 | 21 | /* Print the resultant CI */ 22 | printf("Compound Interest = %f", CI); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Concatination.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(){ 4 | char a1[100] = {}; 5 | char a2[100] = {}; 6 | char a3[200] = {}; 7 | int i = 0, j = 0; 8 | printf("Enter a string: \n"); 9 | scanf("%s", a1); 10 | printf("Enter another string: \n"); 11 | scanf("%s", a2); 12 | // Insert the first string in the new string 13 | while (a1[i] != '\0') { 14 | a3[j] = a1[i]; 15 | i++; 16 | j++; 17 | } 18 | // Insert the second string in the new string 19 | i = 0; 20 | while (a2[i] != '\0') { 21 | a3[j] = a2[i]; 22 | i++; 23 | j++; 24 | } 25 | a3[j] = '\0'; 26 | printf("The concatinated string is without using the function is: %s", a3); 27 | strcat(a1, a2); 28 | printf("\nThe concatinated string using the function is: %s", a1); 29 | retur 30 | -------------------------------------------------------------------------------- /Dijikstra’s_algorithm.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define V 9 4 | int minDistance(int dist[], bool sptSet[]) { 5 | int min = INT_MAX, min_index; 6 | for (int v = 0; v < V; v++) 7 | if (sptSet[v] == false && dist[v] <= min) 8 | min = dist[v], min_index = v; 9 | return min_index; 10 | } 11 | int printSolution(int dist[], int n) { 12 | printf("Vertex Distance from Source\n"); 13 | for (int i = 0; i < V; i++) 14 | printf("%d \t %d\n", i, dist[i]); 15 | } 16 | void dijkstra(int graph[V][V], int src) { 17 | int dist[V]; 18 | bool sptSet[V]; 19 | for (int i = 0; i < V; i++) 20 | dist[i] = INT_MAX, sptSet[i] = false; 21 | dist[src] = 0; 22 | for (int count = 0; count < V - 1; count++) { 23 | int u = minDistance(dist, sptSet); 24 | sptSet[u] = true; 25 | for (int v = 0; v < V; v++) 26 | if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) dist[v] = dist[u] + graph[u][v]; 27 | } 28 | printSolution(dist, V); 29 | } 30 | int main() { 31 | int graph[V][V] = { { 0, 6, 0, 0, 0, 0, 0, 8, 0 }, 32 | { 6, 0, 8, 0, 0, 0, 0, 13, 0 }, 33 | { 0, 8, 0, 7, 0, 6, 0, 0, 2 }, 34 | { 0, 0, 7, 0, 9, 14, 0, 0, 0 }, 35 | { 0, 0, 0, 9, 0, 10, 0, 0, 0 }, 36 | { 0, 0, 6, 14, 10, 0, 2, 0, 0 }, 37 | { 0, 0, 0, 0, 0, 2, 0, 1, 6 }, 38 | { 8, 13, 0, 0, 0, 0, 1, 0, 7 }, 39 | { 0, 0, 2, 0, 0, 0, 6, 7, 0 } 40 | }; 41 | dijkstra(graph, 0); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Directed_Graph.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Define the maximum number of vertices in the graph 5 | #define N 6 6 | 7 | // Data structure to store a graph object 8 | struct Graph 9 | { 10 | // An array of pointers to Node to represent an adjacency list 11 | struct Node* head[N]; 12 | }; 13 | 14 | // Data structure to store adjacency list nodes of the graph 15 | struct Node 16 | { 17 | int dest; 18 | struct Node* next; 19 | }; 20 | 21 | // Data structure to store a graph edge 22 | struct Edge { 23 | int src, dest; 24 | }; 25 | 26 | // Function to create an adjacency list from specified edges 27 | struct Graph* createGraph(struct Edge edges[], int n) 28 | { 29 | // allocate storage for the graph data structure 30 | struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph)); 31 | 32 | // initialize head pointer for all vertices 33 | for (int i = 0; i < N; i++) { 34 | graph->head[i] = NULL; 35 | } 36 | 37 | // add edges to the directed graph one by one 38 | for (int i = 0; i < n; i++) 39 | { 40 | // get the source and destination vertex 41 | int src = edges[i].src; 42 | int dest = edges[i].dest; 43 | 44 | // allocate a new node of adjacency list from src to dest 45 | struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); 46 | newNode->dest = dest; 47 | 48 | // point new node to the current head 49 | newNode->next = graph->head[src]; 50 | 51 | // point head pointer to the new node 52 | graph->head[src] = newNode; 53 | } 54 | 55 | return graph; 56 | } 57 | 58 | // Function to print adjacency list representation of a graph 59 | void printGraph(struct Graph* graph) 60 | { 61 | for (int i = 0; i < N; i++) 62 | { 63 | // print current vertex and all its neighbors 64 | struct Node* ptr = graph->head[i]; 65 | while (ptr != NULL) 66 | { 67 | printf("(%d —> %d)\t", i, ptr->dest); 68 | ptr = ptr->next; 69 | } 70 | 71 | printf("\n"); 72 | } 73 | } 74 | 75 | // Directed graph implementation in C 76 | int main(void) 77 | { 78 | // input array containing edges of the graph (as per the above diagram) 79 | // (x, y) pair in the array represents an edge from x to y 80 | struct Edge edges[] = 81 | { 82 | {0, 1}, {1, 2}, {2, 0}, {2, 1}, {3, 2}, {4, 5}, {5, 4} 83 | }; 84 | 85 | // calculate the total number of edges 86 | int n = sizeof(edges)/sizeof(edges[0]); 87 | 88 | // construct a graph from the given edges 89 | struct Graph *graph = createGraph(edges, n); 90 | 91 | // Function to print adjacency list representation of a graph 92 | printGraph(graph); 93 | 94 | return 0; 95 | } -------------------------------------------------------------------------------- /Disarium.c: -------------------------------------------------------------------------------- 1 | // That accept an integer number, find if it is Disarium number or not (Hint: A number 2 | // is said to be a Disarium number when the sum of its digit raised to the power of their 3 | // respective position is equal to the number itself. Eg: 175 is a Disarium number, 11 + 4 | // 72 + 53 = 175) 5 | 6 | #include 7 | #include 8 | 9 | int disarim(int a, int m){ 10 | int ans=0, n=0; 11 | while(a!=0){ 12 | n = a%10; 13 | a/=10; 14 | ans += pow(n,m); 15 | --m; 16 | } 17 | } 18 | 19 | int main(){ 20 | int num, sum, length; 21 | printf("Enter the number of digits in the number: "); 22 | scanf("%d",&length); 23 | printf("Enter a num: "); 24 | scanf("%d",&num); 25 | sum = disarim(num, length); 26 | if (sum == num) 27 | printf("%d is an disarium number",num); 28 | else 29 | printf("%d is not an disarium number",num); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Duplicate_indexArr.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int n; 4 | int checkDuplicate(int arr[], int element) 5 | { 6 | int p = 0; 7 | for (int i = 0; i < n; i++) 8 | if (arr[i] == element) 9 | p++; 10 | if (p > 1) 11 | return 1; 12 | else 13 | return 0; 14 | } 15 | 16 | void findPosition(int arr[], int element) 17 | { 18 | for (int i = 0; i < n; i++) 19 | if (arr[i] == element) 20 | printf("%d ", i); 21 | } 22 | int main() 23 | { 24 | printf("Enter the size of the array: "); 25 | scanf("%d", &n); 26 | int arr[n]; 27 | printf("Enter the array elements: \n"); 28 | for (int i = 0; i < n; i++) 29 | scanf("%d", &arr[i]); 30 | int key=0; 31 | for (int i = 0; i < n; i++) 32 | if (checkDuplicate(arr, arr[i]) == 1) 33 | { 34 | printf("The repeated element is: %d and it is at the index: ", arr[i]); 35 | findPosition(arr, arr[i]); 36 | printf("\n"); 37 | // break; 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /EvenOdd_dowhile.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int i,j=0; 4 | printf("EVEN "); 5 | do{ 6 | if (i%2==0){ 7 | printf("%d ",i);} 8 | ++i; 9 | }while (i<=50); 10 | 11 | printf("\nODD "); 12 | do{ 13 | if (j%2!=0){ 14 | printf("%d ",j);} 15 | ++j; 16 | }while (j<=50); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Exponents.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | double F,numerator,denominator; 7 | numerator=(2.9678*pow(10,-27))+(0.876*pow(10,-38)); 8 | denominator=(7.025*pow(10,16))-(9.75*pow(10,12)); 9 | F=numerator/denominator; 10 | printf("Evaluated answer: %e",F); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /Factorial.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Recursive method 4 | 5 | int factorial_recur(int num) { 6 | if (num == 0 || num == 1) 7 | return 1; 8 | return num * factorial_recur(num - 1); 9 | } 10 | 11 | 12 | // Iterative method 13 | 14 | int factorial(int num) { 15 | int fac = 1; 16 | for (int x = 2; x <= num; x++) 17 | fac = fac * x; 18 | return fac; 19 | } 20 | 21 | int main() 22 | { 23 | int num = 5; 24 | 25 | int fac = factorial(5); 26 | 27 | printf("Factorial of %d is %d", num, fac); 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Fibonacci_Recursion.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | using namespace std; 4 | int F[100]; 5 | int fib(int n) 6 | { 7 | if (n <= 1) 8 | { 9 | F[n] = n; 10 | return n; 11 | } 12 | else 13 | { 14 | if (F[n - 1] == -1) 15 | F[n - 1] = fib(n - 1); 16 | if (F[n - 2] == -1) 17 | F[n - 2] = fib(n - 2); 18 | F[n] = F[n - 1] + F[n - 2]; 19 | return F[n - 1] + F[n - 2]; 20 | } 21 | } 22 | int main() 23 | { 24 | int n = 0; 25 | cout << "Enter how many numbers you want to print : "; 26 | cin >> n; 27 | for (int i = 0; i < 100; i++) 28 | { 29 | F[i] = -1; 30 | } 31 | for (int i = 0; i < n; i++) 32 | { 33 | cout << fib(i) << " "; 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Fibonacci_Series.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | 4 | int i, n; 5 | 6 | // initialize first and second terms 7 | int t1 = 0, t2 = 1; 8 | 9 | // initialize the next term (3rd term) 10 | int nextTerm = t1 + t2; 11 | 12 | // get no. of terms from user 13 | printf("Enter the number of terms: "); 14 | scanf("%d", &n); 15 | 16 | // print the first two terms t1 and t2 17 | printf("Fibonacci Series: %d, %d, ", t1, t2); 18 | 19 | // print 3rd to nth terms 20 | for (i = 3; i <= n; ++i) { 21 | printf("%d, ", nextTerm); 22 | t1 = t2; 23 | t2 = nextTerm; 24 | nextTerm = t1 + t2; 25 | } 26 | 27 | return 0; 28 | } 29 | 30 | /* 31 | Output 32 | 33 | Enter the number of terms: 10 34 | Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 35 | */ 36 | -------------------------------------------------------------------------------- /Geometric.c: -------------------------------------------------------------------------------- 1 | // That accepts a number and find the sum of geometric series of that number. (Hint: 2 | // Geometric series of 4 = 1+1/2+1/4+1/8) 3 | 4 | #include 5 | #include 6 | 7 | double geometric(int n){ 8 | if (n<2){ 9 | return 1; 10 | } 11 | else{ 12 | return 1/pow(2,n)+geometric(n-1); 13 | } 14 | } 15 | 16 | int main(){ 17 | int n; 18 | printf("Enter a number n: "); 19 | scanf("%d",&n); 20 | printf("The geometric sum of the series is: %f", geometric(n-1)); 21 | } 22 | -------------------------------------------------------------------------------- /Grades.c: -------------------------------------------------------------------------------- 1 | // Write a C program to input marks of four subjects Physics, Chemistry, Biology, and 2 | // Mathematics. Calculate percentage and grade according to following 3 | 4 | #include 5 | 6 | int main() 7 | { 8 | int arr[4]; 9 | int sum; 10 | int percent; 11 | for (int i = 0; i < 4; i++) 12 | { 13 | printf("Enter the marks: "); 14 | scanf("%d", &arr[i]); 15 | sum += arr[i]; 16 | } 17 | percent = (sum / 4); 18 | if (percent >= 90) 19 | printf("U got GRADE A"); 20 | else if (percent >= 80 && percent < 90) 21 | printf("U got GRADE B"); 22 | else if (percent >= 70 && percent < 80) 23 | printf("U got GRADE C"); 24 | else if (percent >= 60 && percent < 70) 25 | printf("U got GRADE D"); 26 | else if (percent >= 40 && percent < 60) 27 | printf("U got GRADE E"); 28 | else 29 | printf("U got GRADE F"); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /GraphColoring.c: -------------------------------------------------------------------------------- 1 | //Graph Coloring algorithm using backtracking in c 2 | #include 3 | int G[10][10],m,edges,color_tab[10],v1,v2,i,j,n,a,b; 4 | void Gen_Col_Value(int,int); 5 | void Gr_coloring(int,int); 6 | int main() 7 | { 8 | printf("\nEnter the number of nodes & edges\n"); 9 | scanf("%d%d",&n,&edges); 10 | m=n-1; 11 | printf("\nEnter the edges of the graph\n\n"); 12 | for (i=1;i<=edges; i++) 13 | { 14 | printf("Enter value of x,y\n"); 15 | scanf("%d%d",&v1,&v2); 16 | G[v1][v2] = G[v2][v1] = 1; 17 | printf("\n"); 18 | } 19 | Gr_coloring(1,n); 20 | printf("\n The Vertices To be Coloured As...\n"); 21 | for(i=1;i<=n;i++) 22 | printf("\n V%d:=%d",i,color_tab[i]); 23 | return 0; 24 | } 25 | void Gen_Col_Value(int k,int n) 26 | { 27 | while(1) 28 | { 29 | a=color_tab[k]+1; 30 | b=m+1; 31 | color_tab[k] = a%b; 32 | if(color_tab[k]==0) return; 33 | for(j=1;j<=n;j++) 34 | { 35 | if(G[k][j] && color_tab[k]==color_tab[j]) 36 | break; 37 | } 38 | if(j==n+1) return; 39 | } 40 | } 41 | void Gr_coloring(int k,int n) 42 | { 43 | Gen_Col_Value(k,n); 44 | if(color_tab[k]==0) return; 45 | if(k==n) return; 46 | else Gr_coloring(k+1,n); 47 | } 48 | /* 49 | output:- 50 | 51 | Enter the number of nodes & edges 52 | 4 53 | 5 54 | 55 | Enter the edges of the graph 56 | 57 | Enter value of x,y 58 | 0 59 | 1 60 | 61 | Enter value of x,y 62 | 1 63 | 2 64 | 65 | Enter value of x,y 66 | 1 67 | 3 68 | 69 | Enter value of x,y 70 | 2 71 | 3 72 | 73 | Enter value of x,y 74 | 3 75 | 0 76 | 77 | The Vertices To be Coloured As... 78 | 79 | V1:=1 80 | V2:=2 81 | V3:=3 82 | V4:=1 83 | */ 84 | -------------------------------------------------------------------------------- /HeronsFormula.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main() 6 | { 7 | int x1,y1,x2,y2,x3,y3; 8 | double AB,BC,AC; 9 | printf("Enter the first coordinate x1 y1:"); 10 | scanf("%d%d",&x1,&y1); 11 | printf("Enter the first coordinate x2 y2:"); 12 | scanf("%d%d",&x2,&y2); 13 | printf("Enter the first coordinate x2 y2:"); 14 | scanf("%d%d",&x3,&y3); 15 | 16 | AB=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); 17 | BC=sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3)); 18 | AC=sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1)); 19 | printf("The length of the 3 sides is: %lf %lf %lf",AB,BC,AC); 20 | 21 | double s,area; 22 | s=(AB+BC+AC)*0.5; 23 | area=sqrt(s*(s-AB)*(s-BC)*(s-AC)); 24 | printf("The area of the triangle is: %lf",area); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Insertion&Searching_BST.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node{ 4 | int data; 5 | struct node *left; 6 | struct node *right; 7 | }; 8 | struct node * createNode(int value) 9 | { 10 | struct node *n; 11 | n=(struct node *)malloc(sizeof(struct node)); 12 | n->data=value; 13 | n->left=NULL; 14 | n->right=NULL; 15 | return n; 16 | } 17 | struct node * search(struct node *root,int key) 18 | { 19 | while(root!=NULL) 20 | { 21 | if(root->data ==key) 22 | { 23 | return root; 24 | } 25 | else if(root->data>key) 26 | { 27 | root=root->left; 28 | } 29 | else{ 30 | root=root->right; 31 | } 32 | } 33 | return NULL; 34 | } 35 | void insert(struct node *root, int key) 36 | { 37 | struct node *prev=NULL; 38 | while(root!=NULL) 39 | { 40 | prev =root; 41 | if(key==root->data) 42 | { 43 | printf("The element %d cannot be inserted in the Binary Search Tree\n",key); 44 | return ; 45 | } 46 | else if(keydata) 47 | { 48 | root=root->left; 49 | } 50 | else{ 51 | root=root->right; 52 | } 53 | } 54 | struct node *new=createNode(key); 55 | if(keydata) 56 | { 57 | prev->left=new; 58 | }else{ 59 | prev->right=new; 60 | } 61 | 62 | } 63 | int main() 64 | { 65 | struct node *p1=createNode(5); 66 | struct node *p2=createNode(3); 67 | struct node *p3=createNode(6); 68 | struct node *p4=createNode(1); 69 | struct node *p5=createNode(4); 70 | p1->left=p2; 71 | p1->right=p3; 72 | p2->left=p4; 73 | p2->right=p5; 74 | 75 | printf("Enter the element to be inserted:\n"); 76 | int num; 77 | scanf("%d",&num); 78 | insert(p1,num); 79 | printf("Enter the element to be searched:\n"); 80 | int e; 81 | scanf("%d",&e); 82 | struct node *x= search(p1,e); 83 | if(x!=NULL) 84 | { 85 | printf("The element %d has been found in the Binary Search Tree\n",x->data); 86 | }else{ 87 | printf("The element is not found in the Binary Search Tree\n"); 88 | } 89 | 90 | 91 | return 0; 92 | } -------------------------------------------------------------------------------- /Insertion_sort.c: -------------------------------------------------------------------------------- 1 | /* C Program to sort an array in ascending order using Insertion Sort */ 2 | 3 | #include 4 | int main(void) 5 | { 6 | int n, i, j, temp; 7 | int arr[64]; 8 | printf("Enter number of elements\n"); 9 | scanf("%d", &n); 10 | printf("Enter %d integers\n", n); 11 | for (i = 0; i < n; i++) 12 | 13 | scanf("%d", &arr[i]); 14 | for (i = 1; i < n; i++) 15 | { 16 | j = i; 17 | while (j > 0 && arr[j - 1] > arr[j]) 18 | { 19 | temp = arr[j]; 20 | arr[j] = arr[j - 1]; 21 | arr[j - 1] = temp; 22 | j--; 23 | } 24 | } 25 | printf("Sorted list in ascending order:\n"); 26 | for (i = 0; i < n; i++) 27 | { 28 | printf("%d\n", arr[i]); 29 | } 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Interestingpatter.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { int a=0; 4 | for (int i=0; i<5; i++){ 5 | for (int j=0;j 7 | #include 8 | 9 | int main() 10 | { 11 | int n; 12 | scanf("%d", &n); 13 | 14 | int arr[n]; 15 | 16 | for(int i=0; i 4 | #include 5 | #include 6 | int i,j,k,a,b,u,v,n,ne=1; 7 | int min,mincost=0,cost[9][9],parent[9]; // Declaration of variables 8 | int find(int); 9 | int uni(int,int); 10 | void main() 11 | { 12 | 13 | printf("\n\tImplementation of Kruskal's algorithm\n"); 14 | printf("\nEnter the no. of vertices:"); 15 | scanf("%d",&n); 16 | printf("\nEnter the cost adjacency matrix:\n"); 17 | for(i=1;i<=n;i++) 18 | { 19 | for(j=1;j<=n;j++) 20 | { 21 | printf("Enter for [%d,%d] \n",i,j); 22 | scanf("%d",&cost[i][j]); 23 | if(cost[i][j]==0) 24 | cost[i][j]=999; 25 | } 26 | } 27 | printf("The edges of Minimum Cost Spanning Tree are\n"); 28 | while(ne < n) 29 | { 30 | for(i=1,min=999;i<=n;i++) 31 | 32 | for(j=1;j <= n;j++) 33 | 34 | if(cost[i][j] < min) 35 | { 36 | min=cost[i][j]; 37 | a=u=i; 38 | b=v=j; 39 | } 40 | 41 | 42 | u=find(u); 43 | v=find(v); 44 | if(uni(u,v)) 45 | { 46 | printf("%d edge (%d,%d) =%d\n",ne++,a,b,min); 47 | mincost +=min; 48 | } 49 | cost[a][b]=cost[b][a]=999; 50 | } 51 | printf("\n\tMinimum cost = %d\n",mincost); 52 | } 53 | int find(int i) 54 | { 55 | while(parent[i]) 56 | i=parent[i]; 57 | return i; 58 | } 59 | int uni(int i,int j) 60 | { 61 | if(i!=j) 62 | { 63 | parent[j]=i; 64 | return 1; 65 | } 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /LCS.c: -------------------------------------------------------------------------------- 1 | // The longest common subsequence in C 2 | #include 3 | #include 4 | 5 | int i, j, m, n, LCS_table[20][20]; 6 | char S1[20] = "abaaba", S2[20] = "babbab", b[20][20]; 7 | void lcsAlgo() { 8 | m = strlen(S1); 9 | n = strlen(S2); 10 | // Filling 0's in the matrix 11 | for (i = 0; i <= m; i++) 12 | LCS_table[i][0] = 0; 13 | for (i = 0; i <= n; i++) 14 | LCS_table[0][i] = 0; 15 | // Creating the mtrix in bottom-up way 16 | for (i = 1; i <= m; i++) 17 | for (j = 1; j <= n; j++) { 18 | if (S1[i - 1] == S2[j - 1]) { 19 | LCS_table[i][j] = LCS_table[i - 1][j - 1] + 1; 20 | } else if (LCS_table[i - 1][j] >= LCS_table[i][j - 1]) { 21 | LCS_table[i][j] = LCS_table[i - 1][j]; 22 | } else { 23 | LCS_table[i][j] = LCS_table[i][j - 1]; 24 | } 25 | } 26 | 27 | int index = LCS_table[m][n]; 28 | char lcsAlgo[index + 1]; 29 | lcsAlgo[index] = '\0'; 30 | 31 | int i = m, j = n; 32 | while (i > 0 && j > 0) { 33 | if (S1[i - 1] == S2[j - 1]) { 34 | lcsAlgo[index - 1] = S1[i - 1]; 35 | i--; 36 | j--; 37 | index--; 38 | } 39 | 40 | else if (LCS_table[i - 1][j] > LCS_table[i][j - 1]) 41 | i--; 42 | else 43 | j--; 44 | } 45 | 46 | // Printing the sub sequences 47 | printf("S1 : %s \nS2 : %s \n", S1, S2); 48 | printf("LCS: %s", lcsAlgo); 49 | } 50 | 51 | int main() { 52 | lcsAlgo(); 53 | printf("\n"); 54 | } 55 | -------------------------------------------------------------------------------- /LinearSearch.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a; 5 | printf("Enter the number of elements in the array: "); 6 | scanf("%d",&a); 7 | int arr[a]; 8 | printf("Enter the elements of the array: "); 9 | for(int i=0; i 2 | #include 3 | #include 4 | struct node 5 | { 6 | int info; 7 | struct node *link; 8 | }; 9 | struct node *START; 10 | 11 | struct node* createnode() 12 | { 13 | struct node *n; 14 | n = (struct node*)malloc(sizeof(struct node)); 15 | return n; 16 | } 17 | 18 | void insertnodelast() 19 | { 20 | struct node *newnode, *temp; 21 | newnode = createnode(); 22 | printf("Enter a Value to insert: "); 23 | scanf("%d", &newnode->info); 24 | newnode->link = NULL; 25 | if (START == NULL) 26 | START = newnode; 27 | else 28 | { 29 | temp = START; 30 | while (temp->link != NULL) 31 | temp = temp->link; 32 | temp->link = newnode; 33 | } 34 | printf("Node Inserted.\n"); 35 | } 36 | 37 | void deletenodefirst() 38 | { 39 | struct node *dele; 40 | dele = START; 41 | if (START == NULL) 42 | printf("List is Empty.\n"); 43 | else 44 | { 45 | START = START->link; 46 | printf("Deleted Node: %d\n", dele->info); 47 | free(dele); 48 | } 49 | } 50 | void deletenodelast() 51 | { 52 | struct node *dele, *temp; 53 | if (START == NULL) 54 | printf("List is Empty.\n"); 55 | else if (START->link == NULL) 56 | { 57 | printf("Deleted Node: %d\n", START->info); 58 | START = NULL; 59 | } 60 | else 61 | { 62 | temp = START; 63 | while (temp->link != NULL) 64 | { 65 | dele = temp; 66 | temp = temp->link; 67 | } 68 | printf("Deleted Node: %d\n", temp->info); 69 | dele->link = NULL; 70 | } 71 | } 72 | void viewlist() 73 | { 74 | struct node *v; 75 | if (START == NULL) 76 | printf("List is Empty.\n"); 77 | else 78 | { 79 | v = START; 80 | printf("List Contain:\n"); 81 | while (v != NULL) 82 | { 83 | printf(" %d\n", v->info); 84 | v = v->link; 85 | } 86 | } 87 | } 88 | void countnode() 89 | { 90 | struct node *v; 91 | int count = 0; 92 | if (START == NULL) 93 | printf("List is Empty.\n"); 94 | else 95 | { 96 | v = START; 97 | printf("List Contain:\n"); 98 | while (v != NULL) 99 | { 100 | printf("%d\n", v->info); 101 | v = v->link; 102 | count++; 103 | } 104 | } 105 | printf("%d Node in the List.\n", count); 106 | } 107 | void insertnodefirst() 108 | { 109 | struct node *newnode; 110 | newnode = createnode(); 111 | printf("Enter Value to Insert:"); 112 | scanf("%d", &newnode->info); 113 | if (START == NULL) 114 | { 115 | newnode->link = NULL; 116 | START = newnode; 117 | } 118 | else 119 | { 120 | newnode->link = START; 121 | START = newnode; 122 | } 123 | printf("Node Inserted.\n"); 124 | } 125 | void insertnodesort() 126 | { 127 | struct node *newnode, *temp, *save; 128 | int e; 129 | newnode = createnode(); 130 | printf("Enter value to insert: "); 131 | scanf("%d", &e); 132 | newnode->info = e; 133 | if (START == NULL) 134 | { 135 | newnode->link = NULL; 136 | START = newnode; 137 | } 138 | else if (e < START->info) 139 | { 140 | newnode->link = START; 141 | START = newnode; 142 | } 143 | else 144 | { 145 | temp = START; 146 | while (e > temp->info && temp->link != NULL) 147 | { 148 | save = temp; 149 | temp = temp->link; 150 | } 151 | if (temp->link == NULL && e > temp->info) 152 | { 153 | temp->link = newnode; 154 | newnode->link = NULL; 155 | } 156 | else 157 | { 158 | save->link = newnode; 159 | newnode->link = temp; 160 | } 161 | } 162 | printf("Node Inserted.\n"); 163 | } 164 | void insertgivpos() 165 | { 166 | struct node *temp, *newnode, *save; 167 | int count = 0, pos, val; 168 | printf("Enter Value to insert: "); 169 | scanf("%d", &newnode->info); 170 | printf("Enter Position to insert: "); 171 | scanf("%d", &pos); 172 | if (START == NULL) 173 | printf("List is Empty.\n"); 174 | else 175 | { 176 | temp = START; 177 | while (count <= pos && temp->link != NULL) 178 | { 179 | save = temp; 180 | count++; 181 | temp = temp->link; 182 | if (count == pos) 183 | { 184 | save->link = newnode; 185 | newnode->link = save->link; 186 | } 187 | printf("Node Inserted.\n"); 188 | } 189 | } 190 | } 191 | void main() 192 | { 193 | int choice; 194 | while (1) 195 | { 196 | system("cls"); 197 | printf("1 Insert Node at the Beginning of the List.\n"); 198 | printf("2 Insert Node at the end of List.\n"); 199 | printf("3 Insert Node in Sorted Order.\n"); 200 | printf("4 Delete first Node of the List.\n"); 201 | printf("5 Delete Last Node of the List.\n"); 202 | printf("6 Display all Node in the List.\n"); 203 | printf("7 Count Number of Node in the List.\n"); 204 | printf("8 Insert node at Given Position.\n"); 205 | printf("9 Exit.\n"); 206 | 207 | printf("Enter your choice: "); 208 | scanf("%d", &choice); 209 | switch (choice) 210 | { 211 | case 1: insertnodefirst(); 212 | break; 213 | case 2: insertnodelast(); 214 | break; 215 | case 3: insertnodesort(); 216 | break; 217 | case 4: deletenodefirst(); 218 | break; 219 | case 5: deletenodelast(); 220 | break; 221 | case 6: viewlist(); 222 | break; 223 | case 7: countnode(); 224 | break; 225 | case 8: insertgivpos(); 226 | break; 227 | case 9: exit(0); 228 | break; 229 | } 230 | getch(); 231 | } 232 | } 233 | -------------------------------------------------------------------------------- /Linked_List.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node 4 | { 5 | int data; 6 | struct node *next; 7 | }; 8 | struct node *head; 9 | 10 | void beginsert (); 11 | void lastinsert (); 12 | void randominsert(); 13 | void begin_delete(); 14 | void last_delete(); 15 | void random_delete(); 16 | void display(); 17 | void search(); 18 | void main () 19 | { 20 | int choice =0; 21 | while(choice != 9) 22 | { 23 | printf("\n\n*********Main Menu*********\n"); 24 | printf("\nChoose one option from the following list ...\n"); 25 | printf("\n===============================================\n"); 26 | printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete from Beginning\n 27 | 5.Delete from last\n6.Delete node after specified location\n7.Search for an element\n8.Show\n9.Exit\n"); 28 | printf("\nEnter your choice?\n"); 29 | scanf("\n%d",&choice); 30 | switch(choice) 31 | { 32 | case 1: 33 | beginsert(); 34 | break; 35 | case 2: 36 | lastinsert(); 37 | break; 38 | case 3: 39 | randominsert(); 40 | break; 41 | case 4: 42 | begin_delete(); 43 | break; 44 | case 5: 45 | last_delete(); 46 | break; 47 | case 6: 48 | random_delete(); 49 | break; 50 | case 7: 51 | search(); 52 | break; 53 | case 8: 54 | display(); 55 | break; 56 | case 9: 57 | exit(0); 58 | break; 59 | default: 60 | printf("Please enter valid choice.."); 61 | } 62 | } 63 | } 64 | void beginsert() 65 | { 66 | struct node *ptr; 67 | int item; 68 | ptr = (struct node *) malloc(sizeof(struct node *)); 69 | if(ptr == NULL) 70 | { 71 | printf("\nOVERFLOW"); 72 | } 73 | else 74 | { 75 | printf("\nEnter value\n"); 76 | scanf("%d",&item); 77 | ptr->data = item; 78 | ptr->next = head; 79 | head = ptr; 80 | printf("\nNode inserted"); 81 | } 82 | 83 | } 84 | void lastinsert() 85 | { 86 | struct node *ptr,*temp; 87 | int item; 88 | ptr = (struct node*)malloc(sizeof(struct node)); 89 | if(ptr == NULL) 90 | { 91 | printf("\nOVERFLOW"); 92 | } 93 | else 94 | { 95 | printf("\nEnter value?\n"); 96 | scanf("%d",&item); 97 | ptr->data = item; 98 | if(head == NULL) 99 | { 100 | ptr -> next = NULL; 101 | head = ptr; 102 | printf("\nNode inserted"); 103 | } 104 | else 105 | { 106 | temp = head; 107 | while (temp -> next != NULL) 108 | { 109 | temp = temp -> next; 110 | } 111 | temp->next = ptr; 112 | ptr->next = NULL; 113 | printf("\nNode inserted"); 114 | 115 | } 116 | } 117 | } 118 | void randominsert() 119 | { 120 | int i,loc,item; 121 | struct node *ptr, *temp; 122 | ptr = (struct node *) malloc (sizeof(struct node)); 123 | if(ptr == NULL) 124 | { 125 | printf("\nOVERFLOW"); 126 | } 127 | else 128 | { 129 | printf("\nEnter element value"); 130 | scanf("%d",&item); 131 | ptr->data = item; 132 | printf("\nEnter the location after which you want to insert "); 133 | scanf("\n%d",&loc); 134 | temp=head; 135 | for(i=0;inext; 138 | if(temp == NULL) 139 | { 140 | printf("\ncan't insert\n"); 141 | return; 142 | } 143 | 144 | } 145 | ptr ->next = temp ->next; 146 | temp ->next = ptr; 147 | printf("\nNode inserted"); 148 | } 149 | } 150 | void begin_delete() 151 | { 152 | struct node *ptr; 153 | if(head == NULL) 154 | { 155 | printf("\nList is empty\n"); 156 | } 157 | else 158 | { 159 | ptr = head; 160 | head = ptr->next; 161 | free(ptr); 162 | printf("\nNode deleted from the begining ...\n"); 163 | } 164 | } 165 | void last_delete() 166 | { 167 | struct node *ptr,*ptr1; 168 | if(head == NULL) 169 | { 170 | printf("\nlist is empty"); 171 | } 172 | else if(head -> next == NULL) 173 | { 174 | head = NULL; 175 | free(head); 176 | printf("\nOnly node of the list deleted ...\n"); 177 | } 178 | 179 | else 180 | { 181 | ptr = head; 182 | while(ptr->next != NULL) 183 | { 184 | ptr1 = ptr; 185 | ptr = ptr ->next; 186 | } 187 | ptr1->next = NULL; 188 | free(ptr); 189 | printf("\nDeleted Node from the last ...\n"); 190 | } 191 | } 192 | void random_delete() 193 | { 194 | struct node *ptr,*ptr1; 195 | int loc,i; 196 | printf("\n Enter the location of the node after which you want to perform deletion \n"); 197 | scanf("%d",&loc); 198 | ptr=head; 199 | for(i=0;inext; 203 | 204 | if(ptr == NULL) 205 | { 206 | printf("\nCan't delete"); 207 | return; 208 | } 209 | } 210 | ptr1 ->next = ptr ->next; 211 | free(ptr); 212 | printf("\nDeleted node %d ",loc+1); 213 | } 214 | void search() 215 | { 216 | struct node *ptr; 217 | int item,i=0,flag; 218 | ptr = head; 219 | if(ptr == NULL) 220 | { 221 | printf("\nEmpty List\n"); 222 | } 223 | else 224 | { 225 | printf("\nEnter item which you want to search?\n"); 226 | scanf("%d",&item); 227 | while (ptr!=NULL) 228 | { 229 | if(ptr->data == item) 230 | { 231 | printf("item found at location %d ",i+1); 232 | flag=0; 233 | } 234 | else 235 | { 236 | flag=1; 237 | } 238 | i++; 239 | ptr = ptr -> next; 240 | } 241 | if(flag==1) 242 | { 243 | printf("Item not found\n"); 244 | } 245 | } 246 | 247 | } 248 | 249 | void display() 250 | { 251 | struct node *ptr; 252 | ptr = head; 253 | if(ptr == NULL) 254 | { 255 | printf("Nothing to print"); 256 | } 257 | else 258 | { 259 | printf("\nprinting values . . . . .\n"); 260 | while (ptr!=NULL) 261 | { 262 | printf("\n%d",ptr->data); 263 | ptr = ptr -> next; 264 | } 265 | } 266 | } 267 | -------------------------------------------------------------------------------- /MagicNumber.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main () 5 | { 6 | int n, temp, rev = 0, digit, sum = 0; 7 | printf (" Enter a Number: \n"); 8 | scanf (" %d", &n); 9 | // assigning the number to temporary variable 10 | temp = n; 11 | // calculating the sum of digits 12 | while ( temp > 0) 13 | { 14 | // extracting digits and calculating the sum 15 | sum = sum + temp % 10; 16 | temp = temp / 10; 17 | } 18 | //storing the sum 19 | temp = sum; 20 | printf (" \n digit sum = %d", temp); 21 | 22 | // reverse sum of given digits 23 | while ( temp > 0) 24 | { 25 | rev = rev * 10 + temp % 10; 26 | temp /= 10; 27 | } 28 | printf (" \n The reverse of sum = %d", rev); 29 | printf (" \n The product of %d * %d = %d", sum, rev, rev * sum); 30 | // checking for magic number 31 | if ( rev * sum == n) 32 | printf (" \n %d is a Magic Number. ", n); 33 | else 34 | printf (" \n %d is not a Magic Number. ", n); 35 | return 0; 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Matrix Multiplication.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int a[3][3]; 5 | printf("Enter the number to array 'a':\n"); 6 | for(int i=0;i<3;i++) 7 | { 8 | for(int j=0;j<3;j++) 9 | { 10 | scanf("%d",&a[i][j]); 11 | } 12 | printf("\n"); 13 | } 14 | int b[3][3]; 15 | printf("Enter the number to array 'b':\n"); 16 | for(int i=0;i<3;i++) 17 | { 18 | for(int j=0;j<3;j++) 19 | { 20 | scanf("%d",&b[i][j]); 21 | } 22 | printf("\n"); 23 | } 24 | int m[3][3]; 25 | printf("The product of the two matrix is:\n"); 26 | for(int i=0;i<3;i++) 27 | { 28 | for(int j=0;j<3;j++) 29 | { 30 | m[i][j]=0; 31 | for(int k=0;k<3;k++) 32 | { 33 | m[i][j]=m[i][j]+a[i][k]*b[k][j]; 34 | } 35 | } 36 | } 37 | for(int i=0;i<3;i++) 38 | { 39 | for(int j=0;j<3;j++) 40 | { 41 | 42 | printf("%d ",m[i][j]); 43 | } 44 | printf("\n"); 45 | } 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Matrix_Chain_Multiplication.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Matrix Ai has dimension p[i-1] x p[i] for i = 1..n 5 | 6 | int MatrixChainMultiplication(int p[], int n) 7 | { 8 | int m[n][n]; 9 | int i, j, k, L, q; 10 | 11 | for (i=1; i 2 | 3 | int main() 4 | { 5 | int m, n; 6 | printf("Enter the rows of the matrix: "); 7 | scanf("%d", &m); 8 | printf("Enter the columns of the matrix: "); 9 | scanf("%d", &n); 10 | 11 | int arr[m][n]; 12 | printf("Enter the elements of the array:\n "); 13 | for (int i = 0; i < m; i++) 14 | for (int j = 0; j < n; j++) 15 | scanf("%d", &arr[i][j]); 16 | 17 | printf("The given matrix is: \n"); 18 | for (int i = 0; i < m; i++) 19 | { 20 | for (int j = 0; j < n; j++) 21 | printf("%d ", arr[i][j]); 22 | printf("\n"); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Minimum_jumps.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define ll long long 4 | 5 | void nobody(){ 6 | int n,flag=1; 7 | cin >> n; 8 | int arr[n]; 9 | for(int i=0;i> arr[i]; 11 | } 12 | int jmp=0,m=0,c=0; 13 | for(int i=0;i> t; 30 | while(t--){ 31 | nobody(); 32 | } 33 | } 34 | 35 | -------------------------------------------------------------------------------- /Pascal_Triangle.C: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() 4 | { 5 | int array[15][15], i, j, rows, num = 25, k; 6 | 7 | printf("\n Enter the number of rows:"); 8 | scanf("%d", &rows); 9 | for (i = 0; i < rows; i++) 10 | { 11 | for (k = num - 2 * i; k >= 0; k--) 12 | printf(" "); 13 | for (j = 0; j <= i; j++) 14 | { 15 | if (j == 0 || i == j) 16 | { 17 | array[i][j] = 1; 18 | } 19 | else 20 | { 21 | array[i][j] = array[i - 1][j - 1] + array[i - 1][j]; 22 | } 23 | printf("%4d", array[i][j]); 24 | } 25 | printf("\n"); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Program Files/:small_or_capital_letter.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void) 3 | { 4 | char letter; 5 | printf("Please enter a letter from English alphabet:"); 6 | scanf("%c", &letter); 7 | int ascii = letter; 8 | if(ascii >= 65 && ascii <=90) 9 | { 10 | printf("Uppercase letter!"); 11 | } 12 | else if(ascii >= 97 && ascii <= 122) 13 | { 14 | printf("Lowercase letter!"); 15 | } 16 | else 17 | { 18 | printf("Invalid input!"); 19 | } 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Program Files/README.md: -------------------------------------------------------------------------------- 1 | # README.md 2 | 3 | Add code files in Program files folder, after pull request #41 4 | -------------------------------------------------------------------------------- /Program Files/number_of_digits.c: -------------------------------------------------------------------------------- 1 | //a function that will take an integer as parameter and will return number of digits on it. 2 | 3 | #include 4 | 5 | int totalDigit(int); 6 | 7 | int main(void) 8 | { 9 | int n; 10 | printf("Please enter and integer:"); 11 | scanf("%i", &n); 12 | int count = totalDigit(n); 13 | printf("Total digit in the number : %i", count); 14 | 15 | return 0; 16 | } 17 | 18 | int totalDigit(int num) 19 | { 20 | int remainder, counter = 0; 21 | while(num != 0) 22 | { 23 | remainder = remainder % 10; 24 | counter++; 25 | num = num / 10; 26 | } 27 | return counter; 28 | } 29 | -------------------------------------------------------------------------------- /Quadratic.c: -------------------------------------------------------------------------------- 1 | // Write a C program to solve Quadratic Equation. 2 | 3 | #include 4 | 5 | int main(){ 6 | int a,b,c; 7 | printf("Enter the coieffient(a,b,c):"); 8 | scanf("%d %d %d", &a, &b, &c); 9 | if (((b*b)-(4*a*c))>=0){ 10 | if (((b*b)-(4*a*c))==0) 11 | printf("The roots of the quadratic equation are real and equal."); 12 | else 13 | printf("The roots of the quadratic equation are real but not equal."); 14 | } 15 | else 16 | printf("The roots of the quadratic equation are imaginary."); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Queue DS.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define MAX 5 5 | void enq(); 6 | void deq(); 7 | void display(); 8 | int a[MAX]; 9 | int FRONT=-1; 10 | int REAR=-1; 11 | void enq() 12 | { 13 | int ele; 14 | if(REAR==MAX-1) 15 | printf("QUEUE IS FULL.\n"); 16 | else 17 | { 18 | printf("Enter a Element to Insert:"); 19 | scanf("%d",&ele); 20 | if(FRONT==-1 && REAR==-1) 21 | { 22 | FRONT=REAR=0; 23 | a[REAR]=ele; 24 | printf("Value inserted.\n"); 25 | } 26 | else 27 | { 28 | REAR=REAR+1; 29 | a[REAR]=ele; 30 | printf("Value inserted.\n"); 31 | } 32 | } 33 | } 34 | void deq() 35 | { 36 | if(FRONT==-1) 37 | printf("QUEUE IS EMPTY.\n"); 38 | else 39 | { 40 | printf("Dequeued values: %d\n",a[FRONT]); 41 | if(FRONT==REAR) 42 | FRONT=REAR=-1; 43 | else 44 | FRONT=FRONT+1; 45 | } 46 | } 47 | void display() 48 | { 49 | int i; 50 | printf(" YOUR QUEUE:\n"); 51 | if(FRONT==-1) 52 | printf("Queue is Empty.\n"); 53 | else 54 | { 55 | for(i=FRONT;i<=REAR;i++) 56 | printf("%d\n",a[i]); 57 | } 58 | } 59 | void main() 60 | { 61 | int choice; 62 | while(1) 63 | { 64 | system("cls"); 65 | printf(" 1 ENQUEUE\n 2 DEQUEUE\n 3 DISPLAY\n 4 EXIT\n"); 66 | printf("Enter your choice:"); 67 | scanf("%d",&choice); 68 | switch(choice) 69 | { 70 | case 1:enq(); 71 | break; 72 | case 2:deq(); 73 | break; 74 | case 3:display(); 75 | break; 76 | case 4:exit(0); 77 | } 78 | getch(); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /Queue.c: -------------------------------------------------------------------------------- 1 | // Queue implementation in C 2 | 3 | #include 4 | #define SIZE 5 5 | 6 | void enQueue(int); 7 | void deQueue(); 8 | void display(); 9 | int items[SIZE], front = -1, rear = -1; 10 | int main() { 11 | //deQueue is not possible on empty queue 12 | deQueue(); 13 | //enQueue 5 elements 14 | enQueue(1); 15 | enQueue(2); 16 | enQueue(3); 17 | enQueue(4); 18 | enQueue(5); 19 | // 6th element can't be added to because the queue is full 20 | enQueue(6); 21 | display(); 22 | //deQueue removes element entered first i.e. 1 23 | deQueue(); 24 | //Now we have just 4 elements 25 | display(); 26 | return 0; 27 | } 28 | 29 | void enQueue(int value) { 30 | if (rear == SIZE - 1) 31 | printf("\nQueue is Full!!"); 32 | else { 33 | if (front == -1) 34 | front = 0; 35 | rear++; 36 | items[rear] = value; 37 | printf("\nInserted -> %d", value); 38 | } 39 | } 40 | 41 | void deQueue() { 42 | if (front == -1) 43 | printf("\nQueue is Empty!!"); 44 | else { 45 | printf("\nDeleted : %d", items[front]); 46 | front++; 47 | if (front > rear) 48 | front = rear = -1; 49 | } 50 | } 51 | 52 | // Function to print the queue 53 | void display() { 54 | if (rear == -1) 55 | printf("\nQueue is Empty!!!"); 56 | else { 57 | int i; 58 | printf("\nQueue elements are:\n"); 59 | for (i = front; i <= rear; i++) 60 | printf("%d ", items[i]); 61 | } 62 | printf("\n"); 63 | } 64 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 | 3 |
4 | 5 | HACKTOBERFEST IS FOR EVERYONE. WHETHER IT’S YOUR FIRST TIME—OR YOUR NINTH—IT’S ALMOST TIME TO HACK OUT FOUR PRISTINE PULL/MERGE REQUESTS AND COMPLETE YOUR MISSION FOR OPEN SOURCE. 6 | 7 | Go through this link and [Register](https://hacktoberfest.com/) here for Hacktoberfest and make four pull requests (PRs) between October 1st-31st to grab free SWAGS 🎁🎁🔥. 8 | 9 |
10 |

C Programming Basics

11 |
12 |
13 | C is a general-purpose computer programming language. It was created in the 1970s by Dennis Ritchie, and remains very widely used and influential. By design, C's features cleanly reflect the capabilities of the targeted CPUs. It has found lasting use in operating systems, device drivers, protocol stacks, though decreasingly for application software. C is commonly used on computer architectures that range from the largest supercomputers to the smallest microcontrollers and embedded systems. 14 | 15 |
16 |
17 | 18 | - C has a formal grammar specified by the C standard. Line endings are generally not significant in C; however, line boundaries do have significance during the preprocessing phase. Comments may appear either between the delimiters /* and */, or (since C99) following // until the end of the line. Comments delimited by /* and */ do not nest, and these sequences of characters are not interpreted as comment delimiters if they appear inside string or character literals. 19 | 20 | - C source files contain declarations and function definitions. Function definitions, in turn, contain declarations and statements. Declarations either define new types using keywords such as struct, union, and enum, or assign types to and perhaps reserve storage for new variables, usually by writing the type followed by the variable name. Keywords such as char and int specify built-in types. Sections of code are enclosed in braces ({ and }, sometimes called "curly brackets") to limit the scope of declarations and to act as a single statement for control structures. 21 | 22 |
23 | 24 | This project needs a ⭐️ from you. Don't forget to leave a star ⭐️ 25 | 26 | 27 |
28 |

Contributors

29 | 30 |
31 | -------------------------------------------------------------------------------- /ReportCardGeneration.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int i,a,b,c,d,e,n; 5 | float f,cgpa; 6 | char name[20]; 7 | printf("***********************************************************\n\n\n"); 8 | printf("Welcome to Student Report Card System\n"); 9 | 10 | 11 | printf("\n\n\n***********************************************************\n"); 12 | 13 | printf("Enter number of students in class\n"); 14 | scanf("%d",&n); 15 | printf("Enter the Roll Number of student whose report card is required\n"); 16 | scanf("%d",&i); 17 | switch (i) 18 | { 19 | case 1: 20 | { 21 | int a=99,b=98,c=97,d=96,e=96; 22 | f=(float)(a+b+c+d+e)/5; 23 | cgpa=(float)f/10; 24 | printf("Name :Alisha \n"); 25 | printf("The Marks obtained by student are as follows :-\n"); 26 | printf("%d\n%d\n%d\n%d\n%d\n",a,b,c,d,e); 27 | printf("The C.G.P.A obtained is %f \n",cgpa); 28 | printf("The grade obtained is \n"); 29 | if(cgpa>9) 30 | { 31 | printf("A+ grade\n"); 32 | printf("Excellent performance\nKeep that up!\n"); 33 | } 34 | else if(cgpa>8) 35 | { 36 | printf("A grade\n"); 37 | printf("Very Good\n Keep it up!\n"); 38 | } 39 | else if(cgpa>7) 40 | { 41 | printf("B+ grade\n"); 42 | printf("Good performance\n"); 43 | } 44 | else if(cgpa > 6) 45 | { 46 | printf("B Grade\n"); 47 | printf("Good performance. Can be improved!\n"); 48 | } 49 | else if(cgpa > 5) 50 | { 51 | printf("C Grade\n"); 52 | } 53 | else if(cgpa > 4) 54 | { 55 | printf("D Grade\n"); 56 | } 57 | else 58 | { 59 | printf("E grade"); 60 | printf("Failed, have to apply for re-test\n"); 61 | } 62 | break; 63 | } 64 | 65 | case 2: 66 | 67 | { 68 | int a=89,b=98,c=87,d=96,e=96; 69 | f=(float)(a+b+c+d+e)/5; 70 | cgpa=(float)f/10; 71 | printf("Name : Rocky Malvia\n"); 72 | printf("The Marks obtained by student are as follows :-\n"); 73 | printf("%d\n%d\n%d\n%d\n%d\n",a,b,c,d,e); 74 | printf("The C.G.P.A obtained is %f \n",cgpa); 75 | printf("The grade obtained is \n"); 76 | if(cgpa>9) 77 | { 78 | printf("A+ grade\n"); 79 | printf("Excellent performance\nKeep that up!\n"); 80 | } 81 | else if(cgpa>8) 82 | { 83 | printf("A grade\n"); 84 | printf("Very Good\n Keep it up!\n"); 85 | } 86 | else if(cgpa>7) 87 | { 88 | printf("B+ grade\n"); 89 | printf("Good performance\n"); 90 | } 91 | else if(cgpa > 6) 92 | { 93 | printf("B Grade\n"); 94 | printf("Good performance. Can be improved!\n"); 95 | } 96 | else if(cgpa > 5) 97 | { 98 | printf("C Grade\n"); 99 | } 100 | else if(cgpa > 4) 101 | { 102 | printf("D Grade\n"); 103 | } 104 | else 105 | { 106 | printf("E grade"); 107 | printf("Failed, have to apply for re-test\n"); 108 | } 109 | break; 110 | } 111 | case 3: 112 | 113 | 114 | { 115 | int a=89,b=88,c=87,d=96,e=96; 116 | f=(float)(a+b+c+d+e)/5; 117 | cgpa=(float)f/10; 118 | printf("Name : Twinkle Khajuria\n"); 119 | printf("The Marks obtained by student are as follows :-\n"); 120 | printf("%d\n%d\n%d\n%d\n%d\n",a,b,c,d,e); 121 | printf("The C.G.P.A obtained is %f \n",cgpa); 122 | printf("The grade obtained is \n"); 123 | if(cgpa>9) 124 | { 125 | printf("A+ grade\n"); 126 | printf("Excellent performance\nKeep that up!\n"); 127 | } 128 | else if(cgpa>8) 129 | { 130 | printf("A grade\n"); 131 | printf("Very Good\n Keep it up!\n"); 132 | } 133 | else if(cgpa>7) 134 | { 135 | printf("B+ grade\n"); 136 | printf("Good performance\n"); 137 | } 138 | else if(cgpa > 6) 139 | { 140 | printf("B Grade\n"); 141 | printf("Good performance. Can be improved!\n"); 142 | } 143 | else if(cgpa > 5) 144 | { 145 | printf("C Grade\n"); 146 | } 147 | else if(cgpa > 4) 148 | { 149 | printf("D Grade\n"); 150 | } 151 | else 152 | { 153 | printf("E grade"); 154 | printf("Failed, have to apply for re-test\n"); 155 | } 156 | break; 157 | } 158 | case 4: 159 | 160 | { 161 | int a=89,b=98,c=57,d=96,e=96; 162 | f=(float)(a+b+c+d+e)/5; 163 | cgpa=(float)f/10; 164 | printf("Name : Ayushi Sharma\n"); 165 | printf("The Marks obtained by student are as follows :-\n"); 166 | printf("%d\n%d\n%d\n%d\n%d\n",a,b,c,d,e); 167 | printf("The C.G.P.A obtained is %f \n",cgpa); 168 | printf("The grade obtained is \n"); 169 | if(cgpa>9) 170 | { 171 | printf("A+ grade\n"); 172 | printf("Excellent performance\nKeep that up!\n"); 173 | } 174 | else if(cgpa>8) 175 | { 176 | printf("A grade\n"); 177 | printf("Very Good\n Keep it up!\n"); 178 | } 179 | else if(cgpa>7) 180 | { 181 | printf("B+ grade\n"); 182 | printf("Good performance\n"); 183 | } 184 | else if(cgpa > 6) 185 | { 186 | printf("B Grade\n"); 187 | printf("Good performance. Can be improved!\n"); 188 | } 189 | else if(cgpa > 5) 190 | { 191 | printf("C Grade\n"); 192 | } 193 | else if(cgpa > 4) 194 | { 195 | printf("D Grade\n"); 196 | } 197 | else 198 | { 199 | printf("E grade"); 200 | printf("Failed, have to apply for re-test\n"); 201 | } 202 | break; 203 | } 204 | } 205 | 206 | if(i>=5) 207 | { 208 | for(i=5;i<=n;i++) 209 | { 210 | printf("Enter the name of student having Roll number %d\n",i); 211 | scanf("%s",&name); 212 | printf("enter the marks of student in 5 subject\n"); 213 | scanf("%d\t%d\t%d\t%d\t%d",&a,&b,&c,&d,&e); 214 | float f=(a+b+c+d+e)/(float)5; 215 | float cgpa = f/(float)10; 216 | printf("**********************************************\n"); 217 | printf("Name: %s\n",name); 218 | printf("Marks in different subjects are as follows:-\n"); 219 | printf("%d\n%d\n%d\n%d\n%d\n",a,b,c,d,e,f); 220 | printf("The C.G.P.A obtained by student is : %f \n",cgpa); 221 | printf("The Grade obtained is : "); 222 | if(cgpa>9) 223 | { 224 | printf("A+ grade\n"); 225 | printf("Excellent performance\nKeep that up!\n"); 226 | } 227 | else if(cgpa>8) 228 | { 229 | printf("A grade\n"); 230 | printf("Very Good\n Keep it up!\n"); 231 | } 232 | else if(cgpa>7) 233 | { 234 | printf("B+ grade\n"); 235 | printf("Good performance\n"); 236 | } 237 | else if(cgpa > 6) 238 | { 239 | printf("B Grade\n"); 240 | printf("Good performance. Can be improved!\n"); 241 | } 242 | else if(cgpa > 5) 243 | { 244 | printf("C Grade\n"); 245 | } 246 | else if(cgpa > 4) 247 | { 248 | printf("D Grade\n"); 249 | } 250 | else 251 | { 252 | printf("E grade"); 253 | printf("Failed, have to apply for re-test\n"); 254 | } 255 | printf("----------------------------------------\n"); 256 | } 257 | } 258 | return 0; 259 | } 260 | -------------------------------------------------------------------------------- /ReverseInt.c: -------------------------------------------------------------------------------- 1 | #include 2 | //Given a 5-digit integer number. 3 | //Write a program to print it in reverse order 4 | int main() 5 | { 6 | int number,i; 7 | printf("Enter the number:"); 8 | scanf("%d",&number); 9 | 10 | while(number!=0){ 11 | i=number%10; 12 | printf("%d",i); 13 | number/=10; 14 | } 15 | printf(" :is the reversed number"); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /ReverseLinkedList.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | 6 | struct Node { 7 | int data; 8 | struct Node* next; 9 | }; 10 | 11 | 12 | static void reverse(struct Node** head_ref) 13 | { 14 | struct Node* prev = NULL; 15 | struct Node* current = *head_ref; 16 | struct Node* next = NULL; 17 | while (current != NULL) { 18 | 19 | next = current->next; 20 | 21 | 22 | current->next = prev; 23 | 24 | 25 | prev = current; 26 | current = next; 27 | } 28 | *head_ref = prev; 29 | } 30 | 31 | 32 | void push(struct Node** head_ref, int new_data) 33 | { 34 | struct Node* new_node 35 | = (struct Node*)malloc(sizeof(struct Node)); 36 | new_node->data = new_data; 37 | new_node->next = (*head_ref); 38 | (*head_ref) = new_node; 39 | } 40 | 41 | 42 | void printList(struct Node* head) 43 | { 44 | struct Node* temp = head; 45 | while (temp != NULL) { 46 | printf("%d ", temp->data); 47 | temp = temp->next; 48 | } 49 | } 50 | 51 | 52 | int main() 53 | { 54 | struct Node* head = NULL; 55 | 56 | push(&head, 20); 57 | push(&head, 4); 58 | push(&head, 15); 59 | push(&head, 85); 60 | 61 | printf("Given linked list\n"); 62 | printList(head); 63 | reverse(&head); 64 | printf("\nReversed linked list \n"); 65 | printList(head); 66 | getchar(); 67 | } 68 | -------------------------------------------------------------------------------- /ShortestJobFirstScheduling.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void swap(int *a , int *b){ 4 | int temp = *a; 5 | *a = *b; 6 | *b = temp; 7 | } 8 | 9 | int main(){ 10 | int n,p[10],bt[10],tat[10],wt[10],i,j; 11 | float avg_tat=0,avg_wt=0; 12 | 13 | printf("Enter no of processes: "); 14 | scanf("%d",&n); 15 | 16 | printf("Enter burst time of each processes: "); 17 | 18 | for(i=0;ibt[j+1]){ 25 | swap(&bt[j],&bt[j+1]); 26 | swap(&p[j],&p[j+1]); 27 | } 28 | } 29 | } 30 | for(i=0;i 2 | 3 | int main() 4 | { 5 | char string_1[20]="Hello guys"; 6 | char string_2[20]; 7 | printf("String1: %s\n",string_1); 8 | for (int i=0; i<10;i++) 9 | scanf("%c ",&string_2[i]); 10 | printf("String2: %s",string_2); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /Sphere_in_tetrahadron_problem.c: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | double area(double a,double b,double c) 4 | { 5 | double s=(a+b+c)/2.0; 6 | return sqrt(s*(s-a)*(s-b)*(s-c)); 7 | } 8 | int main() { 9 | int t;scanf("%d",&t); 10 | while(t--) 11 | { 12 | double u,v,w,U,V,W; 13 | cin>>u>>v>>w>>W>>V>>U; 14 | double u1=v*v+w*w-U*U; 15 | double v1=u*u+w*w-V*V; 16 | double w1=u*u+v*v-W*W; 17 | double vol=(sqrt(4*u*u*v*v*w*w-u*u*u1*u1-v*v*v1*v1-w*w*w1*w1+u1*v1*w1))/12; 18 | double ar=0; 19 | ar=area(U,V,W)+area(v,u,W)+area(w,v,U)+area(u,w,V); 20 | double volt=3*vol/ar; 21 | cout< 2 | #include 3 | #include 4 | 5 | struct stkarray *createstack(int); 6 | void push(struct stkarray *); 7 | void pop(struct stkarray *); 8 | void display(struct stkarray *stk); 9 | 10 | struct stkarray 11 | { 12 | int top; 13 | int capacity; 14 | int *array; 15 | }; 16 | 17 | struct stkarray *createstack(int cap) 18 | { 19 | struct stkarray *stk; 20 | stk = (struct stkarray*)malloc(sizeof(struct stkarray)); 21 | stk->top = -1; 22 | stk->capacity = cap; 23 | stk->array = (int*)malloc(sizeof(int) * stk->capacity); 24 | return (stk); 25 | } 26 | 27 | void push(struct stkarray *stk) 28 | { 29 | int ele; 30 | if (stk->top == stk->capacity - 1) 31 | printf("Stack is Overflow.\n"); 32 | else 33 | { 34 | printf("Enter Element to Insert: "); 35 | scanf("%d", &ele); 36 | stk->top++; 37 | stk->array[stk->top] = ele; 38 | printf("Element Insertsd.\n"); 39 | } 40 | } 41 | 42 | void pop(struct stkarray *stk) 43 | { 44 | int ele; 45 | if (stk->top == -1) 46 | printf("Stack is Underflow.\n"); 47 | else 48 | { 49 | ele = stk->array[stk->top]; 50 | stk->top--; 51 | printf("Poped value: %d", ele); 52 | } 53 | } 54 | 55 | void display(struct stkarray *stk) 56 | { 57 | int i; 58 | if (stk->top == -1) 59 | printf("Stack is Underflow.\n"); 60 | else 61 | { 62 | printf(" YOUR STACK:\n"); 63 | for (i = 0; i <= stk->top; i++) 64 | printf("%d\n", stk->array[i]); 65 | } 66 | } 67 | 68 | void main() 69 | { 70 | struct stkarray *stk; 71 | int capacity, choice; 72 | printf("Enter size of Stack:"); 73 | scanf("%d", &capacity); 74 | stk = createstack(capacity); 75 | while (1) 76 | { 77 | system("cls"); 78 | printf(" 1 PUSH\n 2 POP\n 3 DISPLAY\n 4 EXIT"); 79 | printf("\nEnter your Choice: "); 80 | scanf("%d", &choice); 81 | switch (choice) 82 | { 83 | case 1: push(stk); 84 | break; 85 | case 2: pop(stk); 86 | break; 87 | case 3: display(stk); 88 | break; 89 | case 4: exit(0); 90 | break; 91 | default: printf("INVALID CHOICE,\n"); 92 | } 93 | getch(); 94 | } 95 | } 96 | 97 | -------------------------------------------------------------------------------- /SwitchCases.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a=1; 5 | switch (a << (2+a)){ 6 | default: 7 | printf("1"); 8 | case 4: 9 | printf("2"); 10 | case 5: 11 | printf("3"); 12 | case 8: 13 | printf("4"); 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /TicTacToe.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int board[10] = {2,2,2,2,2,2,2,2,2,2}; 7 | int turn = 1,flag = 0; 8 | int player,comp; 9 | 10 | void menu(); 11 | void go(int n); 12 | void start_game(); 13 | void check_draw(); 14 | void draw_board(); 15 | void player_first(); 16 | void put_X_O(char ch,int pos); 17 | COORD coord= {0,0}; // this is global variable 18 | //center of axis is set to the top left cornor of the screen 19 | void gotoxy(int x,int y) 20 | { 21 | coord.X=x; 22 | coord.Y=y; 23 | SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord); 24 | } 25 | 26 | void main() 27 | { 28 | system("cls"); 29 | menu(); 30 | getch(); 31 | 32 | } 33 | 34 | void menu() 35 | { 36 | int choice; 37 | system("cls"); 38 | printf("\n--------MENU--------"); 39 | printf("\n1 : Play with X"); 40 | printf("\n2 : Play with O"); 41 | printf("\n3 : Exit"); 42 | printf("\nEnter your choice:>"); 43 | scanf("%d",&choice); 44 | turn = 1; 45 | switch (choice) 46 | { 47 | case 1: 48 | player = 1; 49 | comp = 0; 50 | player_first(); 51 | break; 52 | case 2: 53 | player = 0; 54 | comp = 1; 55 | start_game(); 56 | break; 57 | case 3: 58 | exit(1); 59 | default: 60 | menu(); 61 | } 62 | } 63 | 64 | int make2() 65 | { 66 | if(board[5] == 2) 67 | return 5; 68 | if(board[2] == 2) 69 | return 2; 70 | if(board[4] == 2) 71 | return 4; 72 | if(board[6] == 2) 73 | return 6; 74 | if(board[8] == 2) 75 | return 8; 76 | return 0; 77 | } 78 | 79 | int make4() 80 | { 81 | if(board[1] == 2) 82 | return 1; 83 | if(board[3] == 2) 84 | return 3; 85 | if(board[7] == 2) 86 | return 7; 87 | if(board[9] == 2) 88 | return 9; 89 | return 0; 90 | } 91 | 92 | int posswin(int p) 93 | { 94 | // p==1 then X p==0 then O 95 | int i; 96 | int check_val,pos; 97 | 98 | if(p == 1) 99 | check_val = 18; 100 | else 101 | check_val = 50; 102 | 103 | i = 1; 104 | while(i<=9)//row check 105 | { 106 | if(board[i] * board[i+1] * board[i+2] == check_val) 107 | { 108 | if(board[i] == 2) 109 | return i; 110 | if(board[i+1] == 2) 111 | return i+1; 112 | if(board[i+2] == 2) 113 | return i+2; 114 | } 115 | i+=3; 116 | } 117 | 118 | i = 1; 119 | while(i<=3)//column check 120 | { 121 | if(board[i] * board[i+3] * board[i+6] == check_val) 122 | { 123 | if(board[i] == 2) 124 | return i; 125 | if(board[i+3] == 2) 126 | return i+3; 127 | if(board[i+6] == 2) 128 | return i+6; 129 | } 130 | i++; 131 | } 132 | 133 | if(board[1] * board[5] * board[9] == check_val) 134 | { 135 | if(board[1] == 2) 136 | return 1; 137 | if(board[5] == 2) 138 | return 5; 139 | if(board[9] == 2) 140 | return 9; 141 | } 142 | 143 | if(board[3] * board[5] * board[7] == check_val) 144 | { 145 | if(board[3] == 2) 146 | return 3; 147 | if(board[5] == 2) 148 | return 5; 149 | if(board[7] == 2) 150 | return 7; 151 | } 152 | return 0; 153 | } 154 | 155 | void go(int n) 156 | { 157 | if(turn % 2) 158 | board[n] = 3; 159 | else 160 | board[n] = 5; 161 | turn++; 162 | } 163 | 164 | void player_first() 165 | { 166 | int pos; 167 | 168 | check_draw(); 169 | draw_board(); 170 | gotoxy(30,18); 171 | printf("Your Turn :> "); 172 | scanf("%d",&pos); 173 | 174 | if(board[pos] != 2) 175 | player_first(); 176 | 177 | if(pos == posswin(player)) 178 | { 179 | go(pos); 180 | draw_board(); 181 | gotoxy(30,20); 182 | //textcolor(128+RED); 183 | printf("Player Wins"); 184 | getch(); 185 | exit(0); 186 | } 187 | 188 | go(pos); 189 | draw_board(); 190 | start_game(); 191 | } 192 | 193 | void start_game() 194 | { 195 | // p==1 then X p==0 then O 196 | if(posswin(comp)) 197 | { 198 | go(posswin(comp)); 199 | flag = 1; 200 | } 201 | else if(posswin(player)) 202 | go(posswin(player)); 203 | else if(make2()) 204 | go(make2()); 205 | else 206 | go(make4()); 207 | draw_board(); 208 | 209 | if(flag) 210 | { 211 | gotoxy(30,20); 212 | //textcolor(128+RED); 213 | printf("Computer wins"); 214 | getch(); 215 | } 216 | else 217 | player_first(); 218 | } 219 | 220 | void check_draw() 221 | { 222 | if(turn > 9) 223 | { 224 | gotoxy(30,20); 225 | //textcolor(128+RED); 226 | printf("Game Draw"); 227 | getch(); 228 | exit(0); 229 | } 230 | } 231 | 232 | void draw_board() 233 | { 234 | int j; 235 | 236 | for(j=9; j<17; j++) 237 | { 238 | gotoxy(35,j); 239 | printf("| |"); 240 | } 241 | gotoxy(28,11); 242 | printf("-----------------------"); 243 | gotoxy(28,14); 244 | printf("-----------------------"); 245 | 246 | for(j=1; j<10; j++) 247 | { 248 | if(board[j] == 3) 249 | put_X_O('X',j); 250 | else if(board[j] == 5) 251 | put_X_O('O',j); 252 | } 253 | } 254 | 255 | void put_X_O(char ch,int pos) 256 | { 257 | int m; 258 | int x = 31, y = 10; 259 | 260 | m = pos; 261 | 262 | if(m > 3) 263 | { 264 | while(m > 3) 265 | { 266 | y += 3; 267 | m -= 3; 268 | } 269 | } 270 | if(pos % 3 == 0) 271 | x += 16; 272 | else 273 | { 274 | pos %= 3; 275 | pos--; 276 | while(pos) 277 | { 278 | x+=8; 279 | pos--; 280 | } 281 | } 282 | gotoxy(x,y); 283 | printf("%c",ch); 284 | } 285 | -------------------------------------------------------------------------------- /Tower_of_Hanoi.c: -------------------------------------------------------------------------------- 1 | //Tower of Hanoi using Recursion 2 | #include 3 | #include 4 | int move(int n, char source, char dest, char aux) { 5 | if(n==1) 6 | printf("Move disc from %c to %c \n", source, dest); 7 | else { 8 | move(n-1, source, aux, dest); 9 | move(1, source, dest, aux); 10 | move(n-1, aux, dest, source); 11 | } 12 | } 13 | int main() { 14 | int n, r; 15 | char A, B, C; 16 | printf("Enter the number of disc: \n"); 17 | scanf("%d", &n); 18 | move(n, 'A', 'B', 'C'); 19 | printf("\n"); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Travelling_SalesmanProblem.c: -------------------------------------------------------------------------------- 1 | // Travelling Salesman Problem in C 2 | #include 3 | int matrix[25][25], visited_cities[10], limit, cost = 0; 4 | 5 | int tsp(int c) 6 | { 7 | int count, nearest_city = 999; 8 | int minimum = 999, temp; 9 | for(count = 0; count < limit; count++) 10 | { 11 | if((matrix[c][count] != 0) && (visited_cities[count] == 0)) 12 | { 13 | if(matrix[c][count] < minimum) 14 | { 15 | minimum = matrix[count][0] + matrix[c][count]; 16 | } 17 | temp = matrix[c][count]; 18 | nearest_city = count; 19 | } 20 | } 21 | if(minimum != 999) 22 | { 23 | cost = cost + temp; 24 | } 25 | return nearest_city; 26 | } 27 | 28 | void minimum_cost(int city) 29 | { 30 | int nearest_city; 31 | visited_cities[city] = 1; 32 | printf("%d ", city + 1); 33 | nearest_city = tsp(city); 34 | if(nearest_city == 999) 35 | { 36 | nearest_city = 0; 37 | printf("%d", nearest_city + 1); 38 | cost = cost + matrix[city][nearest_city]; 39 | return; 40 | } 41 | minimum_cost(nearest_city); 42 | } 43 | 44 | int main() 45 | { 46 | int i, j; 47 | printf("Enter Total Number of Cities:\t"); 48 | scanf("%d", &limit); 49 | printf("\nEnter Cost Matrix\n"); 50 | for(i = 0; i < limit; i++) 51 | { 52 | printf("\nEnter %d Elements in Row[%d]\n", limit, i + 1); 53 | for(j = 0; j < limit; j++) 54 | { 55 | scanf("%d", &matrix[i][j]); 56 | } 57 | visited_cities[i] = 0; 58 | } 59 | printf("\nEntered Cost Matrix\n"); 60 | for(i = 0; i < limit; i++) 61 | { 62 | printf("\n"); 63 | for(j = 0; j < limit; j++) 64 | { 65 | printf("%d ", matrix[i][j]); 66 | } 67 | } 68 | printf("\n\nPath:\t"); 69 | minimum_cost(0); 70 | printf("\n\nMinimum Cost: \t"); 71 | printf("%d\n", cost); 72 | return 0; 73 | } 74 | /* 75 | Output: 76 | Enter Total Number of Cities: 4 77 | 78 | Enter Cost Matrix 79 | 80 | Enter 4 Elements in Row[1] 81 | 1 2 3 4 82 | 83 | Enter 4 Elements in Row[2] 84 | 5 6 7 8 85 | 86 | Enter 4 Elements in Row[3] 87 | 3 4 5 6 88 | 89 | Enter 4 Elements in Row[4] 90 | 9 8 4 3 91 | 92 | Entered Cost Matrix 93 | 94 | 1 2 3 4 95 | 5 6 7 8 96 | 3 4 5 6 97 | 9 8 4 3 98 | 99 | Path: 1 4 3 2 1 100 | 101 | Minimum Cost: 17 102 | */ 103 | -------------------------------------------------------------------------------- /TriangleFibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(){ 4 | int a=0,b=1,i,c,n,j; 5 | printf("Enter the height of the triangle: \n"); 6 | scanf("%d",&n); 7 | for(i=1;i<=n;i++) 8 | { 9 | a=0; 10 | b=1; 11 | printf("%d\t",b); 12 | for(j=1;j 2 | int main(){ 3 | int n=7; 4 | int first=1; 5 | int second=3; 6 | int third=first+second; 7 | printf("%d %d %d ",first,second,third); 8 | for (int i=0; i 2 | int main() 3 | { 4 | int a[10000],b[10000],i,j,n,c=0 ; 5 | printf("Enter size of the array : "); 6 | scanf("%d", &n); 7 | printf("Enter elements in array : "); 8 | for(i=0; i 2 | int main() { 3 | char c; 4 | int lowercase_vowel, uppercase_vowel; 5 | printf("Enter an alphabet: "); 6 | scanf("%c", &c); 7 | 8 | // evaluates to 1 if variable c is a lowercase vowel 9 | lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); 10 | 11 | // evaluates to 1 if variable c is a uppercase vowel 12 | uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'); 13 | 14 | // evaluates to 1 (true) if c is a vowel 15 | if (lowercase_vowel || uppercase_vowel) 16 | printf("%c is a vowel.", c); 17 | else 18 | printf("%c is a consonant.", c); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Window shutdown in C .C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | printf("window will shut down"); 7 | 8 | char ch; 9 | 10 | printf("Do you want to shutdown your pc now (y/n)?"); 11 | scanf("%c", &ch); 12 | 13 | if(ch == 'y'|| ch == 'Y') 14 | { /* 15 | 16 | */ 17 | system("C:\\WINDOWS\\System32\\shutdown /s"); 18 | } 19 | 20 | printf("\n\n\t\t\tCoding is Fun !\n\n\n"); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /area_of_triangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { float b ,h, area; 4 | b= 5; 5 | h= 13; 6 | area = (b*h) / 2 ; 7 | 8 | printf("\n\n Area of Triangle is: %f",area); 9 | return (0); 10 | } -------------------------------------------------------------------------------- /areaoftriangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 4 | Write a program to read base & altitude of a triangle & print its area 5 | */ 6 | 7 | int main() 8 | { 9 | int base,altitude,area; 10 | printf("Enter base and altitude:"); 11 | scanf("%d%d",&base,&altitude); 12 | area=0.5*base*altitude; 13 | printf("The area of triangle is: %d", area); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /arfunction.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int a,b,i=0; 5 | scanf("%d\t%d",&a,&b); 6 | while(a<=b) 7 | { 8 | a*=3; 9 | b*=2; 10 | i++; 11 | } 12 | printf("%d",i); 13 | return(0); 14 | } 15 | 16 | 17 | -------------------------------------------------------------------------------- /array_2d_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int a[3][3], i, j; 7 | int *p = a, temp; 8 | 9 | printf("Enter 9 Elements: "); 10 | for (i = 0; i < 3; i++) 11 | for (j = 0; j < 3; j++) 12 | scanf("%d", &a[i][j]); 13 | 14 | printf("Given 2D Array:\n"); 15 | for (i = 0; i < 3; i++) 16 | { 17 | for (j = 0; j < 3; j++) 18 | { 19 | printf("%d", a[i][j]); 20 | } 21 | printf("\n"); 22 | } 23 | 24 | for (i = 0; i < 3; i++) 25 | { 26 | for (j = i + 1; j < 3; j++) 27 | { 28 | if (a[i][j] > a[i][j + 1]) 29 | { 30 | temp = a[i][j]; 31 | a[i][j] = a[i][j + 1]; 32 | a[i][j + 1] = temp; 33 | } 34 | } 35 | printf("\n"); 36 | } 37 | 38 | printf("Sorted 2D Array:\n"); 39 | for (i = 0; i < 3; i++) 40 | { 41 | for (j = 0; j < 3; j++) 42 | { 43 | printf("%d", a[i][j]); 44 | } 45 | printf("\n"); 46 | } 47 | 48 | return 0; 49 | } -------------------------------------------------------------------------------- /array_2d_sum.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void main() 4 | { 5 | int a[3][3],i,j,sum=0; 6 | printf("Enter 9 Elements: "); 7 | for(i=0;i<3;i++) 8 | { 9 | for(j=0;j<3;j++) 10 | { 11 | scanf("%d",&a[i][j]); 12 | } 13 | } 14 | for(i=0;i<3;i++) 15 | { 16 | for(j=0;j<3;j++) 17 | { 18 | sum=sum+a[i][j]; 19 | } 20 | } 21 | printf("Sum of All Elements in Array = %d",sum); 22 | } -------------------------------------------------------------------------------- /array_2d_transposition.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void transpose(int *, int *, int, int); 4 | void main() 5 | { 6 | int a[10][10], R, C, i, j; 7 | int b[10][10], temp; 8 | printf("Enter Number of Rows: "); 9 | scanf("%d", &R); 10 | printf("Enter Number of Columns: "); 11 | scanf("%d", &C); 12 | printf("Enter %d Elements: ", R * C); 13 | for (i = 0; i < R; i++) 14 | for (j = 0; j < C; j++) 15 | scanf("%d", &a[i][j]); 16 | transpose(a, b, R, C); 17 | printf("Transpose of Matrix:\n"); 18 | for (i = 0; i < R; i++) 19 | { 20 | for (j = 0; j < C; j++) 21 | { 22 | printf("%d ", b[i][j]); 23 | } 24 | printf("\n"); 25 | } 26 | getch(); 27 | } 28 | void transpose(int *src, int *dest, int rows, int cols) 29 | { 30 | int i, j; 31 | for (i = 0; i < rows; i++) 32 | { 33 | for (j = 0; j < cols; j++) 34 | { 35 | dest[] = src[]; 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /bankers_algorithm.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10], safeSequence[10]; 5 | int p, r, i, j, process, count; 6 | count = 0; 7 | 8 | // read no of processes(p0,p1,p2,p3) 9 | printf("Enter the no of processes : "); 10 | scanf("%d", &p); 11 | 12 | //read no of resources (A,B,C,D) 13 | printf("\n\nEnter the no of resources : "); 14 | scanf("%d", &r); 15 | 16 | //read alloc (for p processes and r resources) 17 | printf("\n\nEnter the allocation for each process : "); 18 | for(i = 0; i < p; i++) 19 | { 20 | printf("\nFor process %d : ",i + 1); 21 | for(j = 0; j < r; j++) 22 | scanf("%d", &alloc[i][j]); 23 | } 24 | 25 | //read max (for p processes and r resources) 26 | printf("\n\nEnter the Max Matrix for each process : "); 27 | for(i = 0; i < p; i++) 28 | { 29 | printf("\nFor process %d : ", i + 1); 30 | for(j = 0; j < r; j++) 31 | scanf("%d", &Max[i][j]); 32 | } 33 | 34 | //read avail (for p processes and r resources) 35 | printf("\n\nEnter the Available Resources : "); 36 | for(i = 0; i < r; i++) 37 | scanf("%d", &avail[i]); 38 | 39 | //calc need (for p processes and r resources) 40 | for(i = 0; i < p; i++) 41 | for(j = 0; j < r; j++) 42 | need[i][j] = Max[i][j] - alloc[i][j]; 43 | 44 | // set all completed proccesses as 0 45 | for(i = 0; i< p; i++) 46 | completed[i] = 0; 47 | 48 | // do while count !=process & process!=-1 49 | do 50 | { 51 | // process no i will be assigned to 'process' variable .So initialized as -1 52 | process = -1; 53 | 54 | // for each process p , if completion of p is 0 ,set process to i 55 | for(i = 0; i < p; i++) 56 | { 57 | if(completed[i] == 0) 58 | { 59 | process = i ; 60 | 61 | //for each resource r , if need > avail (process not selected),set process -1 62 | for(j = 0; j < r; j++) 63 | { 64 | 65 | if(need[i][j] > avail[j]) 66 | { 67 | process = -1; 68 | break; 69 | } 70 | } 71 | } 72 | // break loop if selected (process!=-1) 73 | if(process != -1) 74 | break; 75 | } 76 | 77 | // if selected 78 | if(process != -1) 79 | { 80 | //print 'process +1 selected' (if p=3 ; i=0,1,2,3 ; if i = 2 selected ,3rd process is selected) 81 | printf("\nProcess %d runs to completion!", process + 1); 82 | 83 | // process stored to safe sequence matrix 84 | safeSequence[count] = process + 1; 85 | count++; 86 | 87 | //for each resource r , 88 | for(j = 0; j < r; j++) 89 | { 90 | //new alloc = alloc +avail (not i but selected i which is 'process') 91 | avail[j] += alloc[process][j]; 92 | // alloc & max = 0 ,completion of 'process' =1 93 | alloc[process][j] = 0; 94 | Max[process][j] = 0; 95 | completed[process] = 1; 96 | } 97 | } 98 | }while(count != p && process != -1); 99 | 100 | // if all process selected ,ie count ==p 101 | if(count == p) 102 | { 103 | // print safe state 104 | printf("\nThe system is in a safe state!!\n"); 105 | // print safe sequence [i] 106 | printf("Safe Sequence : < "); 107 | for( i = 0; i < p; i++) 108 | printf("%d ", safeSequence[i]); 109 | printf(">\n"); 110 | } 111 | 112 | // else print unsafe 113 | else 114 | printf("\nThe system is in an unsafe state!!"); 115 | } -------------------------------------------------------------------------------- /bubble_sort.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | 5 | // perform the bubble sort 6 | void bubbleSort(int array[], int size) { 7 | // loop to access each array element 8 | int f=0; 9 | for (int step = 0; step < size - 1; ++step) { 10 | // loop to compare array elements 11 | for (int i = 0; i < size - step - 1; ++i) { 12 | if (array[i] > array[i + 1]) { 13 | f=1; 14 | int temp = array[i]; 15 | array[i] = array[i + 1]; 16 | array[i + 1] = temp; 17 | } 18 | } 19 | if(f==0) 20 | return; 21 | } 22 | } 23 | 24 | // print array 25 | void printArray(int array[], int size) { 26 | for (int i = 0; i < size; ++i) { 27 | printf("%d ", array[i]); 28 | } 29 | printf("\n"); 30 | } 31 | 32 | int main() { 33 | int data[] = {-2, 45, 0, 11, -9}; 34 | 35 | // find the array's length 36 | int size = sizeof(data) / sizeof(data[0]); 37 | 38 | bubbleSort(data, size); 39 | 40 | printf("Sorted Array in Ascending Order:\n"); 41 | printArray(data, size); 42 | } 43 | -------------------------------------------------------------------------------- /checlArrow/checkArrow.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int chr1, chr2; 5 | printf("Press any arrow key...\n"); 6 | chr1 = getch(); 7 | if (chr1 == 0xE0) //to check scroll key interrupt 8 | { 9 | chr2 = getch(); //to read arrow key 10 | switch(chr2) 11 | { 12 | case 72: printf("UP ARROW KEY PRESSED\n"); 13 | break; 14 | case 80: printf("DOWN ARROW KEY PRESSED\n"); 15 | break; 16 | case 75: printf("LEFT ARROW KEY PRESSED\n"); 17 | break; 18 | case 77: printf("RIGHT ARROW KEY PRESSED\n"); 19 | break; 20 | default: printf("OTHER KEY PRESSED: %d %d\n", chr1, chr2); 21 | break; 22 | }; 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /coinsCS50.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | float change; 8 | int rchange,coins=0; 9 | do 10 | { 11 | change=get_float("Change owed : "); 12 | }while(change<0); 13 | 14 | rchange=round(change*100); 15 | while(rchange>=25) 16 | { 17 | rchange=rchange-25; 18 | coins++; 19 | } 20 | while(rchange>=10) 21 | { 22 | rchange=rchange-10; 23 | coins++; 24 | } 25 | while(rchange>=5) 26 | { 27 | rchange=rchange-5; 28 | coins++; 29 | } 30 | while(rchange>=1 && rchange>0) 31 | { 32 | rchange=rchange-1; 33 | coins++; 34 | } 35 | printf("%d\n",coins); 36 | } -------------------------------------------------------------------------------- /conditionals.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int a=0, b=1, c=2, d=4, z; 4 | int x=3&&4; 5 | printf("%d\n",x); 6 | if (z=a|b|c&&d){printf("Tennis\n");} 7 | else{printf("Football\n");} 8 | printf("%d",z); 9 | return 0;} 10 | -------------------------------------------------------------------------------- /date.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int c_date, c_month, c_year; 4 | scanf("%d %d %d",&c_date,&c_month,&c_year); 5 | int n_date, n_month, n_year; 6 | n_year=c_year; 7 | if (c_month==1 ||c_month==3 ||c_month==5 || c_month==7 || c_month==8 || c_month ==10 ||(c_month==12 && c_date<30)){ 8 | if (c_date<=30){ 9 | n_date=c_date+1; 10 | n_month=c_month; 11 | } 12 | else{ 13 | n_date=1; 14 | n_month=c_month+1; 15 | } 16 | 17 | } 18 | 19 | else if (c_month==4 ||c_month==6 ||c_month==9 || c_month==11){ 20 | if (c_date<=29){ 21 | n_date=c_date+1; 22 | n_month=c_month; 23 | } 24 | else{ 25 | n_date=1; 26 | n_month=c_month+1; 27 | } 28 | 29 | } 30 | 31 | else if (c_month==2){ 32 | if (c_date<=27){ 33 | n_date=c_date+1; 34 | } 35 | else{ 36 | if ((c_year%4==0 &&c_year%100==0)||(c_year%400==0)){ 37 | n_date=c_date+1; 38 | n_month=c_month; 39 | } 40 | else{ 41 | n_date=1; 42 | n_month=c_month+1; 43 | } 44 | } 45 | 46 | } 47 | 48 | else if (c_month==12 && c_date==31){ 49 | n_date=1; 50 | n_month=1; 51 | n_year=c_year+1; 52 | } 53 | printf("%d %d %d",n_date,n_month,n_year); 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /deathyear.c: -------------------------------------------------------------------------------- 1 | // This program will give you output of after how many years you will die according to your contient choice 2 | 3 | #include 4 | void main() 5 | { 6 | int ch,CH; 7 | float age,y=0; 8 | printf("Enter your age:\n"); 9 | scanf("%f",&age); 10 | 11 | printf("choose your country for 1.ASIA 2.AFRICA 3.NORTH AMERICA 4.SOUTH AMERICA 5.EUROPE 6.AUSTRALIA\n"); 12 | scanf("%d",&CH); 13 | 14 | switch(CH) 15 | { 16 | case 1: 17 | { 18 | printf("Enter your choice for 1.male 2.female\n"); 19 | scanf("%d",&ch); 20 | switch(ch) 21 | { 22 | case 1: 23 | { 24 | y=75.2-age; 25 | printf("you will die after %.1f years",y); 26 | break; 27 | } 28 | case 2: 29 | { 30 | y=79.8-age; 31 | printf("you will die after %.1f years",y); 32 | break; 33 | } 34 | default:printf("choose the correct option\n"); 35 | break; 36 | } 37 | break; 38 | } 39 | case 2: 40 | { 41 | printf("Enter your choice for 1.male 2.female\n"); 42 | scanf("%d",&ch); 43 | switch(ch) 44 | { 45 | case 1: 46 | { 47 | y=61.4-age; 48 | printf("you will die after %.1f years",y); 49 | break; 50 | } 51 | case 2: 52 | { 53 | y=65.1-age; 54 | printf("you will die after %.1f years",y); 55 | break; 56 | } 57 | default:printf("choose the correct option\n"); 58 | break; 59 | } 60 | break; 61 | } 62 | case 3: 63 | { 64 | printf("Enter your choice for 1.male 2.female\n"); 65 | scanf("%d",&ch); 66 | switch(ch) 67 | { 68 | case 1: 69 | { 70 | y=76.3-age; 71 | printf("you will die after %.1f years",y); 72 | break; 73 | } 74 | case 2: 75 | { 76 | y=81.4-age; 77 | printf("you will die after %.1f years",y); 78 | break; 79 | } 80 | default:printf("choose the correct option\n"); 81 | break; 82 | } 83 | break; 84 | } 85 | case 4: 86 | { 87 | printf("Enter your choice for 1.male 2.female\n"); 88 | scanf("%d",&ch); 89 | switch(ch) 90 | { 91 | case 1: 92 | { 93 | y=73.2-age; 94 | printf("you will die after %.1f years",y); 95 | break; 96 | } 97 | case 2: 98 | { 99 | y=79.6-age; 100 | printf("you will die after %.1f years",y); 101 | break; 102 | } 103 | default:printf("choose the correct option\n"); 104 | break; 105 | } 106 | break; 107 | } 108 | 109 | case 5: 110 | { 111 | printf("Enter your choice for 1.male 2.female\n"); 112 | scanf("%d",&ch); 113 | switch(ch) 114 | { 115 | case 1: 116 | { 117 | y=79.7-age; 118 | printf("you will die after %.1f years",y); 119 | break; 120 | } 121 | case 2: 122 | { 123 | y=84.5-age; 124 | printf("you will die after %.1f years",y); 125 | break; 126 | } 127 | default:printf("choose the correct option\n"); 128 | break; 129 | } 130 | break; 131 | } 132 | case 6: 133 | { 134 | printf("Enter your choice for 1.male 2.female\n"); 135 | scanf("%d",&ch); 136 | switch(ch) 137 | { 138 | case 1: 139 | { 140 | y=81.4-age; 141 | printf("you will die after %.1f years",y); 142 | break; 143 | } 144 | case 2: 145 | { 146 | y=85.3-age; 147 | printf("you will die after %.1f years",y); 148 | break; 149 | } 150 | default:printf("choose the correct option\n"); 151 | break; 152 | } 153 | break; 154 | } 155 | default:printf("choose correct option"); 156 | } 157 | } 158 | 159 | -------------------------------------------------------------------------------- /decimal_to_binary.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | long decimalToBinary(int decimalnum) 5 | { 6 | long binarynum = 0; 7 | int rem, temp = 1; 8 | 9 | while (decimalnum!=0) 10 | { 11 | rem = decimalnum%2; 12 | decimalnum = decimalnum / 2; 13 | binarynum = binarynum + rem*temp; 14 | temp = temp * 10; 15 | } 16 | return binarynum; 17 | } 18 | 19 | int main() 20 | { 21 | int decimalnum; 22 | printf("Enter a Decimal Number: "); 23 | scanf("%d", &decimalnum); 24 | printf("Equivalent Binary Number is: %ld", decimalToBinary(decimalnum)); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /duplicateElement.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int n; 4 | int Dup(int arr[], int element) 5 | { 6 | int p = 0; 7 | for (int i = 0; i < n; i++) 8 | if (arr[i] == element) 9 | p++; 10 | if (p > 1) 11 | return 1; 12 | else 13 | return 0; 14 | } 15 | 16 | void place(int arr[], int element) 17 | { 18 | for (int i = 0; i < n; i++) 19 | if (arr[i] == element) 20 | printf("%d ", i); 21 | } 22 | int main() 23 | { 24 | printf("Enter the size of the array: "); 25 | scanf("%d", &n); 26 | int arr[n]; 27 | printf("Enter the array elements: \n"); 28 | for (int i = 0; i < n; i++) 29 | scanf("%d", &arr[i]); 30 | int key=0; 31 | for (int i = 0; i < n; i++) 32 | if (Dup(arr, arr[i]) == 1) 33 | { 34 | printf("The repeated element is: %d and it is at the index: ", arr[i]); 35 | place(arr, arr[i]); 36 | printf("\n"); 37 | 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /duplicateIndex.c: -------------------------------------------------------------------------------- 1 | #include 2 | int search(int arr[], int n, int key) 3 | { 4 | int p = 0; 5 | for (int i = 0; i < n; i++) 6 | if (arr[i] == key) 7 | p++; 8 | return p; 9 | } 10 | void duplicateCheck(int arr[], int n) 11 | { 12 | for (int i = 0; i < n; i++) 13 | { 14 | int result = search(arr, n, arr[i]); 15 | if (result > 1) 16 | printf("%d ", i); 17 | } 18 | } 19 | int main() 20 | { 21 | int n; 22 | printf("Enter the length of the array: "); 23 | scanf("%d", &n); 24 | int arr[n]; 25 | printf("Enter the elements of the array: "); 26 | for (int i = 0; i < n; i++) 27 | scanf("%d", &arr[i]); 28 | printf("Index of the duplicated element is : "); 29 | duplicateCheck(arr, n); 30 | } 31 | -------------------------------------------------------------------------------- /expressions.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int m=5, n=4; 4 | (m>n)? printf("m is greater than n that is %d > %d",m,n):printf("n is a greater than m that is %d > %d",n,m); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /factorial.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a,i,b; 5 | scanf("%d",&a); 6 | b=1; 7 | for(i=1;i<=a;i++){ 8 | b=b*i; 9 | } 10 | printf("%d",b); 11 | } 12 | -------------------------------------------------------------------------------- /factorial_recursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Recursive method 4 | 5 | int factorial_recur(int num) { 6 | if (num == 0 || num == 1) 7 | return 1; 8 | return num * factorial_recur(num - 1); 9 | } 10 | 11 | 12 | // Iterative method 13 | 14 | int factorial(int num) { 15 | int fac = 1; 16 | for (int x = 2; x <= num; x++) 17 | fac = fac * x; 18 | return fac; 19 | } 20 | 21 | int main() 22 | { 23 | int num = 5; 24 | 25 | int fac = factorial(5); 26 | 27 | printf("Factorial of %d is %d", num, fac); 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /fcfs.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | 5 | { 6 | //this program which process has to excucte first according to their property 7 | 8 | int p[10],at[10],bt[10],ct[10],tat[10],wt[10],i,j,temp=0,n; 9 | 10 | float awt=0,atat=0; 11 | 12 | //no. of processes running on a system 13 | printf("enter no of proccess you want:"); 14 | 15 | scanf("%d",&n); 16 | 17 | printf("enter %d process:",n); 18 | 19 | for(i=0;iat[j+1]) 59 | 60 | { 61 | 62 | temp=p[j+1]; 63 | 64 | p[j+1]=p[j]; 65 | 66 | p[j]=temp; 67 | 68 | temp=at[j+1]; 69 | 70 | at[j+1]=at[j]; 71 | 72 | at[j]=temp; 73 | 74 | temp=bt[j+1]; 75 | 76 | bt[j+1]=bt[j]; 77 | 78 | bt[j]=temp; 79 | 80 | } 81 | 82 | } 83 | 84 | } 85 | 86 | /* calculating 1st ct */ 87 | 88 | ct[0]=at[0]+bt[0]; 89 | 90 | /* calculating 2 to n ct */ 91 | 92 | for(i=1;i 2 | int main() 3 | { 4 | int n1, n2, i, gcd; 5 | 6 | printf("Enter two integers: "); 7 | scanf("%d %d", &n1, &n2); 8 | 9 | for(i=1; i <= n1 && i <= n2; ++i) 10 | { 11 | if(n1%i==0 && n2%i==0) 12 | gcd = i; 13 | } 14 | 15 | printf("G.C.D of %d and %d is %d", n1, n2, gcd); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /harmonic_sum.c: -------------------------------------------------------------------------------- 1 | // That accepts a number and find the harmonic sum of that number. (Hint: the 2 | // harmonic sum of n is equal to the sum of reciprocals of positive integers up to n). For 3 | // example, if n =3, HS = 1+1/2+1/3 4 | 5 | #include 6 | 7 | double sum(int n) 8 | { 9 | //double i, s = 0.0; 10 | if (n < 2) 11 | return 1; 12 | else 13 | return 1 / n + sum(n - 1); 14 | } 15 | 16 | int main() 17 | { 18 | int n; 19 | printf("Enter n: "); 20 | scanf("%d", &n); 21 | printf("Sum is %f", sum(n)); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /heap_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // HEAP SORT 4 | void max_heapify(int arr[], int n, int i) { 5 | int max_idx = i; 6 | int left = i * 2 + 1; 7 | int right = i * 2 + 2; 8 | 9 | if (left < n && arr[left] > arr[max_idx]) 10 | max_idx = left; 11 | 12 | if (right < n && arr[right] > arr[max_idx]) 13 | max_idx = right; 14 | 15 | if (max_idx != i) { 16 | int temp = arr[i]; 17 | arr[i] = arr[max_idx]; 18 | arr[max_idx] = temp; 19 | 20 | max_heapify(arr, n, max_idx); 21 | } 22 | } 23 | 24 | void heap_sort(int arr[], int n) { 25 | for (int i = n / 2 - 1; i >= 0; i--) 26 | max_heapify(arr, n, i); 27 | 28 | for (int i = n - 1; i >= 0; i--) { 29 | int temp = arr[i]; 30 | arr[i] = arr[0]; 31 | arr[0] = temp; 32 | max_heapify(arr, i, 0); 33 | } 34 | } 35 | 36 | 37 | // PRINT ARRAY 38 | 39 | void print_arr(int arr[], int n) { 40 | for (int i = 0; i < n; ++i) 41 | printf("%d ", arr[i]); 42 | } 43 | 44 | int main() 45 | { 46 | int arr[] = { 80, 90, 100, 40, 50, 30, 20, 70, 60, 10}, n = 10; 47 | 48 | heap_sort(arr, n); 49 | 50 | print_arr(arr, n); 51 | 52 | return 0; 53 | } -------------------------------------------------------------------------------- /helloworld.c: -------------------------------------------------------------------------------- 1 | // Tribute to all the teachers who taught kids coding! 2 | #include 3 | 4 | int main(){ 5 | printf("Hello world!"); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /joinString.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | char str1[50], str2[50], i, j; 5 | printf("\nEnter first string: "); 6 | scanf("%s",str1); 7 | printf("\nEnter second string: "); 8 | scanf("%s",str2); 9 | for(i=0; str1[i]!='\0'; ++i); 10 | for(j=0; str2[j]!='\0'; ++j, ++i) 11 | { 12 | str1[i]=str2[j]; 13 | } 14 | str1[i]='\0'; 15 | printf("\nOutput: %s",str1); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /krishnamurthy.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int number, temp, sum, currentDigit, fact; 5 | printf("Enter an Integer: "); 6 | scanf("%d",&number); 7 | temp = number; 8 | sum = 0; 9 | 10 | while(temp!=0) 11 | { 12 | currentDigit = temp % 10; 13 | fact = 1; 14 | 15 | // finding factorial of the currentDigit 16 | for(int i=1; i<=currentDigit; i++) 17 | { 18 | fact *= i; 19 | } 20 | 21 | sum += fact; 22 | temp /= 10; 23 | } 24 | 25 | if(sum == number) 26 | { 27 | printf("%d is Krishnamurthy Number.",number); 28 | } 29 | else 30 | { 31 | printf("%d is not a Krishnamurthy Number.",number); 32 | } 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /kth_largest_element.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int cmp_func (const void * a, const void * b) { 5 | return ( *(int*)a - *(int*)b ); 6 | } 7 | 8 | void reverse (int arr[], int n){ 9 | for(int i = 0; i < n/2; i++){ 10 | int temp = arr[i]; 11 | arr[i] = arr[n-i-1]; 12 | arr[n-i-1] = temp; 13 | } 14 | } 15 | int kth_largest_element(int arr[], int k, int n){ 16 | // Sorts the array 17 | qsort(arr, n, sizeof(int), cmp_func); 18 | 19 | // Reverses the array 20 | reverse(arr, n); 21 | 22 | // Returns the required element 23 | return arr[k-1]; 24 | } 25 | 26 | int main(){ 27 | // Given array 28 | int arr[] = {12, 15, 7, 3, 8, 16, 25}; 29 | 30 | // n represents the size of the array 31 | int n = sizeof(arr) / sizeof(arr[0]); 32 | 33 | int k = 4; 34 | printf("The %dth largest element = %d", k, kth_largest_element(arr, k, n)); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /largestnum.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int num1, num2, num3; 5 | int *p1, *p2, *p3; 6 | 7 | //taking input from user 8 | printf("Enter First Number: "); 9 | scanf("%d",&num1); 10 | printf("Enter Second Number: "); 11 | scanf("%d",&num2); 12 | printf("Enter Third Number: "); 13 | scanf("%d",&num3); 14 | 15 | //assigning the address of input numbers to pointers 16 | p1 = &num1; 17 | p2 = &num2; 18 | p3 = &num3; 19 | if(*p1 > *p2) 20 | { 21 | if(*p1 > *p3) 22 | { 23 | printf("%d is the largest number", *p1); 24 | } 25 | else 26 | { 27 | printf("%d is the largest number", *p3); 28 | } 29 | } 30 | else 31 | { 32 | if(*p2 > *p3) 33 | { 34 | printf("%d is the largest number", *p2); 35 | } 36 | else 37 | { 38 | printf("%d is the largest number", *p3); 39 | } 40 | } 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /lcm_of_two.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int num1, num2, max; 4 | printf("Enter two positive integers: "); 5 | scanf("%d %d", &num1, &num2); 6 | 7 | // greater number between num1 and num2 is stored in max 8 | max = (num1 > num2) ? num1 : num2; 9 | 10 | while (1) { 11 | if (max % num1 == 0 && max % num2 == 0) { 12 | printf("LCM of input numbers %d and %d is %d.", num1, num2, max); 13 | break; 14 | } 15 | ++max; 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /left shift.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a,b; 5 | printf("LEFT SHIFT BY 2 POSITIONS!!\n"); 6 | printf("Enter a number:"); 7 | scanf("%d",&a); 8 | b = a<<2; 9 | printf("%d",b); 10 | } 11 | -------------------------------------------------------------------------------- /linkedlist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | //Function Declaration 4 | void insert_begin(int ele); 5 | void insert_choice(int ele); 6 | void insert_end(int ele); 7 | void delete_begin(); 8 | void delete_choice(); 9 | void delete_end(); 10 | void traverse(); 11 | 12 | struct node 13 | { 14 | int data; 15 | struct node* next; 16 | }; 17 | struct node* start=NULL; 18 | void main() 19 | {int c=1,choice; 20 | 21 | int ele; 22 | while(c==1) 23 | { printf("Enter the number to do required operation on Linked List: \n1.Insert At Beginning \n2.Insert At Choice \n3.Insert At End \n4.Delete At Beginning \n5.Delete At Choice \n6.Delete At End \n7.Traverse Through the LinkedList \n0.Exit\n"); 24 | scanf("%d",&choice); 25 | switch(choice) 26 | { 27 | case 0: 28 | c=0; 29 | break; 30 | case 1: 31 | printf("Enter element to be inserted:-"); 32 | scanf("%d",&ele); 33 | insert_begin(ele); 34 | break; 35 | case 2: 36 | printf("Enter element to be inserted:-"); 37 | scanf("%d",&ele); 38 | insert_choice(ele); 39 | break; 40 | case 3: 41 | printf("Enter element to be inserted:-"); 42 | scanf("%d",&ele); 43 | insert_end(ele); 44 | break; 45 | case 4: 46 | delete_begin(); 47 | break; 48 | case 5: 49 | delete_choice(); 50 | break; 51 | case 6: 52 | delete_end(); 53 | break; 54 | case 7: 55 | traverse(); 56 | break; 57 | default: 58 | printf("\nChoose a correct choice..."); 59 | break; 60 | } 61 | } 62 | } 63 | void insert_begin(int x) 64 | { 65 | struct node* temp; 66 | temp=(struct node*)malloc(sizeof(struct node)); 67 | temp->data=x; 68 | if(temp==NULL) 69 | { 70 | start=temp; 71 | temp->next=NULL; 72 | } 73 | else 74 | { 75 | temp->next=start; 76 | start=temp; 77 | } 78 | } 79 | void insert_end(int x) 80 | { 81 | struct node* temp; 82 | struct node* t; 83 | temp=(struct node*)malloc(sizeof(struct node)); 84 | //t=(struct node*)malloc(sizeof(struct node)); 85 | t=start; 86 | while(t->next!=NULL) 87 | { 88 | t=t->next; 89 | } 90 | t->next=temp; 91 | temp->data=x; 92 | temp->next=NULL; 93 | } 94 | void insert_choice(int x) 95 | { 96 | struct node* temp; 97 | struct node* t; 98 | int pos,i=1; 99 | //t=(struct node*)malloc(sizeof(struct node)); 100 | t=start; 101 | temp=(struct node*)malloc(sizeof(struct node)); 102 | temp->data=x; 103 | printf("Enter position to be added:"); 104 | scanf("%d",&pos); 105 | while(inext; 108 | i++; 109 | } 110 | temp->next=t->next; 111 | t->next=temp; 112 | } 113 | void delete_begin() 114 | { 115 | if(start==NULL) 116 | { 117 | printf("List is empty!"); 118 | } 119 | else{ 120 | start=start->next; 121 | } 122 | } 123 | void delete_end() 124 | { 125 | if(start==NULL) 126 | { 127 | printf("List is empty!"); 128 | } 129 | else{ 130 | struct node* temp; 131 | temp=start; 132 | while(temp->next->next!=NULL) 133 | { 134 | temp=temp->next; 135 | } 136 | temp=temp->next; 137 | } 138 | } 139 | void delete_choice() 140 | {if(start==NULL) 141 | { 142 | printf("List is empty!"); 143 | } 144 | else{ 145 | struct node* temp; 146 | struct node* t; 147 | int pos,i=1; 148 | printf("Enter position to be deleted:"); 149 | scanf("%d",&pos); 150 | while(inext; 153 | i++; 154 | } 155 | t->next=t->next->next; 156 | } 157 | } 158 | void traverse() 159 | { 160 | struct node* temp; 161 | temp=(struct node*)malloc(sizeof(struct node)); 162 | temp=start; 163 | printf("START -->"); 164 | while(temp!=NULL) 165 | { 166 | printf(" %d -->",temp->data); 167 | temp=temp->next; 168 | } 169 | printf(" NULL"); 170 | } 171 | -------------------------------------------------------------------------------- /linkedlist.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hitaarthh/C-Programming/3f23917dbe829185ad85ed7e60064adc8ace132b/linkedlist.exe -------------------------------------------------------------------------------- /luckyNumber.c: -------------------------------------------------------------------------------- 1 | // C program for Lucky Numbers 2 | #include 3 | #define bool int 4 | 5 | //Returns 1 if n is a lucky no. otherwise returns 0 6 | bool isLucky(int n){ 7 | static int counter = 2; 8 | 9 | if (counter > n) 10 | return 1; 11 | if (n % counter == 0) 12 | return 0; 13 | 14 | /*calculate next position of input no. 15 | Variable "next_position" is just for 16 | readability of the program we can 17 | remove it and update in "n" only */ 18 | int next_position = n - (n / counter); 19 | 20 | counter++; 21 | return isLucky(next_position); 22 | } 23 | 24 | // Driver code 25 | int main(){ 26 | int x = 5; 27 | 28 | // Function call 29 | if (isLucky(x)) 30 | printf("%d is a lucky no.", x); 31 | else 32 | printf("%d is not a lucky no.", x); 33 | getchar(); 34 | } 35 | -------------------------------------------------------------------------------- /matrix_multiplication.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n; 5 | printf("Enter the size of the matrix: "); 6 | scanf("%d",&n); 7 | int a[n][n],b[n][n]; 8 | printf("Enter the elements of the first matrix: "); 9 | for(int i=0;i 4 | 5 | // Merge two subarrays L and M into arr 6 | void merge(int arr[], int p, int q, int r) { 7 | 8 | // Create L ← A[p..q] and M ← A[q+1..r] 9 | int n1 = q - p + 1; 10 | int n2 = r - q; 11 | 12 | int L[n1], M[n2]; 13 | 14 | for (int i = 0; i < n1; i++) 15 | L[i] = arr[p + i]; 16 | for (int j = 0; j < n2; j++) 17 | M[j] = arr[q + 1 + j]; 18 | 19 | // Maintain current index of sub-arrays and main array 20 | int i, j, k; 21 | i = 0; 22 | j = 0; 23 | k = p; 24 | 25 | // Until we reach either end of either L or M, pick larger among 26 | // elements L and M and place them in the correct position at A[p..r] 27 | while (i < n1 && j < n2) { 28 | if (L[i] <= M[j]) { 29 | arr[k] = L[i]; 30 | i++; 31 | } else { 32 | arr[k] = M[j]; 33 | j++; 34 | } 35 | k++; 36 | } 37 | 38 | // When we run out of elements in either L or M, 39 | // pick up the remaining elements and put in A[p..r] 40 | while (i < n1) { 41 | arr[k] = L[i]; 42 | i++; 43 | k++; 44 | } 45 | 46 | while (j < n2) { 47 | arr[k] = M[j]; 48 | j++; 49 | k++; 50 | } 51 | } 52 | 53 | // Divide the array into two subarrays, sort them and merge them 54 | void mergeSort(int arr[], int l, int r) { 55 | if (l < r) { 56 | 57 | // m is the point where the array is divided into two subarrays 58 | int m = l + (r - l) / 2; 59 | 60 | mergeSort(arr, l, m); 61 | mergeSort(arr, m + 1, r); 62 | 63 | // Merge the sorted subarrays 64 | merge(arr, l, m, r); 65 | } 66 | } 67 | 68 | // Print the array 69 | void printArray(int arr[], int size) { 70 | for (int i = 0; i < size; i++) 71 | printf("%d ", arr[i]); 72 | printf("\n"); 73 | } 74 | 75 | // Driver program 76 | int main() { 77 | int arr[] = {6, 5, 12, 10, 9, 1}; 78 | int size = sizeof(arr) / sizeof(arr[0]); 79 | 80 | mergeSort(arr, 0, size - 1); 81 | 82 | printf("Sorted array: \n"); 83 | printArray(arr, size); 84 | } 85 | -------------------------------------------------------------------------------- /merge_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // MERGE SORT 4 | 5 | void merge(int arr[], int low, int mid, int high) { 6 | int n1 = mid - low + 1; 7 | int n2 = high - mid; 8 | 9 | int L[n1], R[n2], i, j, k; 10 | 11 | for (i = 0; i < n1; i++) 12 | L[i] = arr[low + i]; 13 | 14 | for (j = 0; j < n2; j++) 15 | R[j] = arr[mid + 1 + j]; 16 | 17 | i = j = 0; 18 | k = low; 19 | 20 | while (i < n1 && j < n2) { 21 | if (L[i] <= R[j]) { 22 | arr[k] = L[i]; 23 | i++; 24 | } else { 25 | arr[k] = R[j]; 26 | j++; 27 | } 28 | k++; 29 | } 30 | 31 | while (i < n1) { 32 | arr[k] = L[i]; 33 | k++; 34 | i++; 35 | } 36 | while (j < n2) { 37 | arr[k] = R[j]; 38 | k++; 39 | j++; 40 | } 41 | } 42 | 43 | void merge_sort(int arr[], int low, int high) { 44 | if (low < high) { 45 | int mid = low + (high - low) / 2; 46 | merge_sort(arr, low, mid); 47 | merge_sort(arr, mid + 1, high); 48 | merge(arr, low, mid, high); 49 | } 50 | } 51 | 52 | 53 | // PRINT ARRAY 54 | 55 | void print_arr(int arr[], int n) { 56 | for (int i = 0; i < n; ++i) 57 | printf("%d ", arr[i]); 58 | } 59 | 60 | int main() 61 | { 62 | int arr[] = { 80, 90, 100, 40, 50, 30, 20, 70, 60, 10}, n = 10; 63 | 64 | bubble_sort(arr, n); 65 | 66 | print_arr(arr, n); 67 | 68 | return 0; 69 | } -------------------------------------------------------------------------------- /missing element.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int n,sum=0,sum1=0; 5 | printf("Enter a number: "); 6 | scanf("%d",&n); 7 | int a[n-1]; 8 | for(int i=0;i 2 | 3 | // Finding the most frequent character in a string 4 | 5 | int main() 6 | { 7 | char strg[1000]; 8 | int a[1000], i, j, k=0, count=0, n; 9 | 10 | printf("Enter any string: "); 11 | scanf("%s", &strg); 12 | 13 | for(j=0; strg[j]; j++) { 14 | n = j; 15 | } 16 | 17 | for(i = 0; i < n; i++) { 18 | a[i] = 0; 19 | count = 1; 20 | 21 | if(strg[i]) { 22 | for(j = i + 1; j < n; j++) { 23 | if(strg[i] == strg[j]) { 24 | count++; 25 | strg[j]= '\0'; 26 | } 27 | } 28 | } 29 | 30 | a[i] = count; 31 | 32 | if(count >= k) { 33 | k = count; 34 | } 35 | } 36 | 37 | printf("The character occuring maximum: \n"); 38 | 39 | for(j=0; j 2 | #include 3 | #include 4 | int a[30],count=0; 5 | int place(int pos) { 6 | int i; 7 | for (i=1;i 2 | #define start main 3 | void start() { 4 | printf("Hello"); 5 | } 6 | -------------------------------------------------------------------------------- /no_semicolon.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | if(printf("hello world")){} 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /oddpairs.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | // your code goes here 5 | int t, n; 6 | int count=0; 7 | scanf("%d",&t); 8 | for(int i=0; i=n; i++) 12 | { 13 | for(int j=n; j>0; j--) 14 | { 15 | if((i+j)%2!=0) 16 | { 17 | count++; 18 | } 19 | } 20 | } 21 | printf("%d",count); 22 | } 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /ordinal.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char *ordinal(int v) 4 | { 5 | 6 | if(v==11 || v==12 || v==13){ 7 | return ("th");} 8 | 9 | v%=10; 10 | 11 | if(v==1){ 12 | return ("st");} 13 | if(v==2){ 14 | return ("nd");} 15 | if(v==3){ 16 | return ("rd");} 17 | return ("th"); 18 | } 19 | 20 | int main() 21 | { 22 | 23 | int c,a=10; 24 | 25 | for( c=1; c<=20; c++){ 26 | printf("%3d%s\t%3d%s\t%3d%s\t%3d%s\t%3d%s\n", 27 | c, ordinal(c), 28 | c+20, ordinal(c+20), 29 | c+40, ordinal(c+40), 30 | c+60, ordinal(c+60), 31 | c+80, ordinal(c+80) 32 | ); 33 | } 34 | 35 | return (0); 36 | } -------------------------------------------------------------------------------- /palindrome.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int n, reversed = 0, remainder, original; 4 | printf("Enter an integer: "); 5 | scanf("%d", &n); 6 | original = n; 7 | 8 | // reversed integer is stored in reversed variable 9 | while (n != 0) { 10 | remainder = n % 10; 11 | reversed = reversed * 10 + remainder; 12 | n /= 10; 13 | } 14 | 15 | // palindrome if orignal and reversed are equal 16 | if (original == reversed) 17 | printf("%d is a palindrome.", original); 18 | else 19 | printf("%d is not a palindrome.", original); 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /patterns/Diamond.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | int i, j, k = 0, n, x; 6 | printf("Enter Number of Rows: "); 7 | scanf("%d", &n); 8 | x = (n + 1) / 2; 9 | for (i = 1; i <= n; i++) 10 | { 11 | if (n % 2 == 0) { 12 | if (i <= x) k++; 13 | if (i > x + 1) k--; 14 | } 15 | else 16 | i <= x ? k++ : k--; 17 | for (j = 1; j <= n; j++) 18 | { 19 | if (j >= x + 1 - k && j <= x - 1 + k) 20 | printf("*"); 21 | else 22 | printf(" "); 23 | } 24 | printf("\n"); 25 | } 26 | return 0; 27 | } 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /patterns/Diamond_Hollow.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void main() 4 | { 5 | int i,j,k=0,n; 6 | printf("Enter Number of Rows: "); 7 | scanf("%d",&n); 8 | for(i=1;i<=n;i++) 9 | { 10 | i<=(n/2+1)?k++:k--; 11 | for(j=1;j<=n;j++) 12 | { 13 | if(j<=(n/2+2)-k || j>=(n/2)+k) 14 | printf("*"); 15 | else 16 | printf(" "); 17 | } 18 | printf("\n"); 19 | } 20 | } 21 | 22 | 23 | -------------------------------------------------------------------------------- /patterns/Pattern_EQI_T_1.c: -------------------------------------------------------------------------------- 1 | // Equilateral triangle 2 | 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | int i, j, n; 9 | printf("Enter How many Rows to print: "); 10 | scanf("%d", &n); 11 | for (i = 1; i <= n; i++) 12 | { 13 | for (j = 1; j <= (n * 2 - 1); j++) 14 | { 15 | if (j >= (n + 1) - i && j <= (n - 1) + i) 16 | printf("*"); 17 | else 18 | printf(" "); 19 | } 20 | printf("\n"); 21 | } 22 | getch(); 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /patterns/Pattern_EQI_T_2.c: -------------------------------------------------------------------------------- 1 | // Equilateral triangle 2 | 3 | #include 4 | #include 5 | int main() 6 | { 7 | int i, j, n; 8 | printf("Enter How Many Rows to print: "); 9 | scanf("%d", &n); 10 | for (i = 1; i <= n; i++) 11 | { 12 | for (j = 1; j <= (n * 2 - 1); j++) 13 | { 14 | if (j >= i && j <= n * 2 - i) 15 | printf("*"); 16 | else 17 | printf(" "); 18 | } 19 | printf("\n"); 20 | } 21 | getch(); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /patterns/Pattern_EQI_T_3.c: -------------------------------------------------------------------------------- 1 | // Equilateral triangle 2 | 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | int i, j, k = 0, rows; 9 | printf("Enter How many rows to print: "); 10 | scanf("%d", &rows); 11 | for (i = 1; i <= rows; i++) 12 | { 13 | if (rows % 2 == 0) 14 | { 15 | if (i <= rows / 2) 16 | k++; 17 | if (i > rows / 2 + 1) 18 | k--; 19 | } 20 | else 21 | i <= (rows + 1) / 2 ? k++ : k--; 22 | for (j = 1; j <= (rows + 1) / 2; j++) 23 | { 24 | if (j <= k) 25 | { 26 | printf("*"); 27 | } 28 | else 29 | printf(" "); 30 | } 31 | printf("\n"); 32 | } 33 | getch(); 34 | 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /patterns/Pattern_EQI_T_4.c: -------------------------------------------------------------------------------- 1 | // Equilateral triangle 2 | 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | int i, j, n, k = 0; 9 | printf("Enter a Number of Rows: "); 10 | scanf("%d", &n); 11 | for (i = 1; i <= n; i++) 12 | { 13 | if (n % 2 == 0) 14 | { 15 | if (i <= n / 2) 16 | k++; 17 | if (i > n / 2 + 1) 18 | k--; 19 | } 20 | else 21 | i <= (n / 2 + 1) ? k++ : k--; 22 | for (j = 1; j <= (n / 2 + 1); j++) 23 | { 24 | if (j >= (n / 2 + 2) - k) 25 | printf("*"); 26 | else 27 | printf(" "); 28 | } 29 | printf("\n"); 30 | } 31 | getch(); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /patterns/Pattern_EQI_T_with_Space_5.c: -------------------------------------------------------------------------------- 1 | // Equilateral triangle with space 2 | 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | int i, j, k, n; 9 | printf("Enter How Many Rows to Print: "); 10 | scanf("%d", &n); 11 | for (i = 1; i <= n; i++) 12 | { k = 1; 13 | for (j = 1; j <= (n * 2 - 1); j++) 14 | { 15 | if (j >= (n + 1) - i && j <= (n - 1) + i && k) 16 | { printf("*"); 17 | k = 0; 18 | } 19 | else { 20 | printf(" "); 21 | k = 1; 22 | } 23 | } 24 | printf("\n"); 25 | } 26 | getch(); 27 | 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /patterns/Pattern_EQI_T_with_Space_6.c: -------------------------------------------------------------------------------- 1 | // Equilateral triangle with space 2 | 3 | #include 4 | #include 5 | int main() 6 | { 7 | int i, j, k, n; 8 | printf("Enter How Many Rows to Print: "); 9 | scanf("%d", &n); 10 | for (i = 1; i <= n; i++) 11 | { k = 1; 12 | for (j = 1; j <= (n * 2 - 1); j++) 13 | { 14 | if (j >= i && j <= n * 2 - i && k) 15 | { printf("*"); 16 | k = 0; 17 | } 18 | else { 19 | printf(" "); 20 | k = 1; 21 | } 22 | } 23 | printf("\n"); 24 | } 25 | getch(); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /patterns/Pattern_EQI_T_with_Space_7.c: -------------------------------------------------------------------------------- 1 | // Equilateral triangle with space 2 | 3 | #include 4 | #include 5 | int main() 6 | { 7 | int i, j, k = 0, rows, s; 8 | printf("Enter How many rows to print: "); 9 | scanf("%d", &rows); 10 | for (i = 1; i <= rows; i++) 11 | { 12 | if (rows % 2 == 0) 13 | { 14 | if (i <= rows / 2) 15 | k++; 16 | if (i > rows / 2 + 1) 17 | k--; 18 | } 19 | else 20 | i <= (rows + 1) / 2 ? k++ : k--; 21 | if (i % 2 == 0) 22 | s = 0; 23 | for (j = 1; j <= rows * 2 - 1; j++) 24 | { 25 | if (j <= k && s) { 26 | printf("*"); 27 | s = 0; 28 | } 29 | else { 30 | printf(" "); 31 | s = 1; 32 | } 33 | } 34 | printf("\n"); 35 | } 36 | getch(); 37 | 38 | return 0; 39 | } 40 | 41 | 42 | -------------------------------------------------------------------------------- /patterns/Pattern_EQI_T_with_Space_8.c: -------------------------------------------------------------------------------- 1 | // Equilateral triangle with space 2 | 3 | #include 4 | #include 5 | int main() 6 | { 7 | int i, j, n, k = 0, s; 8 | printf("Enter a Number of Rows: "); 9 | scanf("%d", &n); 10 | for (i = 1; i <= n; i++) 11 | { 12 | if (n % 2 == 0) 13 | { 14 | if (i <= n / 2) 15 | k++; 16 | if (i > n / 2 + 1) 17 | k--; 18 | } 19 | else 20 | i <= (n / 2 + 1) ? k++ : k--; 21 | s = 1; 22 | for (j = 1; j <= (n / 2 + 1); j++) 23 | { 24 | if (j >= (n / 2 + 2) - k && s) { 25 | printf("*"); 26 | s = 0; 27 | } 28 | else { 29 | printf(" "); 30 | s = 1; 31 | } 32 | } 33 | printf("\n"); 34 | } 35 | getch(); 36 | 37 | return 0; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /post increment.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a=10; 5 | printf("Post increment\n%d\n",a++); 6 | printf("Pre increment\n%d",++a); 7 | } 8 | -------------------------------------------------------------------------------- /precedence.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | printf("%d", 1<< 2+2 <<4); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /priority.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define max 10 3 | int main() 4 | { 5 | int i,j,n,bt[max],p[max],wt[max],tat[max],pr[max],total=0,pos,temp; 6 | float avg_wt,avg_tat; 7 | printf("Enter Total Number of Process:"); 8 | scanf("%d",&n); 9 | printf("\nEnter Burst Time and Priority For "); 10 | for(i=0;i 2 | #include 3 | 4 | int mutex=1,fill=0,empty=3,x=0; 5 | 6 | int main() 7 | { 8 | int n; 9 | void producer(); 10 | void consumer(); 11 | int wait(int); 12 | int signal(int); 13 | 14 | printf("\n1.Producer\n2.Consumer\n3.Exit"); 15 | while(1) 16 | { 17 | printf("\nEnter your choice:"); 18 | scanf("%d",&n); 19 | //menu driven producer consumer 20 | switch(n) 21 | { 22 | 23 | //case 1:mutex availability = true & empty space !=0 [PRODUCER] 24 | case 1: if((mutex==1)&&(empty!=0)) 25 | producer(); 26 | else 27 | printf("Buffer is full!!"); 28 | break; 29 | 30 | //case 2:mutex availability = true & filled space !=0 [CONSUMER] 31 | case 2: if((mutex==1)&&(fill!=0)) 32 | consumer(); 33 | else 34 | printf("Buffer is empty!!"); 35 | break; 36 | 37 | //case 3:exit and break 38 | case 3: 39 | exit(0); 40 | break; 41 | } 42 | } 43 | return 0; 44 | } 45 | 46 | // wait function decrements semaphore 47 | int wait(int s) 48 | { 49 | return (--s); 50 | } 51 | 52 | //signal function increments semaphore 53 | int signal(int s) 54 | { 55 | return(++s); 56 | } 57 | 58 | // In producer function , -mutex , +fill , -empty , +mutex (where wait perform decrement(-) & signal perform increment(+)) 59 | void producer() 60 | { 61 | mutex=wait(mutex); 62 | fill=signal(fill); 63 | empty=wait(empty); 64 | x++; 65 | printf("\nProducer produces the item %d",x); 66 | mutex=signal(mutex); 67 | } 68 | 69 | // In consumer function , -mutex , -fill , +empty , +mutex (where wait perform decrement(-) & signal perform increment(+)) 70 | void consumer() 71 | { 72 | mutex=wait(mutex); 73 | fill=wait(fill); 74 | empty=signal(empty); 75 | printf("\nConsumer consumes item %d",x); 76 | x--; 77 | mutex=signal(mutex); 78 | } 79 | -------------------------------------------------------------------------------- /product.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int product(int a, int b){ 4 | if (a 2 | 3 | void quicksort(int number[25],int first,int last){ 4 | 5 | int i, j, pivot, temp; 6 | 7 | if(firstnumber[pivot]) 22 | 23 | j--; 24 | 25 | if(i 2 | 3 | int main(){ 4 | int a,b,c; 5 | printf("LEFT SHIFT BY 2 POSITIONS!!\n"); 6 | printf("Enter a number:"); 7 | scanf("%d",&a); 8 | b = a>>1; 9 | c = a>>2; 10 | printf("%d\n%d",b,c); 11 | } 12 | -------------------------------------------------------------------------------- /roundrobin.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int count,n,time,remain,flag=0,time_quantum; 5 | int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10]; 6 | printf("Enter Total Process:\t "); 7 | scanf("%d",&n); 8 | remain=n; 9 | for(count=0;count0) 22 | { 23 | time+=rt[count]; 24 | rt[count]=0; 25 | flag=1; 26 | } 27 | else if(rt[count]>0) 28 | { 29 | rt[count]-=time_quantum; 30 | time+=time_quantum; 31 | } 32 | if(rt[count]==0 && flag==1) 33 | { 34 | remain--; 35 | printf("P[%d]\t\t%d\t\t%d\n",count+1,time-at[count],time-at[count]-bt[count]); 36 | wait_time+=time-at[count]-bt[count]; 37 | turnaround_time+=time-at[count]; 38 | flag=0; 39 | } 40 | if(count==n-1) 41 | count=0; 42 | else if(at[count+1]<=time) 43 | count++; 44 | else 45 | count=0; 46 | } 47 | printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n); 48 | printf("Avg Turnaround Time = %f",turnaround_time*1.0/n); 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /searching.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // LINEAR SEARCH 4 | 5 | int linear_search(int arr[], int n, int key) { 6 | for (int i = 0; i < n; i++) { 7 | if (arr[i] == key) 8 | return i; 9 | } 10 | return -1; 11 | } 12 | 13 | // BINARY SEARCH 14 | 15 | int binary_search(int arr[], int n, int key) { 16 | int left = 0, right = n - 1, mid; 17 | 18 | while (left <= right) 19 | { 20 | mid = (left + right) / 2; 21 | 22 | if (arr[mid] < key) 23 | left = mid + 1; 24 | else if (arr[mid] > key) 25 | right = mid - 1; 26 | else 27 | return mid; 28 | } 29 | return -1; 30 | } 31 | 32 | 33 | // PRINT ARRAY 34 | 35 | void print_arr(int arr[], int n) { 36 | for (int i = 0; i < n; ++i) 37 | printf("%d ", arr[i]); 38 | } 39 | 40 | int main() 41 | { 42 | // int arr[] = { 80, 90, 100, 40, 50, 30, 20, 70, 60, 10}, n = 10; 43 | 44 | int arr2[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}, n = 10; 45 | 46 | int index = binary_search(arr2, n , 80); 47 | 48 | print_arr(arr2, n); 49 | 50 | if (index != -1) 51 | printf("\n\nValue found at position %d\n", index + 1); 52 | else 53 | printf("\n\nValue not found\n"); 54 | 55 | return 0; 56 | } -------------------------------------------------------------------------------- /selection_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void selection(int arr[], int n) 4 | { 5 | int i, j, small; 6 | 7 | for (i = 0; i < n-1; i++) 8 | { 9 | small = i; 10 | 11 | for (j = i+1; j < n; j++) 12 | if (arr[j] < arr[small]) 13 | small = j; 14 | 15 | int temp = arr[small]; 16 | arr[small] = arr[i]; 17 | arr[i] = temp; 18 | } 19 | } 20 | 21 | void printArr(int a[], int n) /* function to print the array */ 22 | { 23 | int i; 24 | for (i = 0; i < n; i++) 25 | printf("%d ", a[i]); 26 | } 27 | 28 | int main() 29 | { 30 | int a[] = { 12, 31, 25, 8, 32, 17 }; 31 | int n = sizeof(a) / sizeof(a[0]); 32 | printf("Before sorting array elements are - \n"); 33 | printArr(a, n); 34 | selection(a, n); 35 | printf("\nAfter sorting array elements are - \n"); 36 | printArr(a, n); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /snake.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define UP 72 12 | #define DOWN 80 13 | #define LEFT 75 14 | #define RIGHT 77 15 | 16 | int length; 17 | int bend_no; 18 | int len; 19 | char key; 20 | void record(); 21 | void load(); 22 | int life; 23 | void Delay(long double); 24 | void Move(); 25 | void Food(); 26 | int Score(); 27 | void Print(); 28 | void gotoxy(int x, int y); 29 | void GotoXY(int x,int y); 30 | void Bend(); 31 | void Boarder(); 32 | void Down(); 33 | void Left(); 34 | void Up(); 35 | void Right(); 36 | void ExitGame(); 37 | int Scoreonly(); 38 | 39 | struct coordinate 40 | { 41 | int x; 42 | int y; 43 | int direction; 44 | }; 45 | 46 | typedef struct coordinate coordinate; 47 | 48 | coordinate head, bend[500],food,body[30]; 49 | 50 | int main() 51 | { 52 | 53 | char key; 54 | 55 | Print(); 56 | 57 | system("cls"); 58 | 59 | load(); 60 | 61 | length=5; 62 | 63 | head.x=25; 64 | 65 | head.y=20; 66 | 67 | head.direction=RIGHT; 68 | 69 | Boarder(); 70 | 71 | Food(); //to generate food coordinates initially 72 | 73 | life=3; //number of extra lives 74 | 75 | bend[0]=head; 76 | 77 | Move(); //initialing initial bend coordinate 78 | 79 | return 0; 80 | 81 | } 82 | 83 | void Move() 84 | { 85 | int a,i; 86 | 87 | do 88 | { 89 | 90 | Food(); 91 | fflush(stdin); 92 | 93 | len=0; 94 | 95 | for(i=0; i<30; i++) 96 | 97 | { 98 | 99 | body[i].x=0; 100 | 101 | body[i].y=0; 102 | 103 | if(i==length) 104 | 105 | break; 106 | 107 | } 108 | 109 | Delay(length); 110 | 111 | Boarder(); 112 | 113 | if(head.direction==RIGHT) 114 | 115 | Right(); 116 | 117 | else if(head.direction==LEFT) 118 | 119 | Left(); 120 | 121 | else if(head.direction==DOWN) 122 | 123 | Down(); 124 | 125 | else if(head.direction==UP) 126 | 127 | Up(); 128 | 129 | ExitGame(); 130 | 131 | } 132 | while(!kbhit()); 133 | 134 | a=getch(); 135 | 136 | if(a==27) 137 | 138 | { 139 | 140 | system("cls"); 141 | 142 | exit(0); 143 | 144 | } 145 | key=getch(); 146 | 147 | if((key==RIGHT&&head.direction!=LEFT&&head.direction!=RIGHT)||(key==LEFT&&head.direction!=RIGHT&&head.direction!=LEFT)||(key==UP&&head.direction!=DOWN&&head.direction!=UP)||(key==DOWN&&head.direction!=UP&&head.direction!=DOWN)) 148 | 149 | { 150 | 151 | bend_no++; 152 | 153 | bend[bend_no]=head; 154 | 155 | head.direction=key; 156 | 157 | if(key==UP) 158 | 159 | head.y--; 160 | 161 | if(key==DOWN) 162 | 163 | head.y++; 164 | 165 | if(key==RIGHT) 166 | 167 | head.x++; 168 | 169 | if(key==LEFT) 170 | 171 | head.x--; 172 | 173 | Move(); 174 | 175 | } 176 | 177 | else if(key==27) 178 | 179 | { 180 | 181 | system("cls"); 182 | 183 | exit(0); 184 | 185 | } 186 | 187 | else 188 | 189 | { 190 | 191 | printf("\a"); 192 | 193 | Move(); 194 | 195 | } 196 | } 197 | 198 | void gotoxy(int x, int y) 199 | { 200 | 201 | COORD coord; 202 | 203 | coord.X = x; 204 | 205 | coord.Y = y; 206 | 207 | SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); 208 | 209 | } 210 | void GotoXY(int x, int y) 211 | { 212 | HANDLE a; 213 | COORD b; 214 | fflush(stdout); 215 | b.X = x; 216 | b.Y = y; 217 | a = GetStdHandle(STD_OUTPUT_HANDLE); 218 | SetConsoleCursorPosition(a,b); 219 | } 220 | void load() 221 | { 222 | int row,col,r,c,q; 223 | gotoxy(36,14); 224 | printf("loading..."); 225 | gotoxy(30,15); 226 | for(r=1; r<=20; r++) 227 | { 228 | for(q=0; q<=100000000; q++); //to display the character slowly 229 | printf("%c",177); 230 | } 231 | getch(); 232 | } 233 | void Down() 234 | { 235 | int i; 236 | for(i=0; i<=(head.y-bend[bend_no].y)&&len=70||head.y<=10||head.y>=30||check!=0) 272 | { 273 | life--; 274 | if(life>=0) 275 | { 276 | head.x=25; 277 | head.y=20; 278 | bend_no=0; 279 | head.direction=RIGHT; 280 | Move(); 281 | } 282 | else 283 | { 284 | system("cls"); 285 | printf("All lives completed\nBetter Luck Next Time!!!\nPress any key to quit the game\n"); 286 | record(); 287 | exit(0); 288 | } 289 | } 290 | } 291 | void Food() 292 | { 293 | if(head.x==food.x&&head.y==food.y) 294 | { 295 | length++; 296 | time_t a; 297 | a=time(0); 298 | srand(a); 299 | food.x=rand()%70; 300 | if(food.x<=10) 301 | food.x+=11; 302 | food.y=rand()%30; 303 | if(food.y<=10) 304 | 305 | food.y+=11; 306 | } 307 | else if(food.x==0)/*to create food for the first time coz global variable are initialized with 0*/ 308 | { 309 | food.x=rand()%70; 310 | if(food.x<=10) 311 | food.x+=11; 312 | food.y=rand()%30; 313 | if(food.y<=10) 314 | food.y+=11; 315 | } 316 | } 317 | void Left() 318 | { 319 | int i; 320 | for(i=0; i<=(bend[bend_no].x-head.x)&&len"); 350 | else 351 | printf("*"); 352 | } 353 | /*body[len].x=head.x-i; 354 | body[len].y=head.y;*/ 355 | len++; 356 | } 357 | Bend(); 358 | if(!kbhit()) 359 | head.x++; 360 | } 361 | void Bend() 362 | { 363 | int i,j,diff; 364 | for(i=bend_no; i>=0&&len0) 381 | for(j=1; j<=diff; j++) 382 | { 383 | /*GotoXY(bend[i].x,(bend[i].y-j)); 384 | printf("*");*/ 385 | body[len].x=bend[i].x; 386 | body[len].y=bend[i].y-j; 387 | GotoXY(body[len].x,body[len].y); 388 | printf("*"); 389 | len++; 390 | if(len==length) 391 | break; 392 | } 393 | } 394 | else if(bend[i].y==bend[i-1].y) 395 | { 396 | diff=bend[i].x-bend[i-1].x; 397 | if(diff<0) 398 | for(j=1; j<=(-diff)&&len0) 411 | for(j=1; j<=diff&&len Use arrow keys to move the snake.\n\n-> You will be provided foods at the several coordinates of the screen which you have to eat. Everytime you eat a food the length of the snake will be increased by 1 element and thus the score.\n\n-> Here you are provided with three lives. Your life will decrease as you hit the wall or snake's body.\n\n-> YOu can pause the game in its middle by pressing any key. To continue the paused game press any other key once again\n\n-> If you want to exit press esc. \n"); 455 | printf("\n\nPress any key to play game..."); 456 | if(getch()==27) 457 | exit(0); 458 | } 459 | void record() 460 | { 461 | char plname[20],nplname[20],cha,c; 462 | int i,j,px; 463 | FILE *info; 464 | info=fopen("record.txt","a+"); 465 | getch(); 466 | system("cls"); 467 | printf("Enter your name\n"); 468 | scanf("%[^\n]",plname); 469 | //************************ 470 | for(j=0; plname[j]!='\0'; j++) //to convert the first letter after space to capital 471 | { 472 | nplname[0]=toupper(plname[0]); 473 | if(plname[j-1]==' ') 474 | { 475 | nplname[j]=toupper(plname[j]); 476 | nplname[j-1]=plname[j-1]; 477 | } 478 | else nplname[j]=plname[j]; 479 | } 480 | nplname[j]='\0'; 481 | //***************************** 482 | //sdfprintf(info,"\t\t\tPlayers List\n"); 483 | fprintf(info,"Player Name :%s\n",nplname); 484 | //for date and time 485 | 486 | time_t mytime; 487 | mytime = time(NULL); 488 | fprintf(info,"Played Date:%s",ctime(&mytime)); 489 | //************************** 490 | fprintf(info,"Score:%d\n",px=Scoreonly());//call score to display score 491 | //fprintf(info,"\nLevel:%d\n",10);//call level to display level 492 | for(i=0; i<=50; i++) 493 | fprintf(info,"%c",'_'); 494 | fprintf(info,"\n"); 495 | fclose(info); 496 | printf("Wanna see past records press 'y'\n"); 497 | cha=getch(); 498 | system("cls"); 499 | if(cha=='y') 500 | { 501 | info=fopen("record.txt","r"); 502 | do 503 | { 504 | putchar(c=getc(info)); 505 | } 506 | while(c!=EOF); 507 | } 508 | fclose(info); 509 | } 510 | int Score() 511 | { 512 | int score; 513 | GotoXY(20,8); 514 | score=length-5; 515 | printf("SCORE : %d",(length-5)); 516 | score=length-5; 517 | GotoXY(50,8); 518 | printf("Life : %d",life); 519 | return score; 520 | } 521 | int Scoreonly() 522 | { 523 | int score=Score(); 524 | system("cls"); 525 | return score; 526 | } 527 | void Up() 528 | { 529 | int i; 530 | for(i=0; i<=(bend[bend_no].y-head.y)&&len 2 | 3 | // BUBBLE SORT 4 | 5 | void bubble_sort(int arr[], int n) { 6 | int temp; 7 | 8 | for (int i = 0; i < n - 1; i++) 9 | { 10 | for (int j = 0; j < n - 1 - i; j++) 11 | { 12 | if (arr[j] > arr[j + 1]) 13 | { 14 | temp = arr[j]; 15 | arr[j] = arr[j + 1]; 16 | arr[j + 1] = temp; 17 | } 18 | } 19 | } 20 | } 21 | 22 | 23 | // SELECTION SORT 24 | 25 | void selection_sort(int arr[], int n) { 26 | int min_idx, temp; 27 | for (int i = 0; i < n - 1; i++) 28 | { 29 | min_idx = i; 30 | for (int j = i + 1; j < n; j++) { 31 | if (arr[j] < arr[min_idx]) 32 | min_idx = j; 33 | } 34 | if (min_idx != i) { 35 | temp = arr[i]; 36 | arr[i] = arr[min_idx]; 37 | arr[min_idx] = temp; 38 | } 39 | } 40 | } 41 | 42 | 43 | // INSERTION SORT 44 | 45 | void insertion_sort(int arr[], int n) { 46 | int key, j; 47 | for (int i = 1; i < n; i++) 48 | { 49 | key = arr[i]; 50 | j = i - 1; 51 | while (j >= 0 && arr[j] > key) { 52 | arr[j + 1] = arr[j]; 53 | j--; 54 | } 55 | arr[j + 1] = key; 56 | } 57 | } 58 | 59 | 60 | // MERGE SORT 61 | 62 | void merge(int arr[], int low, int mid, int high) { 63 | int n1 = mid - low + 1; 64 | int n2 = high - mid; 65 | 66 | int L[n1], R[n2], i, j, k; 67 | 68 | for (i = 0; i < n1; i++) 69 | L[i] = arr[low + i]; 70 | 71 | for (j = 0; j < n2; j++) 72 | R[j] = arr[mid + 1 + j]; 73 | 74 | i = j = 0; 75 | k = low; 76 | 77 | while (i < n1 && j < n2) { 78 | if (L[i] <= R[j]) { 79 | arr[k] = L[i]; 80 | i++; 81 | } else { 82 | arr[k] = R[j]; 83 | j++; 84 | } 85 | k++; 86 | } 87 | 88 | while (i < n1) { 89 | arr[k] = L[i]; 90 | k++; 91 | i++; 92 | } 93 | while (j < n2) { 94 | arr[k] = R[j]; 95 | k++; 96 | j++; 97 | } 98 | } 99 | 100 | void merge_sort(int arr[], int low, int high) { 101 | if (low < high) { 102 | int mid = low + (high - low) / 2; 103 | merge_sort(arr, low, mid); 104 | merge_sort(arr, mid + 1, high); 105 | merge(arr, low, mid, high); 106 | } 107 | } 108 | 109 | // QUICK SORT 110 | 111 | 112 | 113 | 114 | // PRINT ARRAY 115 | 116 | void print_arr(int arr[], int n) { 117 | for (int i = 0; i < n; ++i) 118 | printf("%d ", arr[i]); 119 | } 120 | 121 | int main() 122 | { 123 | int arr[] = { 80, 90, 100, 40, 50, 30, 20, 70, 60, 10}, n = 10; 124 | 125 | bubble_sort(arr, n); 126 | 127 | print_arr(arr, n); 128 | 129 | return 0; 130 | } -------------------------------------------------------------------------------- /sorting1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a[5]={10,12,1,3,2},t,i,j; 5 | for(j=0;j<4;j++){ 6 | for(i=0;i<4;i++){ 7 | if (a[i] 2 | #include 3 | #include 4 | #include 5 | 6 | int main() { 7 | 8 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 9 | int n, a[10000], count=0, rev[1000], rem=0, i, j, copy; 10 | scanf("%d",&n); 11 | for(i=0; i 2 | #include 3 | 4 | int main() 5 | { 6 | double number, squareRoot; 7 | 8 | printf("Enter a number: "); 9 | scanf("%lf", &number); 10 | 11 | // computing the square root 12 | squareRoot = sqrt(number); 13 | 14 | printf("Square root of %.2lf = %.2lf", number, squareRoot); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /stack.c: -------------------------------------------------------------------------------- 1 | #include 2 | int max=100; 3 | int top = -1; 4 | int stack[100]; // Initializing STACK using an array 5 | 6 | // Function prototypes 7 | void push(); 8 | int pop(); 9 | int peek(); 10 | int isEmpty(); 11 | void traverse(); 12 | 13 | 14 | int main(){ 15 | int choice,c=1; 16 | while(c==1){ 17 | printf("\n****** \n"); 18 | printf("Press 1 to Push \nPress 2 to Pop \nPress 3 to Peek \nPress 4 to Check if STACK is EMPTY "); 19 | printf("\nPress 5 to Traverse \nPress 0 to Exit\n"); 20 | scanf("%d", &choice); 21 | printf("\n****** \n"); 22 | switch(choice){ 23 | case 0: c=0; break; 24 | case 1: push(); break; 25 | case 2: pop(); break; 26 | case 3: peek(); break; 27 | case 4: 28 | if(isEmpty()) 29 | { 30 | printf("Stack is EMPTY!\n"); 31 | } 32 | else 33 | { 34 | printf("Stack is NOT empty!\n"); 35 | } 36 | break; 37 | case 5: traverse(); break; 38 | default: printf("Please choose a correct option!\n"); 39 | } 40 | } 41 | return 0; 42 | } 43 | 44 | void push() 45 | {// Push element to the top of the stack 46 | if(top == max-1) 47 | printf("STACK Overflow! \n"); 48 | else{ 49 | int ele; 50 | printf("\nEnter element to be pushed into the STACK: "); 51 | scanf("%d", &ele); 52 | top++; 53 | stack[top] = ele; 54 | } 55 | } 56 | 57 | int pop() 58 | {//Remove and return the top most element of the stack 59 | if(top == -1) 60 | printf("STACK is EMPTY!\n"); 61 | else{ 62 | int ele = stack[top]; 63 | printf("%d popped out of the stack\n", ele); 64 | top--; 65 | return ele; 66 | } 67 | return -1; 68 | } 69 | 70 | int peek() 71 | {//Return the top most element of the stack 72 | int ele = stack[top]; 73 | printf("%d is the top most element of the stack\n", ele); 74 | return ele; 75 | } 76 | 77 | int isEmpty() 78 | {//Check if the stack is EMPTY or not 79 | if(top == -1) 80 | return 1; 81 | 82 | return 0; 83 | } 84 | 85 | void traverse() 86 | {//Printing all STACK elements 87 | if(top!=-1) 88 | { for(int i=0;i<=top;i++) 89 | 90 | printf("%d ",stack[i]); 91 | 92 | } 93 | printf("\n"); 94 | } 95 | 96 | -------------------------------------------------------------------------------- /stackUsingLinkedList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node //Creating structure of our linked list 4 | { 5 | int data; 6 | struct node* next; 7 | }; 8 | struct node* start=NULL; //Globally declaring start node 9 | //Declaring Functions 10 | void push(int ele); 11 | void pop(); 12 | void peek(); 13 | void traverse(); 14 | 15 | void main() //Main Method Begins 16 | { int c=1; int choice; 17 | while(c==1) 18 | { 19 | printf("\nEnter 1 to Push, 2 to Pop, 3 to Peek, 4 to Traverse, 0 to Exit..."); 20 | scanf("%d",&choice); 21 | switch(choice) 22 | { 23 | case 0: 24 | c=0; 25 | break; 26 | case 1: 27 | printf("\nEnter element to be pushed in STACK:"); 28 | int ele; 29 | scanf("%d",&ele); 30 | push(ele); 31 | break; 32 | case 2: 33 | pop(); 34 | break; 35 | case 3: 36 | peek(); 37 | break; 38 | case 4: 39 | traverse(); 40 | break; 41 | default: 42 | break; 43 | }//Switch-Case Ends 44 | }//While-Loop Ends 45 | }// Main Function Ends 46 | void push(int x) 47 | { struct node* temp; 48 | temp=(struct node*)malloc(sizeof(struct node));//Memory Allocation for temp Node 49 | temp->data=x; 50 | if(temp==NULL) 51 | { start=temp; 52 | temp->next=NULL; 53 | } 54 | else 55 | { temp->next=start; 56 | start=temp; 57 | } 58 | } 59 | void pop() 60 | { 61 | if(start==NULL) 62 | { printf("\nSTACK is EMPTY!"); 63 | } 64 | else 65 | { printf("\n%d is popped out of STACK!",start->data); 66 | start=start->next; 67 | } 68 | } 69 | void peek() 70 | {if(start==NULL) 71 | { printf("\nSTACK is EMPTY!"); 72 | } 73 | else 74 | { printf("\n%d is the top element!",start->data); 75 | } 76 | 77 | } 78 | 79 | void traverse() 80 | { if(start==NULL) 81 | printf("\nSTACK is EMPTY!"); 82 | 83 | else 84 | { struct node* temp; 85 | temp=(struct node*)malloc(sizeof(struct node)); 86 | temp=start; 87 | while(temp!=NULL) 88 | { 89 | printf(" %d |",temp->data); 90 | temp=temp->next; 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /star.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a; 5 | printf("Enter the odd number only"); 6 | scanf("%d", &a); 7 | for(int i=1;i<=a;i++) 8 | { 9 | if(i==((a/2)+1)) 10 | { 11 | for(int j=1;j<=a;j++) 12 | { 13 | printf("*"); 14 | } 15 | 16 | } 17 | else 18 | { 19 | for(int j=1;j<=a/2;j++) 20 | { 21 | printf(" "); 22 | } 23 | printf("*"); 24 | } 25 | printf("\n"); 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /string.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | char str[50]; 6 | int i=0, j, chk; 7 | printf("Enter a String: "); 8 | gets(str); 9 | while(str[i]!='\0') 10 | { 11 | chk=0; 12 | if(str[i]==' ') 13 | { 14 | j=i; 15 | while(str[j-1]!='\0') 16 | { 17 | str[j] = str[j+1]; 18 | j++; 19 | } 20 | chk = 1; 21 | } 22 | if(chk==0) 23 | i++; 24 | } 25 | printf("\nString (without spaces): %s", str); 26 | getch(); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /sudokuSolver.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // N is the size of the 2D matrix N*N 5 | #define N 9 6 | 7 | /* A utility function to print grid */ 8 | void print(int arr[N][N]) 9 | { 10 | for (int i = 0; i < N; i++) 11 | { 12 | for (int j = 0; j < N; j++) 13 | printf("%d ",arr[i][j]); 14 | printf("\n"); 15 | } 16 | } 17 | 18 | // Checks whether it will be legal 19 | // to assign num to the 20 | // given row, col 21 | int isSafe(int grid[N][N], int row, 22 | int col, int num) 23 | { 24 | 25 | // Check if we find the same num 26 | // in the similar row , we return 0 27 | for (int x = 0; x <= 8; x++) 28 | if (grid[row][x] == num) 29 | return 0; 30 | 31 | // Check if we find the same num in the 32 | // similar column , we return 0 33 | for (int x = 0; x <= 8; x++) 34 | if (grid[x][col] == num) 35 | return 0; 36 | 37 | // Check if we find the same num in the 38 | // particular 3*3 matrix, we return 0 39 | int startRow = row - row % 3, 40 | startCol = col - col % 3; 41 | 42 | for (int i = 0; i < 3; i++) 43 | for (int j = 0; j < 3; j++) 44 | if (grid[i + startRow][j + 45 | startCol] == num) 46 | return 0; 47 | 48 | return 1; 49 | } 50 | 51 | /* Takes a partially filled-in grid and attempts 52 | to assign values to all unassigned locations in 53 | such a way to meet the requirements for 54 | Sudoku solution (non-duplication across rows, 55 | columns, and boxes) */ 56 | int solveSudoku(int grid[N][N], int row, int col) 57 | { 58 | 59 | // Check if we have reached the 8th row 60 | // and 9th column (0 61 | // indexed matrix) , we are 62 | // returning true to avoid 63 | // further backtracking 64 | if (row == N - 1 && col == N) 65 | return 1; 66 | 67 | // Check if column value becomes 9 , 68 | // we move to next row and 69 | // column start from 0 70 | if (col == N) 71 | { 72 | row++; 73 | col = 0; 74 | } 75 | 76 | // Check if the current position 77 | // of the grid already contains 78 | // value >0, we iterate for next column 79 | if (grid[row][col] > 0) 80 | return solveSudoku(grid, row, col + 1); 81 | 82 | for (int num = 1; num <= N; num++) 83 | { 84 | 85 | // Check if it is safe to place 86 | // the num (1-9) in the 87 | // given row ,col ->we move to next column 88 | if (isSafe(grid, row, col, num)==1) 89 | { 90 | /* assigning the num in the 91 | current (row,col) 92 | position of the grid 93 | and assuming our assigned num 94 | in the position 95 | is correct */ 96 | grid[row][col] = num; 97 | 98 | // Checking for next possibility with next 99 | // column 100 | if (solveSudoku(grid, row, col + 1)==1) 101 | return 1; 102 | } 103 | 104 | // Removing the assigned num , 105 | // since our assumption 106 | // was wrong , and we go for next 107 | // assumption with 108 | // diff num value 109 | grid[row][col] = 0; 110 | } 111 | return 0; 112 | } 113 | 114 | int main() 115 | { 116 | // 0 means unassigned cells 117 | int grid[N][N] = { { 3, 0, 6, 5, 0, 8, 4, 0, 0 }, 118 | { 5, 2, 0, 0, 0, 0, 0, 0, 0 }, 119 | { 0, 8, 7, 0, 0, 0, 0, 3, 1 }, 120 | { 0, 0, 3, 0, 1, 0, 0, 8, 0 }, 121 | { 9, 0, 0, 8, 6, 3, 0, 0, 5 }, 122 | { 0, 5, 0, 0, 9, 0, 6, 0, 0 }, 123 | { 1, 3, 0, 0, 0, 0, 2, 5, 0 }, 124 | { 0, 0, 0, 0, 0, 0, 0, 7, 4 }, 125 | { 0, 0, 5, 2, 0, 6, 3, 0, 0 } }; 126 | 127 | if (solveSudoku(grid, 0, 0)==1) 128 | print(grid); 129 | else 130 | printf("No solution exists"); 131 | 132 | return 0; 133 | // This is code is contributed by Pradeep Mondal P 134 | } 135 | -------------------------------------------------------------------------------- /sum of digit.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int rem,s,n; 5 | scanf("%d", &n); 6 | while (n!=0) 7 | 8 | { 9 | rem = n%10; 10 | s= s+rem; 11 | n=n/10; 12 | } 13 | 14 | printf("%d",s); 15 | 16 | } 17 | -------------------------------------------------------------------------------- /swap.c: -------------------------------------------------------------------------------- 1 | //WACP to swap two integer numbers without using 3rd Variable. 2 | #include 3 | int main(){ 4 | int a,b; 5 | printf("Enter the numbers:\n"); 6 | scanf("%d%d",&a,&b); 7 | printf("value of a = %d \nvalue of b = %d\n",a,b); 8 | a=a+b; 9 | b=a-b; 10 | a=a-b; 11 | //b=(a+b)/2; 12 | //a=a-b; 13 | printf("After swaping the value of a = %d and the value of b = %d \n",a,b); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /time-in.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main () { 5 | time_t seconds; 6 | 7 | seconds = time(NULL); 8 | printf("Hours since october 25 , 2022 = %ld\n", seconds/3600); 9 | 10 | return(0); 11 | } 12 | -------------------------------------------------------------------------------- /time.c: -------------------------------------------------------------------------------- 1 | // C program to demonstrate 2 | // example of time() function. 3 | #include 4 | #include 5 | 6 | int main () 7 | { 8 | time_t seconds; 9 | 10 | seconds = time(NULL); 11 | printf("Seconds since January 1, 1970 = %ld\n", seconds); 12 | 13 | return(0); 14 | } 15 | -------------------------------------------------------------------------------- /vowel.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | char str[100]; 5 | char *p; 6 | int vCount=0,cCount=0; 7 | 8 | printf("Enter any string: "); 9 | fgets(str, 100, stdin); 10 | 11 | //assign base address of char array to pointer 12 | p=str; 13 | 14 | //'\0' signifies end of the string 15 | while(*p!='\0') 16 | { 17 | if(*p=='A' ||*p=='E' ||*p=='I' ||*p=='O' ||*p=='U' 18 | ||*p=='a' ||*p=='e' ||*p=='i' ||*p=='o' ||*p=='u') 19 | vCount++; 20 | else 21 | cCount++; 22 | //increase the pointer, to point next character 23 | p++; 24 | } 25 | 26 | printf("Number of Vowels in String: %d\n",vCount); 27 | printf("Number of Consonants in String: %d",cCount); 28 | return 0; 29 | } 30 | --------------------------------------------------------------------------------