├── Algorithms ├── Helloworld.c ├── Screenshot (18).png ├── Screenshot (19).png ├── string │ ├── inputstrings.c │ ├── check_alphabet.c │ ├── palindrome.c │ ├── dynamic_string_input.c │ ├── rev-string.c │ ├── Convert_case_of_letters.c │ ├── Reverse String.c │ ├── reverseofstringusingstack.c │ └── Longest_Common_subsequence.c ├── HELLOWORLD.C ├── add.c ├── WriteTable.c ├── factorial1.c ├── CountCharacter.c ├── Perfect.c ├── ArithmeticProgression.c ├── floyd's_triangle.c ├── Random_alphanumeric.c ├── swapping.c ├── Palindrome-number.c ├── Prime-number.c ├── matrix │ ├── CheckIdentityMatrix.c │ ├── Cartesian_Product.c │ ├── array.c │ ├── matrix.c │ ├── Array sum of n numbers.c │ ├── SparseMatrix.c │ ├── Two-dimensional array to store and print values │ ├── determinant_of_a_2x2_matrix.c │ ├── multiplication_of_matrix.c │ ├── Transpose of a matrix.c │ ├── matrixmultiply.c │ └── matrix-calculator.c ├── fibonacci.c ├── DECIMAL_TO_BINARY.c ├── factorial.c ├── lcm.c ├── Length Converter.c ├── towerofhanoi.c ├── ArmstrongNumber.c ├── Armstrong_Number.c ├── LCM_of_n_term.c ├── printing_strong_numbers.c ├── Calc_Telephone_Bill.c ├── N Queens Problem.c ├── set │ ├── intersection.c │ ├── sum_of_subset_algo_implementation.c │ └── SymmetricDifference.c ├── HCF_of_n_terms.c ├── Sieve_of_Atkin.c ├── Quick_Sort.c ├── file-manipulation │ └── file.c ├── queuequestion.c.txt ├── Mean_Mode_Median.c └── HEXADECIMAL_TO_OCTAL.c ├── Asnmt 6 - Recursion ├── 12Factorial ├── 13Binary.exe ├── 14Octal.exe ├── 3Odd_Num.exe ├── 7Squares.exe ├── 1N_NaturalNum ├── 5Even_Num.exe ├── 8Sum_N_num.exe ├── 12Factorial.exe ├── 1N_NaturalNum.exe ├── 4Odd_Num_Rev.exe ├── 6Even_Num_Rev.exe ├── 2N_NaturalNum_Rev.exe ├── 13Binary.cpp ├── 8Sum_N_num.cpp ├── 3Odd_Num.cpp ├── 5Even_Num.cpp ├── 6Even_Num_Rev.cpp ├── 2N_NaturalNum_Rev.cpp ├── 12Factorial.cpp ├── 4Odd_Num_Rev.cpp ├── 7Squares.cpp ├── 14Octal.cpp └── 1N_NaturalNum.cpp ├── data-structure ├── dynamic_allocation │ ├── malloc.c │ ├── Realloc │ │ ├── 1_malloc.c │ │ ├── 2_malloc.c │ │ ├── 1_calloc.c │ │ └── 2_calloc.c │ ├── calloc.c │ └── function_access_memory.c ├── seraching │ ├── linearsearch.c │ └── binarysearch.c ├── stack │ ├── Implementation of Stacks in C.c │ ├── Stack Using Linkedlist.c │ ├── Stack-Valid-Parenthesis.c │ ├── Stack-Tower-of-Hanoi.c │ ├── max-rectangle.cpp │ └── Infix_to_postfix.c ├── graph │ ├── Tree.c │ ├── All-pairs-shortest-path.c │ ├── dfs.c │ ├── Travelling Salesman Problem.c │ ├── Interval\-Tree.cpp │ ├── Tree-traversal.c │ └── Tree_Traversal.c ├── list │ ├── circular_singly_linked_list.c │ ├── Circular_Linked_List.c │ └── linkedlist.c ├── queue │ ├── circular_queue.c │ ├── queue_linkedlist.c │ └── queue_static.c └── array │ └── array.c ├── FactorialUsingRecursion.c ├── TypeCasting.c ├── README.md ├── average.c ├── Number triangle .c ├── ALTERADD.cpp ├── armstrong.c ├── fact_recursion.c ├── Fibonacci_recursion.c ├── BinarytoDecimal.c ├── Dynamic String Input.c ├── max.c ├── structure ├── 5.c ├── 6.c ├── 4.c ├── 2.c ├── 1.c └── 3.c ├── Print 1 to 100 numbers ├── neonNumber.c ├── sorting ├── insertionnew.c ├── bubble sort.c ├── Count_Sort.c ├── insertion-sort.c ├── bubble_sort.c ├── selection-sort.c ├── quick_sort.c ├── radix_sort.cpp ├── heap.c ├── InsertionSort.c ├── MergeSort.c ├── merge_sort.c ├── bucket_sort.c ├── reverse_queue.c ├── quick-sort.c └── Bucket Sort.cpp ├── sum_first_n_natural_no_recurssion.c ├── PerfectNum.c ├── Palindrome.c ├── Position_of_element_in_Array.c ├── Add Binary Numbers.c ├── codechef_MAHASENA.cpp ├── pronic__number.c ├── krishnamurty number.c ├── unique_elements.c ├── 2D_array3.c ├── harshad__number.c ├── prime.c ├── 2x2MatrixDeterminant.c ├── binary search .c ├── ducknumber.c ├── HCF_n_Numbers.c ├── sort an array ├── geometricprogression.c ├── evalpostfix.c ├── search_insearch ├── lar.c ├── fun_types.c ├── 3Left-Right-Rotation.cpp ├── heapsort.c ├── ref_swap.c ├── matrix multiplaction.c ├── linear queue.c ├── stack using linked list ├── magic_square.c ├── fcfs_arrivaltime.c ├── prim's algorithm.c ├── Queue linkedlist.c ├── sparseMatrix.cpp ├── delete all node of linkedlist.c └── string.c /Algorithms/Helloworld.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | printf("Hello World\n"); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /Algorithms/Screenshot (18).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nihal-Priyadarshi/C-Programs/HEAD/Algorithms/Screenshot (18).png -------------------------------------------------------------------------------- /Algorithms/Screenshot (19).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nihal-Priyadarshi/C-Programs/HEAD/Algorithms/Screenshot (19).png -------------------------------------------------------------------------------- /Asnmt 6 - Recursion/12Factorial: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nihal-Priyadarshi/C-Programs/HEAD/Asnmt 6 - Recursion/12Factorial -------------------------------------------------------------------------------- /Asnmt 6 - Recursion/13Binary.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nihal-Priyadarshi/C-Programs/HEAD/Asnmt 6 - Recursion/13Binary.exe -------------------------------------------------------------------------------- /Asnmt 6 - Recursion/14Octal.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nihal-Priyadarshi/C-Programs/HEAD/Asnmt 6 - Recursion/14Octal.exe -------------------------------------------------------------------------------- /Asnmt 6 - Recursion/3Odd_Num.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nihal-Priyadarshi/C-Programs/HEAD/Asnmt 6 - Recursion/3Odd_Num.exe -------------------------------------------------------------------------------- /Asnmt 6 - Recursion/7Squares.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nihal-Priyadarshi/C-Programs/HEAD/Asnmt 6 - Recursion/7Squares.exe -------------------------------------------------------------------------------- /Asnmt 6 - Recursion/1N_NaturalNum: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nihal-Priyadarshi/C-Programs/HEAD/Asnmt 6 - Recursion/1N_NaturalNum -------------------------------------------------------------------------------- /Asnmt 6 - Recursion/5Even_Num.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nihal-Priyadarshi/C-Programs/HEAD/Asnmt 6 - Recursion/5Even_Num.exe -------------------------------------------------------------------------------- /Asnmt 6 - Recursion/8Sum_N_num.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nihal-Priyadarshi/C-Programs/HEAD/Asnmt 6 - Recursion/8Sum_N_num.exe -------------------------------------------------------------------------------- /Asnmt 6 - Recursion/12Factorial.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nihal-Priyadarshi/C-Programs/HEAD/Asnmt 6 - Recursion/12Factorial.exe -------------------------------------------------------------------------------- /Asnmt 6 - Recursion/1N_NaturalNum.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nihal-Priyadarshi/C-Programs/HEAD/Asnmt 6 - Recursion/1N_NaturalNum.exe -------------------------------------------------------------------------------- /Asnmt 6 - Recursion/4Odd_Num_Rev.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nihal-Priyadarshi/C-Programs/HEAD/Asnmt 6 - Recursion/4Odd_Num_Rev.exe -------------------------------------------------------------------------------- /Asnmt 6 - Recursion/6Even_Num_Rev.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nihal-Priyadarshi/C-Programs/HEAD/Asnmt 6 - Recursion/6Even_Num_Rev.exe -------------------------------------------------------------------------------- /Asnmt 6 - Recursion/2N_NaturalNum_Rev.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nihal-Priyadarshi/C-Programs/HEAD/Asnmt 6 - Recursion/2N_NaturalNum_Rev.exe -------------------------------------------------------------------------------- /Algorithms/string/inputstrings.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | char st[50]; 5 | gets(st); 6 | puts(st); 7 | return 0; 8 | } -------------------------------------------------------------------------------- /Algorithms/HELLOWORLD.C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void main() 4 | { 5 | clrscr(); 6 | textbackground(RED); 7 | textcolor(YELLOW+BLINK); 8 | cprintf("HELLO WORLD"); 9 | getch(); 10 | } 11 | -------------------------------------------------------------------------------- /Algorithms/add.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void main() 4 | { 5 | int a,b,sum; 6 | clrscr(); 7 | printf("Enter Two Numbers:"); 8 | scanf("%d%d",&a,&b); 9 | sum=a+b; 10 | printf("Sum of %d and %d = %d",a,b,sum); 11 | getch(); 12 | } 13 | -------------------------------------------------------------------------------- /data-structure/dynamic_allocation/malloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | int* ptr = NULL; 7 | 8 | ptr = (int*)malloc(sizeof(int)); 9 | assert(ptr != NULL); 10 | 11 | *ptr = 100; 12 | 13 | free(ptr); 14 | return(0); 15 | } -------------------------------------------------------------------------------- /data-structure/dynamic_allocation/Realloc/1_malloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | int* ptr = NULL; 8 | 9 | ptr = (int*)malloc(sizeof(int)); 10 | assert(ptr != NULL); 11 | 12 | *ptr = 100; 13 | free(ptr); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /data-structure/dynamic_allocation/Realloc/2_malloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | int* ptr = NULL; 8 | 9 | ptr = (int*)malloc(sizeof(int)); 10 | assert(ptr != NULL); 11 | 12 | *ptr = 200; 13 | 14 | free(ptr); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /FactorialUsingRecursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | int fact(int n); 3 | int fact(int n){ 4 | if(n<=0) return 1; 5 | else return n*fact(n-1); 6 | } 7 | int main(){ 8 | int n; 9 | 10 | printf("ENTER NUMBER:\n"); 11 | scanf("%d",&n); 12 | 13 | printf("FACTORIAL IS : %d",fact(n)); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Algorithms/WriteTable.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n, i = 1; 6 | printf("Enter the number to write table for \n"); 7 | scanf("%d", &n); 8 | printf("\n"); 9 | while (i <= 10) 10 | { 11 | printf("%d * %d = %d\n", n, i, n * i); 12 | i++; 13 | } 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /TypeCasting.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | //IMPLICIT TYPE CASTING 4 | char c= 'A'; 5 | int i = c; 6 | //i stores ASCII value of character 'A' which is 65 7 | printf("%d\n",i); 8 | //EXPLICIT TYPE CASTING 9 | c=(char)(i+2); //65+2=67 10 | //c='C' whose ASCII value is 67 11 | printf("%c\n",c); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Algorithms/factorial1.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int num; 4 | printf("enter number:"); 5 | scanf("%d",&num); 6 | int ans=fact(num); 7 | printf("the factorial of %d is:%d",num,ans); 8 | } 9 | int fact(int num){ 10 | if(num==0){ 11 | return 1; 12 | } 13 | if(num==1){ 14 | return 1; 15 | } 16 | else{ 17 | return num*fact(num-1); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C-Programs 2 | 3 | Here are all types of C program, you can also add some by doing pull request. 4 | 5 |

6 | 7 | 8 | # :handshake: Our Contributors 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Algorithms/CountCharacter.c: -------------------------------------------------------------------------------- 1 | /* C Program to Count Number of Characters in a string */ 2 | 3 | #include 4 | int main(){ 5 | char str[20]; 6 | int i=0; 7 | printf("\nEnter the string: "); 8 | gets(str); 9 | while(str[i]!='\0') 10 | i++; 11 | printf("\nLength of [ %s ] is [ %d ]\n", str, i); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Algorithms/string/check_alphabet.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | char c; 4 | printf("Enter a character: "); 5 | scanf("%c", &c); 6 | 7 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 8 | printf("%c is an alphabet.", c); 9 | else 10 | printf("%c is not an alphabet.", c); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /average.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main () { 4 | int num1 , num2 , num3 , sum; 5 | float avg; 6 | printf("Enter 3 numbers\n"); 7 | scanf(" %d%d%d", &num1 ,&num2, &num3); 8 | 9 | sum = num1 + num2 + num3; 10 | avg = sum / (float)3; 11 | 12 | printf("Average of %d , %d and %d is %.2f", num1 , num2, num3, avg); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Algorithms/Perfect.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n,i,s=0; 5 | printf("Enter number "); 6 | scanf("%d",&n); 7 | for(i=1;i 2 | int main() 3 | { 4 | int a,d,n,s; 5 | printf("Enter First Element:"); 6 | scanf("%d",&a); 7 | printf("Enter Common Difference:"); 8 | scanf("%d",&d); 9 | printf("Enter the element to find:"); 10 | scanf("%d",&n); 11 | s=a+(n-1)*d; 12 | printf("The %dth term is %d",n,s); 13 | } 14 | -------------------------------------------------------------------------------- /Number triangle .c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | int i,j,k,l,n; 6 | system("cls"); 7 | printf("enter the range="); 8 | scanf("%d",&n); 9 | { 10 | { 11 | printf(" "); 12 | } 13 | { 14 | printf("%d",k); 15 | } 16 | for(l=i-1;l>=1;l--) 17 | { 18 | printf("%d",l); 19 | } 20 | printf("\n"); 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /ALTERADD.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int t; 6 | cin >> t; 7 | while(t--){ 8 | long long a, b; 9 | cin >> a >> b; 10 | long long lol = b-a; 11 | if(lol%3 == 2){ 12 | cout << "NO" << endl; 13 | } 14 | else{ 15 | cout << "YES" << endl; 16 | } 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Algorithms/floyd's_triangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int rows, i, j, number = 1; 4 | printf("Enter the number of rows: "); 5 | scanf("%d", &rows); 6 | for (i = 1; i <= rows; i++) { 7 | for (j = 1; j <= i; ++j) { 8 | printf("%d ", number); 9 | ++number; 10 | } 11 | printf("\n"); 12 | } 13 | return 0; 14 | } -------------------------------------------------------------------------------- /data-structure/dynamic_allocation/Realloc/1_calloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | int* ptr = NULL; 8 | int size= 5; 9 | int i; 10 | 11 | ptr = (int*)calloc(size, sizeof(int)); 12 | assert(ptr != NULL); 13 | 14 | for(i = 0; i < size; i++) 15 | ptr[i] = (i+1)*5; 16 | 17 | free(ptr); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /data-structure/dynamic_allocation/Realloc/2_calloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | int* ptr = NULL; 8 | int size= 5; 9 | int i; 10 | 11 | ptr = (int*)calloc(size, sizeof(int)); 12 | assert(ptr != NULL); 13 | 14 | for(i = 0; i < size; i++) 15 | ptr[i] = (i+1)*100; 16 | 17 | free(ptr); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Algorithms/string/palindrome.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n,r,sum=0,temp; 5 | printf("enter the number="); 6 | scanf("%d",&n); 7 | temp=n; 8 | while(n>0) 9 | { 10 | r=n%10; 11 | sum=(sum*10)+r; 12 | n=n/10; 13 | } 14 | if(temp==sum) 15 | printf("palindrome number "); 16 | else 17 | printf("not palindrome"); 18 | return 0; 19 | } -------------------------------------------------------------------------------- /armstrong.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n,r,sum=0,temp; 5 | printf("enter the number="); 6 | scanf("%d",&n); 7 | temp=n; 8 | while(n>0) 9 | { 10 | r=n%10; 11 | sum=sum+(r*r*r); 12 | n=n/10; 13 | } 14 | if(temp==sum) 15 | printf("armstrong number "); 16 | else 17 | printf("not armstrong number"); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Algorithms/Random_alphanumeric.c: -------------------------------------------------------------------------------- 1 | // This C program is used to generate random alphanumeric character 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | int main() 8 | { 9 | char a[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; 10 | int l = strlen(a); 11 | srand(time(NULL)); 12 | printf("%c\n", a[rand() % l]); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Algorithms/swapping.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | int main() 7 | { 8 | int a,b; 9 | printf ("Enter the first number "); 10 | scanf ("%d", &a); 11 | 12 | printf ("Enter the first number "); 13 | scanf ("%d", &b); 14 | 15 | printf ("The swapped numbers are: %d, %d", (a+b)-a, (a+b)-b); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Algorithms/Palindrome-number.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n,r,sum=0,temp; 5 | printf("enter a number="); 6 | scanf("%d",&n); 7 | temp=n; 8 | while(n>0) 9 | { 10 | r=n%10; 11 | sum=(sum*10)+r; 12 | n=n/10; 13 | } 14 | if(temp==sum) 15 | printf("This is a palindrome number "); 16 | else 17 | printf("This is not a palindrome number"); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Algorithms/Prime-number.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int loop, number; 5 | int prime = 1; 6 | 7 | number = 11; 8 | 9 | for(loop = 2; loop < number; loop++) { 10 | if((number % loop) == 0) { 11 | prime = 0; 12 | } 13 | } 14 | 15 | if (prime == 1) 16 | printf("%d is prime number.", number); 17 | else 18 | printf("%d is not a prime number.", number); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Algorithms/matrix/CheckIdentityMatrix.c: -------------------------------------------------------------------------------- 1 | #include 2 | int identity(int num){ 3 | int row, col; 4 | for (row = 0; row < num; row++){ 5 | for (col = 0; col < num; col++){ 6 | if (row == col) 7 | printf("%d ", 1); 8 | else 9 | printf("%d ", 0); 10 | } 11 | printf("\n"); 12 | } 13 | return 0; 14 | } 15 | int main(){ 16 | int size = 4; 17 | identity(size); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /fact_recursion.c: -------------------------------------------------------------------------------- 1 | //A Program to find factorial using recursion 2 | #include 3 | int fact(int x); 4 | int main() 5 | { 6 | int n; 7 | printf("Enter the value of number --> "); 8 | scanf("%d",&n); 9 | printf("%d",fact(n)); 10 | } 11 | int fact(int x) 12 | { 13 | int res=1; 14 | if(x==0) 15 | { 16 | res*=1; 17 | } 18 | else 19 | { 20 | res = x*fact(x-1); 21 | } 22 | return res; 23 | 24 | } -------------------------------------------------------------------------------- /Fibonacci_recursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | int fib(int limit, int f, int s,int i); 3 | int main() 4 | { 5 | int z,f=0,s=1,it=0; 6 | printf("Enter how many number --> "); 7 | scanf("%d",&z); 8 | printf("0 1 "); 9 | fib(z-2,f,s,it); 10 | } 11 | int fib(int limit,int f, int s, int i) 12 | { 13 | int j,t; 14 | if (i 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | int ptr = NULL; 8 | int size = 5; 9 | int i; 10 | 11 | ptr = (int*)calloc(size, sizeof(int)); 12 | assert(ptr != NULL); 13 | 14 | for(i = 0; i < 5; i++) 15 | { 16 | ptr[i] = (i + 1)*5; 17 | printf("ptr[%d] : %d", i, *ptr); 18 | } 19 | 20 | free(ptr); 21 | return(0); 22 | } -------------------------------------------------------------------------------- /BinarytoDecimal.c: -------------------------------------------------------------------------------- 1 | //A program to convert Binary to Decimal 2 | 3 | #include 4 | int main(){ 5 | int decimal =0,binary,base=1,rem,num; 6 | printf("Enter the binary number:"); 7 | scanf("%d",&binary); 8 | num=binary; 9 | while(binary!=0){ 10 | rem=binary%10; 11 | decimal=decimal +rem*base; 12 | binary=binary/10; 13 | base=base*2; 14 | } 15 | printf("Decimal equivalent of the binary number %d is : %d",num,decimal); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Algorithms/fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | int fib (int n) 3 | { 4 | if (n==1 || n==2) 5 | { 6 | return n-1; 7 | } 8 | 9 | else 10 | return fib(n-1) + fib(n-2); 11 | } 12 | 13 | int main () 14 | { 15 | int x; 16 | printf ("Enter the position number you want to have of fibonacci series: \n"); 17 | scanf ("%d", &x); 18 | printf ("The %d placed member of fibonacci series is %d", x, fib(x)); 19 | 20 | } -------------------------------------------------------------------------------- /Algorithms/DECIMAL_TO_BINARY.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | int decnum, binnum[50], i=0; 6 | printf("Enter any decimal number: "); 7 | scanf("%d", &decnum); 8 | while(decnum!=0) 9 | { 10 | binnum[i] = decnum%2; 11 | i++; 12 | decnum = decnum/2; 13 | } 14 | printf("\nEquivalent Binary Value = "); 15 | for(i=(i-1); i>=0; i--) 16 | printf("%d", binnum[i]); 17 | getch(); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Asnmt 6 - Recursion/13Binary.cpp: -------------------------------------------------------------------------------- 1 | // Write a recursive function to print binary of a decimal number. 2 | #include 3 | using namespace std; 4 | void Binary(int n, int rem) 5 | { 6 | if(n>0) 7 | { 8 | Binary(n/2, rem = n % 2); 9 | } 10 | else 11 | { 12 | return; 13 | } 14 | cout << rem << "\t"; 15 | } 16 | int main() 17 | { 18 | int n, rem=0; 19 | cout << "Enter number: "; 20 | cin >> n; 21 | Binary(n, rem); 22 | } -------------------------------------------------------------------------------- /Dynamic String Input.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void main() 6 | { 7 | char c='a'; 8 | char *ch = (char *) malloc (1*sizeof(char)); 9 | int i=0; 10 | printf("\nEnter your string: "); 11 | while(c != '\r') 12 | { 13 | c = getche(); 14 | 15 | if(c != '\r') 16 | { 17 | ch = (char *) realloc (ch,(i+1)*sizeof(char)); 18 | ch[i++] = c; 19 | } 20 | } 21 | ch[i] = '\0'; 22 | printf("\nEntered String: "); 23 | puts(ch); 24 | getch(); 25 | } 26 | -------------------------------------------------------------------------------- /max.c: -------------------------------------------------------------------------------- 1 | //A Program to find maximum of two numbers 2 | #include 3 | int max(int x, int y); //Function prototype 4 | int main() 5 | { 6 | int a,b; 7 | printf("Enter the two numbers --> "); 8 | scanf("%d%d",&a,&b); 9 | printf("The maximum of the two numbers %d %d is %d",a,b,max(a,b)); 10 | } 11 | //Function definition 12 | int max(int x , int y) 13 | { 14 | if(x>y) 15 | { 16 | return x; 17 | } 18 | else 19 | { 20 | return y; 21 | } 22 | 23 | } -------------------------------------------------------------------------------- /Algorithms/string/dynamic_string_input.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void main() 6 | { 7 | char c='a'; 8 | char *ch = (char *) malloc (1*sizeof(char)); 9 | int i=0; 10 | printf("\nEnter your string: "); 11 | while(c != '\r') 12 | { 13 | c = getche(); 14 | 15 | if(c != '\r') 16 | { 17 | ch = (char *) realloc (ch,(i+1)*sizeof(char)); 18 | ch[i++] = c; 19 | } 20 | } 21 | ch[i] = '\0'; 22 | printf("\nEntered String: "); 23 | puts(ch); 24 | getch(); 25 | } 26 | -------------------------------------------------------------------------------- /Algorithms/factorial.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int n, i; 4 | unsigned long long fact = 1; 5 | printf("Enter an integer: "); 6 | scanf("%d", &n); 7 | 8 | // shows error if the user enters a negative integer 9 | if (n < 0) 10 | printf("Error! Factorial of a negative number doesn't exist."); 11 | else { 12 | for (i = 1; i <= n; ++i) { 13 | fact *= i; 14 | } 15 | printf("Factorial of %d = %llu", n, fact); 16 | } 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /structure/5.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct distance 4 | { 5 | float x1,y1,x2,y2; 6 | }x; 7 | 8 | int main() { 9 | float dist; 10 | printf("Enter the 1st points --> "); 11 | scanf("%f %f", &x.x1, &x.y1); 12 | printf("Enter the 2nd points --> "); 13 | scanf("%f %f", &x.x2, &x.y2); 14 | dist = sqrt((x.x2 - x.x1)*(x.x2 - x.x1) + (x.y2 - x.y1)*(x.y2 - x.y1)); 15 | printf("The distance between the points (%0.01f,%0.01f) and (%0.01f,%0.01f) is %0.01f", x.x1, x.y1, x.x2, x.y2, dist); 16 | } 17 | -------------------------------------------------------------------------------- /Asnmt 6 - Recursion/8Sum_N_num.cpp: -------------------------------------------------------------------------------- 1 | // Write a recursive function to calculate sum of first N natural numbers. 2 | #include 3 | using namespace std; 4 | int Sum_num(int n) 5 | { 6 | static int sum; 7 | if(n) 8 | { 9 | Sum_num(n-1); 10 | } 11 | else 12 | { 13 | return 0; 14 | } 15 | return sum = sum + n; 16 | } 17 | int main() 18 | { 19 | int n; 20 | cout << "Sum number Till : "; 21 | cin >> n; 22 | n = Sum_num(n); 23 | cout << n << endl; 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Print 1 to 100 numbers: -------------------------------------------------------------------------------- 1 | /* 2 | ============================================================================ 3 | Name : print.c 4 | Author : LIJIN DANIEL JOSEPH 5 | Version : 6 | Copyright : Your copyright notice 7 | Description : Hello World in C, Ansi-style 8 | ============================================================================ 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | int main(void) { 15 | int i; 16 | for(i=1;i<=100;i++){ 17 | 18 | printf("%d\n",i);} 19 | return EXIT_SUCCESS; 20 | } 21 | -------------------------------------------------------------------------------- /Algorithms/lcm.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int num1, num2, max; 6 | 7 | printf("Enter two numbers:"); 8 | scanf("%d %d",&num1,&num2); 9 | if(num1 > num2) 10 | { 11 | max = num1; 12 | } 13 | else 14 | { 15 | max = num2; 16 | } 17 | 18 | while(1) 19 | { 20 | if (max % num1 == 0 && max % num2 == 0) 21 | { 22 | printf("The LCM of %d and %d = %d",num1, num2, max); 23 | break; 24 | } 25 | max++; 26 | 27 | } 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Asnmt 6 - Recursion/3Odd_Num.cpp: -------------------------------------------------------------------------------- 1 | // Write a recursive function to print first N odd natural numbers. 2 | #include 3 | using namespace std; 4 | void Odd_Num(int n, int value, int odd); 5 | int main() 6 | { 7 | int n, value=1, odd=1; 8 | cout << "How many odd number will be print: "; 9 | cin >> n; 10 | Odd_Num(n, value, odd); 11 | } 12 | void Odd_Num(int n, int value, int odd) 13 | { 14 | if(value<=n) 15 | { 16 | cout << odd << "\t"; 17 | odd += 2; 18 | Odd_Num(n, value+1, odd); 19 | } 20 | return; 21 | } -------------------------------------------------------------------------------- /Asnmt 6 - Recursion/5Even_Num.cpp: -------------------------------------------------------------------------------- 1 | // Write a recursive function to print first N even natural numbers. 2 | #include 3 | using namespace std; 4 | void Even_Num(int n, int count, int even); 5 | int main() 6 | { 7 | int n, count=1, even=2; 8 | cout << "How many even number will be print: "; 9 | cin >> n; 10 | Even_Num(n, count, even); 11 | return 0; 12 | } 13 | void Even_Num(int n, int count, int even) 14 | { 15 | if(count<=n) 16 | { 17 | cout << even << "\t"; 18 | Even_Num(n, count+1, even+2); 19 | } 20 | return; 21 | } -------------------------------------------------------------------------------- /Asnmt 6 - Recursion/6Even_Num_Rev.cpp: -------------------------------------------------------------------------------- 1 | // Write a recursive function to print first N even natural numbers in reverse order. 2 | #include 3 | using namespace std; 4 | void Even_Num_Rev(int n, int count, int even) 5 | { 6 | if(count <= n) 7 | { 8 | Even_Num_Rev(n, count+1, even+2); 9 | } 10 | else 11 | return; 12 | cout << even << "\t"; 13 | } 14 | int main() 15 | { 16 | int n, count=1, even=2; 17 | cout << "How many even value will be print: "; 18 | cin >> n; 19 | Even_Num_Rev(n, count, even); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Asnmt 6 - Recursion/2N_NaturalNum_Rev.cpp: -------------------------------------------------------------------------------- 1 | // Write a recursive function to print first N natural numbers in reverse order. 2 | #include 3 | using namespace std; 4 | void N_number_rev(int n, int start); 5 | int main() 6 | { 7 | int n, start=1; 8 | cout << "Print Number till: "; 9 | cin >> n; 10 | N_number_rev(n, start); 11 | } 12 | void N_number_rev(int n, int start) 13 | { 14 | if(start<=n) 15 | { 16 | N_number_rev(n, start+1); 17 | } 18 | else 19 | { 20 | return; 21 | } 22 | cout << start << "\t"; 23 | } -------------------------------------------------------------------------------- /neonNumber.c: -------------------------------------------------------------------------------- 1 | //A neon number is a number where the sum of digits of the square of the number is equal to the number. 2 | #include 3 | int main(){ 4 | int n,sqr,sum=0; 5 | //Take input from the user 6 | printf("ENTER THE NUMBER\n"); 7 | scanf("%d",&n); 8 | //Squaring the number 9 | sqr=n*n; 10 | //Find the sum of digits of the square of the number 11 | while(sqr!=0){ 12 | int d=sqr%10; 13 | sum+=d; 14 | sqr/=10; 15 | } 16 | if(sum==n) 17 | printf("%d is a Neon Number",n); 18 | else 19 | printf("%d is not a Neon Number",n); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Asnmt 6 - Recursion/12Factorial.cpp: -------------------------------------------------------------------------------- 1 | // Write a recursive function to calculate factorial of a number. 2 | #include 3 | using namespace std; 4 | int Factorial(int n); 5 | int main() 6 | { 7 | int n; 8 | cout << "Enter a number: "; 9 | cin >> n; 10 | n = Factorial(n); 11 | cout << n << endl; 12 | return 0; 13 | } 14 | int Factorial(int n) 15 | { 16 | if(n==0) 17 | { 18 | return 1; 19 | } 20 | int so = Factorial(n-1); 21 | cout << "n " << n << " so " << so << endl; // for better understanding 22 | return n * so; 23 | } -------------------------------------------------------------------------------- /sorting/insertionnew.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void insertion(int *a,int n) 5 | { 6 | int i,j; 7 | for(i=1;i 0 && y < a[j-1]) 13 | { 14 | a[j]=a[j-1]; 15 | j--; 16 | } 17 | a[j]=y; 18 | } 19 | } 20 | 21 | void main() 22 | { 23 | int *a,i,n; 24 | printf("Enter n : "); 25 | scanf("%d",&n); 26 | a=(int *)malloc(n*sizeof(int)); 27 | for(i=0;i 3 | using namespace std; 4 | void Odd_Num_Rev(int n, int count, int odd) 5 | { 6 | if(count<=n) 7 | { 8 | Odd_Num_Rev(n, count+1, odd+2); 9 | } 10 | else 11 | { 12 | return; 13 | } 14 | cout << odd << "\t"; 15 | } 16 | int main() 17 | { 18 | int n, count=1, odd=1; 19 | cout << "How many odd number will be print: "; 20 | cin >> n; 21 | Odd_Num_Rev(n, count, odd); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Asnmt 6 - Recursion/7Squares.cpp: -------------------------------------------------------------------------------- 1 | // Write a recursive function to print squares of first N natural numbers. 2 | #include 3 | #include 4 | int Squares(int n); 5 | using namespace std; 6 | int main() 7 | { 8 | int n; 9 | cout << "Print number till: "; 10 | cin >> n; 11 | n = Squares(n); 12 | cout << n << endl; 13 | return 0; 14 | } 15 | int Squares(int n) 16 | { 17 | static int sum; 18 | if(n==0) 19 | { 20 | return 1; 21 | } 22 | else 23 | { 24 | Squares(n-1); 25 | } 26 | return sum = sum + (n * n); 27 | } -------------------------------------------------------------------------------- /sum_first_n_natural_no_recurssion.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int n_natural_sum(int n); 4 | 5 | int main(int argc, char const *argv[]) 6 | { 7 | int n, sum; 8 | printf("Enter the value of n "); 9 | scanf("%d", &n); 10 | sum = n_natural_sum(n); 11 | printf("Sum of first n natural numbers is: %d", sum); 12 | return 0; 13 | } 14 | 15 | int n_natural_sum(int n) 16 | { 17 | int sum = 0; 18 | if (n == 1) 19 | { 20 | sum += 1; 21 | return sum; 22 | } 23 | else 24 | { 25 | sum += n_natural_sum(n - 1) + n; 26 | } 27 | return sum; 28 | } -------------------------------------------------------------------------------- /PerfectNum.c: -------------------------------------------------------------------------------- 1 | //Perfect number is a positive integer that is equal to the sum of all its proper positive divisors excluding the number itself. 2 | //Ex: 6 is a perfect number 3 | 4 | #include 5 | #include 6 | int main(){ 7 | int number; 8 | printf("Enter the number:"); 9 | scanf("%d",&number); 10 | int i,rem,sum=0; 11 | for(i=1;i 4 | #include 5 | int main() 6 | { 7 | int rem,res,quo,num; 8 | printf("Enter the number:"); 9 | scanf("%d",&num); 10 | quo=num; 11 | for(res=0;quo!=0;) 12 | { 13 | rem=quo%10; 14 | quo=quo/10; 15 | res=res*10+rem; 16 | } 17 | if(res==num) 18 | printf("\nThe entered number is a palindrome"); 19 | else 20 | printf("\nThe entered number is not a palindrome"); 21 | getch(); 22 | } 23 | -------------------------------------------------------------------------------- /Algorithms/Length Converter.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | float Millimeter, Centimeter, Meter, Kilometer; 6 | 7 | printf("\n Please enter your value in km : "); 8 | scanf("%f", &Kilometer); 9 | 10 | Meter = Kilometer * 1000.0; 11 | Centimeter = Kilometer * 100000.0; 12 | Millimeter = Kilometer * 1000000.0; 13 | 14 | printf("\n %.2f Kilometers = %.2f Meters", Kilometer, Meter); 15 | printf("\n %.2f Kilometers = %.2f Centimeters", Kilometer, Centimeter); 16 | printf("\n %.2f Kilometers = %.2f Millimeters", Kilometer, Millimeter); 17 | 18 | return 0; 19 | -------------------------------------------------------------------------------- /Asnmt 6 - Recursion/14Octal.cpp: -------------------------------------------------------------------------------- 1 | // Write a recursive function to print octal of a decimal number. 2 | #include 3 | using namespace std; 4 | int Octal(int n, int rem) 5 | { 6 | static int a; 7 | if(n>0) 8 | { 9 | Octal(n/8, rem = n%8); 10 | } 11 | else 12 | { 13 | return 1; 14 | } 15 | return a = (a * 10) + rem;; 16 | } 17 | int main() 18 | { 19 | int n, rem=0; 20 | cout << "Enter a number: "; 21 | cin >> n; 22 | n = Octal(n, rem); 23 | cout << n << endl; 24 | return 0; 25 | } -------------------------------------------------------------------------------- /structure/6.c: -------------------------------------------------------------------------------- 1 | #include 2 | struct std{ 3 | int eno[50],sal[50]; 4 | char nm[50][50]; 5 | 6 | }s; 7 | int main() { 8 | int i,n; 9 | printf("How many employees are there\n"); 10 | scanf("%d",&n); 11 | for (int i = 0; i %d\n", s.eno[i], s.nm[i], s.sal[i]); 18 | } 19 | printf("----------------\n"); 20 | 21 | 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /structure/4.c: -------------------------------------------------------------------------------- 1 | #include 2 | struct std{ 3 | int roll[50],marks[50]; 4 | char nm[50][50]; 5 | 6 | }s; 7 | int main() { 8 | int i,n; 9 | printf("How many student are there\n"); 10 | scanf("%d",&n); 11 | for (int i = 0; i %d\n", s.roll[i], s.nm[i], s.marks[i]); 18 | } 19 | printf("----------------\n"); 20 | 21 | 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /Position_of_element_in_Array.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int n, i, r, arr[100]; 4 | printf("Enter the number of elements in the array: "); 5 | scanf("%d", &n); 6 | 7 | printf("Enter the elements in array: "); 8 | for (i = 0; i < n; i++) { 9 | scanf("%d", &arr[i]); 10 | } 11 | 12 | printf("Enter the item to be searched: "); 13 | scanf("%d", &r); 14 | i = 0; 15 | while (r != arr[i]) { 16 | i++; 17 | } 18 | 19 | if (i < n) { 20 | printf("The element is found at the position = %d", (i + 1)); 21 | } 22 | 23 | else { 24 | printf("Element not found!"); 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Add Binary Numbers.c: -------------------------------------------------------------------------------- 1 | char * addBinary(char * a, char * b){ 2 | int sizeA = strlen(a); 3 | int sizeB = strlen(b); 4 | int sizeOutput = (sizeA > sizeB ? sizeA : sizeB) + 1; 5 | char * output = (char *)malloc(sizeOutput + 1); 6 | int sum = 0; 7 | 8 | output[sizeOutput] = '\0'; 9 | 10 | while(sizeA > 0 || sizeB > 0 || sum > 0) { 11 | 12 | if(sizeA > 0) { 13 | sum += a[--sizeA] - '0'; 14 | } 15 | if(sizeB > 0) { 16 | sum += b[--sizeB] - '0'; 17 | } 18 | output[--sizeOutput] = sum % 2 + '0'; 19 | sum /= 2; 20 | } 21 | return output + sizeOutput; 22 | } 23 | -------------------------------------------------------------------------------- /codechef_MAHASENA.cpp: -------------------------------------------------------------------------------- 1 | //Solution for codechef MAHASENA problem 2 | 3 | #include 4 | using namespace std; 5 | 6 | string getResult(int arr[],int num){ 7 | int oddCounter=0,evenCounter=0; 8 | for(int j=0;joddCounter?"READY FOR BATTEL":"NOT READY"; 17 | } 18 | 19 | int main(){ 20 | int num; 21 | cin>>num; 22 | int arr[num]; 23 | for(int i=0;i>arr[i]; 25 | } 26 | cout< 3 | # include 4 | 5 | 6 | 7 | int main() 8 | { 9 | int prno,i,n,flg; 10 | printf(" Input a number: "); 11 | scanf("%d",&prno); 12 | for(i=1;i<=prno;i++) 13 | { 14 | if(i*(i+1)==prno) 15 | { 16 | flg=1; 17 | break; 18 | } 19 | } 20 | 21 | if(flg==1) 22 | { 23 | printf(" The given number is a Pronic Number.\n"); 24 | } 25 | else 26 | { 27 | printf(" The given number is not a Pronic Number.\n"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Algorithms/matrix/Cartesian_Product.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int i,j,n1,n2,S1[20],S2[20]; 5 | printf("Enter the no of element you want in Set1"); 6 | scanf("%d",&n1); 7 | printf("Enter the elements in the Set1"); 8 | for(i=0;i 2 | #include 3 | void read_array(int arr[], int); 4 | void display_array(int arr[], int); 5 | 6 | int main() 7 | { 8 | int num[10],n; 9 | clrscr(); 10 | printf("Enter the size of the array:"); 11 | scanf("%d",&n); 12 | read_array(num,n); 13 | display_array(num,n); 14 | getch(); 15 | return(0); 16 | } 17 | void read_array(int arr[10], int n) 18 | { 19 | int i; 20 | printf("\n Enter the %d elements of the array:",n); 21 | for(i=0;i 2 | void towerofhanoi( int n,char S, char M,char D)/*Source =S Middle = M Destination D of the disk*/ 3 | { 4 | if(n==1) 5 | { 6 | printf("%d TRANSFER DISK FROM %c TO %c\n",n,S,D); 7 | } 8 | else 9 | { 10 | towerofhanoi(n-1,S,D,M); 11 | 12 | printf("%d TRANSFER DISK FROM %c to %c\n",n,S,D); 13 | 14 | towerofhanoi(n-1,M,S,D); 15 | } 16 | } 17 | void main() 18 | { 19 | int n; 20 | printf("/*Source =S Middle = M Destination D the disk*/ \n"); 21 | printf("ENTER THE ELEMENT: "); 22 | scanf("%d",&n); 23 | 24 | towerofhanoi(n,'S','M','D'); 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Algorithms/matrix/matrix.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | char operator; 5 | scanf("%c", &operator); 6 | float a,b; 7 | printf("Enter the numebr a and b\n"); 8 | scanf("%f%f", &a,&b); 9 | switch(operator) 10 | { case '+': 11 | {printf("The result is %f\n",a+b); 12 | break;} 13 | case '-': 14 | { 15 | printf("The result is %f",a-b); 16 | break; 17 | 18 | } 19 | case'*': 20 | { 21 | printf("The result is %f\n", a*b); 22 | break; 23 | } 24 | case '/': 25 | { 26 | printf("The result is %f\n",a/b); 27 | break; 28 | } 29 | default: 30 | { 31 | printf("Please enter a correct input\n"); 32 | 33 | } 34 | return 0; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /krishnamurty number.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | int num, i, rem, temp, fact, sum = 0; 4 | 5 | // Asking for input 6 | printf("Enter a number: "); 7 | scanf("%d", &num); 8 | 9 | for (temp = num; temp > 0; temp = temp/10){ 10 | fact = 1; 11 | rem = temp % 10; 12 | 13 | for (i = 1; i <= rem; i++){ 14 | fact = i * fact; 15 | } 16 | sum = sum + fact; 17 | } 18 | 19 | // Checking for krishnamurthy number 20 | if (num == sum){ 21 | printf("%d is a krishnamurthy number.", num); 22 | } 23 | else{ 24 | printf("%d is not a krishnamurthy number.", num); 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Algorithms/ArmstrongNumber.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int num, originalNum, remainder, result = 0; 4 | printf("Enter a three-digit integer: "); 5 | scanf("%d", &num); 6 | originalNum = num; 7 | 8 | while (originalNum != 0) { 9 | // remainder contains the last digit 10 | remainder = originalNum % 10; 11 | 12 | result += remainder * remainder * remainder; 13 | 14 | // removing last digit from the orignal number 15 | originalNum /= 10; 16 | } 17 | 18 | if (result == num) 19 | printf("%d is an Armstrong number.", num); 20 | else 21 | printf("%d is not an Armstrong number.", num); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Algorithms/Armstrong_Number.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int num, originalNum, remainder, result = 0; 4 | printf("Enter a three-digit integer: "); 5 | scanf("%d", &num); 6 | originalNum = num; 7 | 8 | while (originalNum != 0) { 9 | // remainder contains the last digit 10 | remainder = originalNum % 10; 11 | 12 | result += remainder * remainder * remainder; 13 | 14 | // removing last digit from the orignal number 15 | originalNum /= 10; 16 | } 17 | 18 | if (result == num) 19 | printf("%d is an Armstrong number.", num); 20 | else 21 | printf("%d is not an Armstrong number.", num); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /sorting/bubble sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int a[20],i,n,j, temp; 5 | 6 | printf("ENTER THE NO OF ELEMENT: "); 7 | scanf("%d",&n); 8 | printf("ENTER THE ELEMENT: "); 9 | for(i=0;ia[j+1]) 18 | { 19 | temp=a[j]; 20 | a[j]=a[j+1]; 21 | a[j+1]=temp; 22 | } 23 | } 24 | } 25 | printf(" SORTED ELEMENT: "); 26 | for(i=0;i 2 | #include 3 | int uniqueEle(int array[], int n){ 4 | int i,j; 5 | int count = 1; 6 | for(i = 0; i < n; i++){ 7 | for(j = 0; j < n; j++){ 8 | if(array[i] == array[j] && i != j) 9 | break; 10 | } 11 | if(j == n ){ 12 | printf("\nunique elements in an array is [%d] : %d \n",count,array[i]); 13 | ++count; 14 | } 15 | } 16 | return -1; 17 | } 18 | int main(){ 19 | int n,i; 20 | printf("\nEnter no: of elements : "); 21 | scanf("%d",&n); 22 | int array[n]; 23 | printf("\nenter the array elements : "); 24 | for(i = 0; i < n; i++){ 25 | scanf("%d",&array[i]); 26 | } 27 | uniqueEle(array, n); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /data-structure/seraching/linearsearch.c: -------------------------------------------------------------------------------- 1 | //A program to implement linear search 2 | #include 3 | int main() 4 | { 5 | int var,ar[50],n,i,flag=0; 6 | printf("Enter the size of the array\n"); 7 | scanf("%d",&n); 8 | for(i=0;i 3 | int main() 4 | { 5 | int i,j,x,y; 6 | printf("*** Welcome to 2D array ***"); 7 | printf("\nHow many rows : "); 8 | scanf("%d", &x); 9 | printf("\nHow many columns : "); 10 | scanf("%d", &y); 11 | int arr[x][y]; 12 | printf("\nYou have selected %d rows and %d columns ", x,y); 13 | printf("\nNow Enter %d values : ", x*y); 14 | for(i=0; i 3 | 4 | int main() 5 | { int num; 6 | printf("Enter a number\n"); 7 | scanf("%d",&num); 8 | int rem = 0, sum = 0, n; 9 | 10 | //Make a copy of num and store it in variable n 11 | n = num; 12 | 13 | //Calculates sum of digits 14 | while(num > 0){ 15 | rem = num%10; 16 | sum = sum + rem; 17 | num = num/10; 18 | } 19 | 20 | //Checks whether number is divisible by sum of digits 21 | if(n%sum == 0) 22 | printf("%d is a harshad number", n); 23 | else 24 | printf("%d is not a harshad number", n); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Algorithms/matrix/Array sum of n numbers.c: -------------------------------------------------------------------------------- 1 | /* 2 | ============================================================================ 3 | Name : array.c 4 | Author : LIJIN DANIEL JOSEPH 5 | Version : 6 | Copyright : Your copyright notice 7 | Description : Hello World in C, Ansi-style 8 | ============================================================================ 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | int main(void) { 15 | int l,i,sum=0,a[100]; 16 | printf("ENTER THE ARRAY LIMIT:"); 17 | scanf("%d",&l); 18 | printf("ENTER THE VALUES:"); 19 | for(i=0;i 7 | struct date{ 8 | int day,month,year; 9 | }dt; 10 | int main() 11 | { 12 | char m[][12] = {"January","February","March","April","May","June","July","August","September","October","November","December"}; 13 | int i; 14 | printf("Enter in dd-mm-yy\n"); 15 | scanf("%d%d%d",&dt.day, &dt.month, &dt.year); 16 | if (dt.year/10000<1 && dt.month < 13 && dt.day < 32) 17 | { 18 | for(i=0;i<12;i++) 19 | { 20 | if(i+1==dt.month) 21 | { 22 | printf("%d-%s-%d",dt.day,m[i],dt.year); 23 | } 24 | 25 | } 26 | } 27 | else{ 28 | printf("Please reconsider what u have typed"); 29 | } 30 | } 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /prime.c: -------------------------------------------------------------------------------- 1 | //A program to print the prime numbers from a group of numbers 2 | #include 3 | int main() 4 | { 5 | int ar[50],i,size,j,flag; 6 | printf("Enter the size (less than 30) "); 7 | scanf("%d",&size); 8 | for(i=0;i 2 | 3 | void main () 4 | { 5 | int matrix[10][10]; 6 | int i, j, m, n; 7 | int c = 0; 8 | 9 | printf("Enter order of the matix \n"); 10 | scanf("%d %d", &m, &n); 11 | printf("Enter elements of the matix \n"); 12 | for (i = 0; i < m; ++i) 13 | { 14 | for (j = 0; j < n; ++j) 15 | { 16 | scanf("%d", &matrix[i][j]); 17 | if (matrix[i][j] == 0) 18 | { 19 | ++c; 20 | } 21 | } 22 | } 23 | if (c > ((m * n) / 2)) 24 | { 25 | printf("The given matrix is Sparse Matrix \n"); 26 | } 27 | else 28 | printf("The given matrix is not a Sparse Matrix \n"); 29 | printf("There are %d number of Zeros.", c); 30 | } -------------------------------------------------------------------------------- /2x2MatrixDeterminant.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct matrix2X2 4 | { 5 | int a1, b1, a2, b2; 6 | }; 7 | 8 | int calculateDeterminant(struct matrix2X2* matrix2x2) { 9 | return (matrix2x2->a1 * matrix2x2->b2 - matrix2x2->a2 * matrix2x2->b1); 10 | } 11 | 12 | void main() { 13 | // Take user input 14 | printf("| A1 B1 |\n| A2 B2 |\n"); 15 | printf("Enter The Values for the matrix:\n"); 16 | 17 | struct matrix2X2 matrix; 18 | 19 | printf("\nA1:\n>"); 20 | scanf_s(" %d", &matrix.a1); 21 | 22 | printf("\nB1:\n>"); 23 | scanf_s(" %d", &matrix.b1); 24 | 25 | printf("\nA2:\n>"); 26 | scanf_s(" %d", &matrix.a2); 27 | 28 | printf("\nB2:\n>"); 29 | scanf_s(" %d", &matrix.b2); 30 | 31 | // Calculate determinant 32 | printf("\nThe determinant is: %d", calculateDeterminant(&matrix)); 33 | } -------------------------------------------------------------------------------- /Algorithms/matrix/Two-dimensional array to store and print values: -------------------------------------------------------------------------------- 1 | // C program to store temperature of two cities of a week and display it. 2 | #include 3 | const int CITY = 2; 4 | const int WEEK = 7; 5 | int main() 6 | { 7 | int temperature[CITY][WEEK]; 8 | 9 | // Using nested loop to store values in a 2d array 10 | for (int i = 0; i < CITY; ++i) 11 | { 12 | for (int j = 0; j < WEEK; ++j) 13 | { 14 | printf("City %d, Day %d: ", i + 1, j + 1); 15 | scanf("%d", &temperature[i][j]); 16 | } 17 | } 18 | printf("\nDisplaying values: \n\n"); 19 | 20 | // Using nested loop to display vlues of a 2d array 21 | for (int i = 0; i < CITY; ++i) 22 | { 23 | for (int j = 0; j < WEEK; ++j) 24 | { 25 | printf("City %d, Day %d = %d\n", i + 1, j + 1, temperature[i][j]); 26 | } 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /binary search .c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int i, low, high, mid, n, key, array[100]; 5 | printf("Enter number of elementsn"); 6 | scanf("%d",&n); 7 | printf("Enter %d integersn", n); 8 | for(i = 0; i < n; i++) 9 | scanf("%d",&array[i]); 10 | printf("Enter value to findn"); 11 | scanf("%d", &key); 12 | low = 0; 13 | high = n - 1; 14 | mid = (low+high)/2; 15 | while (low <= high) 16 | { 17 | if(array[mid] < key) 18 | low = mid + 1; 19 | else if (array[mid] == key) 20 | { 21 | printf("%d found at location %d.n", key, mid+1); 22 | break; 23 | } 24 | else 25 | high = mid - 1; 26 | mid = (low + high)/2; 27 | } 28 | if(low > high) 29 | printf("Not found! %d isn't present in the list.n", key); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Asnmt 6 - Recursion/1N_NaturalNum.cpp: -------------------------------------------------------------------------------- 1 | // Write a recursive function to print first N nartural numbers. 2 | #include 3 | using namespace std; 4 | void N_natural(int a) // a = 5; || // 5 initial 5 | { // 4 6 | if(a!=0) // false // 3 7 | { // 2 8 | N_natural(a-1); // 10-1 = 9 || // 1 9 | } // 0 10 | else 11 | { 12 | return; 13 | } 14 | cout << a << "\t"; 15 | } 16 | int main() 17 | { 18 | int n; 19 | cout << "Enter a number : "; 20 | cin >> n; // 5 value 21 | cout << "-----------Series--------------\n"; 22 | N_natural(n); // n = 5 23 | } -------------------------------------------------------------------------------- /ducknumber.c: -------------------------------------------------------------------------------- 1 | //Program to check whether a number is duck number or not 2 | # include 3 | # include 4 | 5 | 6 | int main() 7 | { 8 | int dno,dkno,r,flg; 9 | flg=0; 10 | printf("\n\n Check whether a number is a Duck Number or not: \n"); 11 | printf(" ----------------------------------------------------\n"); 12 | printf(" Input a number: "); 13 | scanf("%d",&dkno); 14 | dno=dkno; 15 | while(dkno>0) 16 | { 17 | if(dkno % 10 == 0) 18 | { 19 | flg=1; 20 | break; 21 | } 22 | dkno/=10; 23 | } 24 | if(dno>0 && flg==1) 25 | { 26 | printf(" The given number is a Duck Number.\n"); 27 | } 28 | else 29 | { 30 | printf(" The given number is not a Duck Number.\n"); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /data-structure/seraching/binarysearch.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int i, low, high, mid, n, key, array[100]; 6 | 7 | printf("Enter number of elements:\n"); 8 | scanf("%d",&n); 9 | 10 | printf("Enter %d integers:\n", n); 11 | for(i = 0; i < n; i++) 12 | scanf("%d",&array[i]); 13 | printf("Enter value to find:\n"); 14 | scanf("%d", &key); 15 | 16 | low = 0; 17 | high = n - 1; 18 | mid = (low+high)/2; 19 | 20 | while (low <= high) 21 | { 22 | if(array[mid] < key) 23 | low = mid + 1; 24 | else if (array[mid] == key) 25 | { 26 | printf("%d found at location %d.\n", key, mid+1); 27 | break; 28 | } 29 | else 30 | high = mid - 1; 31 | mid = (low + high)/2; 32 | } 33 | 34 | if(low > high) 35 | printf("Not found! %d isn't present in the list.\n", key); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Algorithms/LCM_of_n_term.c: -------------------------------------------------------------------------------- 1 | // This C programme is used to find LCM of any number of terms 2 | 3 | #include 4 | 5 | int lcm(int arr[], int n) 6 | { 7 | int i = 0, fact = 1; 8 | while (fact) 9 | { 10 | int count = 0; 11 | i++; 12 | for (int j = 0; j < n; j++) 13 | { 14 | if (i % arr[j] == 0) 15 | { 16 | count++; 17 | } 18 | } 19 | if (count == n) 20 | fact = 0; 21 | } 22 | return i; 23 | } 24 | 25 | int main() 26 | { 27 | int n; 28 | printf("Enter the number of terms : "); 29 | scanf("%d", &n); 30 | int arr[n]; 31 | printf("Enter the numbers\n"); 32 | for (int i = 0; i < n; i++) 33 | { 34 | scanf("%d", &arr[i]); 35 | } 36 | int LCM = lcm(arr, n); 37 | printf("The LCM is %d\n", LCM); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Algorithms/printing_strong_numbers.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | int main() 4 | { 5 | int i,b,j,r,temp; 6 | long long fact,sum; 7 | printf("enter limit number: "); 8 | scanf("%d",&b); 9 | /* printf("enter the last number upto which you want print the series: "); 10 | scanf("%d",&b); 11 | /* if(a>b) 12 | { 13 | b=a; 14 | } */ 15 | for(i=1;i<=b;i++) 16 | { 17 | temp=i; 18 | sum=0; 19 | // temp=a; 20 | while(temp>0) 21 | { 22 | fact=1ll; 23 | r=temp%10; 24 | for(j=r;j>=1;j--) 25 | { 26 | fact=fact*j; 27 | } 28 | sum=sum+fact; 29 | temp=temp/10; 30 | } 31 | // a=temp; 32 | if(sum==i) 33 | { 34 | printf("%d, ",i); 35 | } 36 | } 37 | return 0; 38 | 39 | } 40 | 41 | -------------------------------------------------------------------------------- /HCF_n_Numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | int arr[10], n, mp, i, count; 6 | printf("Enter the Size: "); 7 | scanf("%d", &n); 8 | printf("Enter %d Numbers: ", n); 9 | for(i=0; i 2 | void main() 3 | { 4 | 5 | int i, j, a, n, number[30]; 6 | printf("Enter the value of N \n"); 7 | scanf("%d", &n); 8 | 9 | printf("Enter the numbers \n"); 10 | for (i = 0; i < n; ++i) 11 | scanf("%d", &number[i]); 12 | 13 | for (i = 0; i < n; ++i) 14 | { 15 | 16 | for (j = i + 1; j < n; ++j) 17 | { 18 | 19 | if (number[i] > number[j]) 20 | { 21 | 22 | a = number[i]; 23 | number[i] = number[j]; 24 | number[j] = a; 25 | 26 | } 27 | 28 | } 29 | 30 | } 31 | 32 | printf("The numbers arranged in ascending order are given below \n"); 33 | for (i = 0; i < n; ++i) 34 | printf("%d\n", number[i]); 35 | 36 | } 37 | -------------------------------------------------------------------------------- /Algorithms/Calc_Telephone_Bill.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | int noOfCall, tempNoOfCall, charge; 6 | printf("Enter total unit of calls made this month: "); 7 | scanf("%d", &noOfCall); 8 | printf("\n"); 9 | if(noOfCall<=200) 10 | { 11 | printf("You have not crossed the limit of 200 calls"); 12 | printf("\nNo charge."); 13 | } 14 | else 15 | { 16 | if(noOfCall<=500) 17 | { 18 | tempNoOfCall = noOfCall - 200; 19 | charge = tempNoOfCall * 1; 20 | printf("The charge you have to paid = %d", charge); 21 | } 22 | else 23 | { 24 | tempNoOfCall = noOfCall - 500; 25 | charge = tempNoOfCall * 2; 26 | charge = charge + 300; 27 | printf("The charge you have to paid = %d", charge); 28 | } 29 | } 30 | getch(); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Algorithms/matrix/determinant_of_a_2x2_matrix.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a[2][2], i, j; 6 | long determinant; 7 | 8 | printf("\n\nEnter the 4 elements of the Matrix\n"); 9 | for(i = 0; i < 2; i++) 10 | { 11 | for(j = 0; j < 2; j++) 12 | { 13 | scanf("%d", &a[i][j]); 14 | } 15 | } 16 | 17 | printf("\n\nThe entered matrix is: \n\n"); 18 | for(i = 0; i < 2; i++) 19 | { 20 | for(j = 0; j < 2; j++) 21 | { 22 | printf("%d\t", a[i][j]); // to print the complete row 23 | } 24 | printf("\n"); // to move to the next row 25 | } 26 | 27 | // finding the determinant of a 2x2 matrix 28 | determinant = a[0][0]*a[1][1] - a[1][0]*a[0][1]; 29 | printf("\n\nDeterminant of the 2x2 matrix is : %d - %d = %d", a[0][0]*a[1][1], a[1][0]*a[0][1], determinant); 30 | printf("\n\n"); 31 | 32 | return 0; 33 | 34 | } 35 | -------------------------------------------------------------------------------- /Algorithms/N Queens Problem.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int a[15]={0},N; 5 | 6 | void nextvalue(k) 7 | { 8 | int j; 9 | 10 | stept: 11 | a[k]=(a[k]+1)%(N+1); 12 | 13 | if(a[k]==0) 14 | return ; 15 | 16 | for(j=0;j 2 | #include 3 | #include 4 | 5 | // declaring recursive function 6 | void string_reverse(char str[]) 7 | { 8 | char str2[100]; 9 | int a; 10 | a = strlen(str); 11 | printf("The length of the string is %d\n", a); 12 | strcpy(str2, str); 13 | strrev(str2); 14 | printf("The reverse of the word of the string is :"); 15 | for (int i = 0; i < a; i++) 16 | { 17 | if (str2[i + 1] == ' ' || str2[i + 1] == '\0') 18 | { 19 | for (int j = i; j >= 0 && str2[j] != ' '; j--) 20 | { 21 | printf("%c", str2[j]); 22 | } 23 | printf(" "); 24 | } 25 | } 26 | printf("\n"); 27 | } 28 | 29 | void main() 30 | { 31 | char str[100]; 32 | printf("Enter the string \n"); 33 | gets(str); 34 | string_reverse(str); 35 | strrev(str); 36 | printf("The reverse of the string is: %s\n", str); 37 | } 38 | -------------------------------------------------------------------------------- /geometricprogression.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | int a,b,c,d,i,an; 6 | printf("enter first number of GP "); 7 | scanf("%d",&a); 8 | printf("enter number of terms of GP "); 9 | scanf("%d",&b); 10 | printf("enter the common multiple "); 11 | scanf("%d",&c); 12 | an=(a)*pow(c,b-1); 13 | if(c<1) 14 | { 15 | 16 | d=a*(1-pow(c,b+1)-1)/(c-1); 17 | printf("\n the sum of the sequence is %d ",d); 18 | for(i=a;i<=an;i=i*c) 19 | { 20 | 21 | printf(".%d",i); 22 | 23 | } 24 | 25 | 26 | } 27 | 28 | else if (c>1) 29 | { 30 | d=a*(1-pow(c,b+1)/(1-c)); 31 | printf("\n the sum of the sequence is %d ",d); 32 | for(i=a;i<=an;i=i*c) 33 | { 34 | 35 | printf(".%d",i); 36 | 37 | } 38 | } 39 | 40 | else 41 | 42 | { 43 | printf("request is invalid"); 44 | } 45 | 46 | printf("\n last terms is :%d",an); 47 | 48 | return 0; 49 | 50 | } 51 | -------------------------------------------------------------------------------- /evalpostfix.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int stack[20]; 4 | int top=-1; 5 | 6 | void push(int x) 7 | { 8 | stack[++top]= x; 9 | } 10 | 11 | int pop() 12 | { 13 | return stack[top--]; 14 | } 15 | 16 | int main() 17 | { 18 | char exp[20]; 19 | int i; 20 | char *e; 21 | int n1,n2,n3,num; 22 | printf("Enter the expression:"); 23 | scanf("%s",exp); 24 | e=exp; 25 | while(*e !='\0') 26 | { 27 | if(isdigit(*e)) 28 | { 29 | num=*e-48; 30 | push(num); 31 | } 32 | else 33 | { 34 | n1=pop(); 35 | n2=pop(); 36 | switch(*e) 37 | { 38 | case '+': n3=n1+n2; 39 | break; 40 | case '-': n3=n2-n1; 41 | break; 42 | case '*': n3=n2*n1; 43 | break; 44 | case '/': n3=n2/n1; 45 | break; 46 | case '^': n3=pow(n2,n1); 47 | 48 | #elif 49 | 50 | #endif 51 | break; 52 | } 53 | push(n3); 54 | } 55 | e++; 56 | } 57 | printf("\n the result of expression %s=%d\n",exp,pop()); 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /sorting/Count_Sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | void countSort(int arr[], int n); 3 | int main() 4 | { 5 | int n, i; 6 | printf("Enter size of array: "); 7 | scanf("%d", &n); 8 | int arr[n]; 9 | printf("Enter array: "); 10 | for (i = 0; i < n; i++) 11 | { 12 | scanf("%d", &arr[i]); 13 | } 14 | countSort(arr, n); 15 | printf("The Sorted array is : "); 16 | for (i = 0; i < n; i++) 17 | { 18 | printf("%d ", arr[i]); 19 | } 20 | return 0; 21 | } 22 | 23 | void countSort(int arr[], int n) 24 | { 25 | int temp[n], max = arr[0], i; 26 | 27 | for (i = 1; i < n; i++) 28 | { 29 | if (max < arr[i]) 30 | { 31 | max = arr[i]; 32 | } 33 | } 34 | int count[max + 1]; 35 | for (i = 0; i <= max; i++) 36 | { 37 | count[i] = 0; 38 | } 39 | for (i = 0; i < n; i++) 40 | { 41 | count[arr[i]]++; 42 | } 43 | for (i = 1; i <= max; i++) 44 | { 45 | count[i] += count[i - 1]; 46 | } 47 | for (i = n - 1; i >= 0; i--) 48 | { 49 | temp[--count[arr[i]]] = arr[i]; 50 | } 51 | for (i = 0; i < n; i++) 52 | { 53 | arr[i] = temp[i]; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /sorting/insertion-sort.c: -------------------------------------------------------------------------------- 1 | //sorting an array using insertion sort....... 2 | #include 3 | #include 4 | void main() 5 | { 6 | int i,j,*p,n,temp=0,k; 7 | printf("enter the size of the array:\n"); 8 | scanf("%d",&n); 9 | printf("enter the elements of the array:\n"); 10 | p=(int*)calloc(n,4); 11 | for(i=0;i0&&*(p+j-1);j--) 22 | { 23 | if(*(p+j)<*(p+j-1)) 24 | { 25 | temp=*(p+j); 26 | *(p+j)=*(p+j-1); 27 | *(p+j-1)=temp; 28 | } 29 | } 30 | printf("modified array:"); 31 | for(k=0;k 2 | 3 | void swap(int *xp, int *yp) 4 | { 5 | int temp = *xp; 6 | *xp = *yp; 7 | *yp = temp; 8 | } 9 | 10 | void bubbleSort(int arr[], int n) 11 | { 12 | int i, j; 13 | for (i = 0; i < n-1; i++) 14 | 15 | for (j = 0; j < n-i-1; j++) 16 | if (arr[j] > arr[j+1]) 17 | swap(&arr[j], &arr[j+1]); 18 | } 19 | 20 | void printArray(int arr[], int size) 21 | { 22 | int i; 23 | for (i=0; i < size; i++) 24 | printf("%d ", arr[i]); 25 | printf("\n"); 26 | } 27 | 28 | int main() 29 | { 30 | // int arr[] = {64, 34, 25, 12, 22, 11, 90}; 31 | // int n = sizeof(arr)/sizeof(arr[0]); 32 | int n; 33 | printf("Enter the size of the array\n"); 34 | scanf("%d",&n); 35 | int arr[n]; 36 | printf("Enter the elements for the array\n"); 37 | for(int i=0;i 2 | int main() 3 | { 4 | int a[100],b[100],c[100],n1,n2,n,k=0,i,j; 5 | 6 | // taking input of set A 7 | 8 | printf("Enter number of element of set A\n"); 9 | scanf("%d",&n1); 10 | printf("Enter elements of set A\n"); 11 | for(i=0;i 3 | #include 4 | void main() 5 | { 6 | int n,*p,i,j,min,temp,k; 7 | printf("enter the size of an array:\n"); 8 | scanf("%d",&n); 9 | 10 | p=(int*)calloc(n,4); 11 | 12 | if(p==NULL) 13 | { 14 | printf("error!!"); 15 | exit(1); 16 | } 17 | 18 | printf("enter the elements of the array:\n"); 19 | 20 | for(i=0;i 3 | void searchIns(int [], int *, int ); 4 | void main() 5 | { 6 | int a[100],i,num,n=10; 7 | for(i=0;i=i;j--) 39 | arr[j+1]=arr[j]; 40 | 41 | arr[i]=num; 42 | *n=*n+1; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /lar.c: -------------------------------------------------------------------------------- 1 | //A program to print the largest and second largest element from a group of numbers 2 | #include 3 | int main() 4 | { 5 | int ar[50],i,size,max=0,smax=0,index; //smax is second largest max is largest 6 | printf("Enter the size (less than 30) "); 7 | scanf("%d",&size); 8 | for(i=0;imax) 18 | { 19 | 20 | max = ar[i]; 21 | index=i; //index is the index of the largest array element 22 | 23 | } 24 | } 25 | for ( i = index; i < size-1 ; i++ ) //This is loop is for deleting the max element from the array 26 | ar[i] = ar[i+1]; 27 | 28 | for(i=0;ismax) 31 | { 32 | smax=ar[i]; 33 | 34 | } 35 | } 36 | printf("Largest element is %d \nsecond largest element is %d\n",max,smax); 37 | 38 | 39 | 40 | } -------------------------------------------------------------------------------- /Algorithms/HCF_of_n_terms.c: -------------------------------------------------------------------------------- 1 | // This C programme is used to calculate HCF of any number of terms 2 | 3 | #include 4 | 5 | int hcf(int arr[], int n) 6 | { 7 | int min = arr[0], hcf; 8 | for (int i = 1; i < n; i++) 9 | { 10 | if (arr[i] < min) 11 | min = arr[i]; // Here, we have find the minimum element of the array 12 | } 13 | for (int i = 1; i <= min; i++) 14 | { 15 | int count = 0; 16 | for (int j = 0; j < n; j++) 17 | { 18 | if (arr[j] % i == 0) 19 | { 20 | count++; // Counting number of elements divisible by i 21 | } 22 | } 23 | if (count == n) 24 | { 25 | hcf = i; 26 | } 27 | } 28 | return hcf; 29 | } 30 | 31 | int main() 32 | { 33 | int n; 34 | printf("Enter the number of terms : "); 35 | scanf("%d", &n); 36 | int arr[n]; 37 | printf("Enter the numbers\n"); 38 | for (int i = 0; i < n; i++) 39 | { 40 | scanf("%d", &arr[i]); 41 | } 42 | int HCF = hcf(arr, n); 43 | printf("The HCF is %d\n", HCF); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Algorithms/Sieve_of_Atkin.c: -------------------------------------------------------------------------------- 1 | // Using Sieve of Atkin method to find all prime numbers up to 100000 super fast (you can modify it to increase the limit)! 2 | 3 | /* 4 | Output Format: | 2 | 3 | 5 | 7 | 11 | 13 | 17 | 19 | 23 | 29 | ... | ... | ... | 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | int main(){ 11 | int a=2,b=100000; 12 | bool s[100000] = {0}; 13 | int i,j; 14 | 15 | // Manually specifying 2 and 3! 16 | printf("| 2 | 3 "); 17 | for(i=0; ij && n<=b && n%12 ==11) s[n]^=1; 28 | } 29 | } 30 | 31 | for(int m=5; m*m 2 | void quicksort(int number[25],int first,int last){ 3 | int i, j, pivot, temp; 4 | 5 | if(firstnumber[pivot]) 14 | j--; 15 | if(i 2 | void quicksort(int number[25],int first,int last){ 3 | int i, j, pivot, temp; 4 | 5 | if(firstnumber[pivot]) 14 | j--; 15 | if(i 2 | #include 3 | 4 | void f_1(int* p); 5 | void f_2(int* p); 6 | 7 | int main(void) 8 | { 9 | int* num_p = 0; 10 | 11 | num_p = malloc(sizeof(int)); 12 | 13 | if(num_p == 0) 14 | { 15 | puts("Memory Allocation Failed!!"); 16 | exit(-1); 17 | } 18 | 19 | *num_p = 100; 20 | 21 | printf("Memory location in num_p before call to function: %llu\n", (unsigned long long)num_p); 22 | printf("Value in memory location before calling to function : %d\n\n", *num_p); 23 | 24 | f_1(num_p); 25 | 26 | printf("Memory location in num_p after call to function_1 : %llu\n", (unsigned long long)num_p); 27 | printf("Value in memory location after calling to function_1 : %d\n\n", *num_p); 28 | 29 | f_2(num_p); 30 | 31 | printf("Memory location in num_p after call to function_2 : %llu\n", (unsigned long long)num_p); 32 | printf("Value in memory location after calling to function_2 : %d\n\n", *num_p); 33 | 34 | exit(0); 35 | } 36 | 37 | void f_1(int* p) 38 | { 39 | *p = 123; 40 | } 41 | 42 | void f_2(int* p) 43 | { 44 | *p = 432; 45 | } -------------------------------------------------------------------------------- /sorting/radix_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int getMax(int arr[], int n) 6 | { 7 | int mx = arr[0]; 8 | for (int i = 1; i < n; i++) 9 | if (arr[i] > mx) 10 | mx = arr[i]; 11 | return mx; 12 | } 13 | 14 | void countSort(int arr[], int n, int exp) 15 | { 16 | int output[n]; // output array 17 | int i, count[10] = { 0 }; 18 | 19 | 20 | for (i = 0; i < n; i++) 21 | count[(arr[i] / exp) % 10]++; 22 | 23 | 24 | for (i = 1; i < 10; i++) 25 | count[i] += count[i - 1]; 26 | 27 | for (i = n - 1; i >= 0; i--) { 28 | output[count[(arr[i] / exp) % 10] - 1] = arr[i]; 29 | count[(arr[i] / exp) % 10]--; 30 | } 31 | 32 | 33 | for (i = 0; i < n; i++) 34 | arr[i] = output[i]; 35 | } 36 | 37 | 38 | void radixsort(int arr[], int n) 39 | { 40 | 41 | int m = getMax(arr, n); 42 | 43 | 44 | for (int exp = 1; m / exp > 0; exp *= 10) 45 | countSort(arr, n, exp); 46 | } 47 | 48 | 49 | void print(int arr[], int n) 50 | { 51 | for (int i = 0; i < n; i++) 52 | cout << arr[i] << " "; 53 | } 54 | 55 | 56 | int main() 57 | { 58 | int arr[] = { 120,5,0,22,59,45,2,64,78,122,1 }; 59 | int n = sizeof(arr) / sizeof(arr[0]); 60 | 61 | 62 | radixsort(arr, n); 63 | print(arr, n); 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /data-structure/stack/Implementation of Stacks in C.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | int n; 6 | printf("Enter Stack Size: "); 7 | scanf("%d",&n); 8 | int stack[n],ch=1,top=-1; 9 | while(ch>=1&&ch<4){ 10 | printf("\n 1:Push \n 2:Pop \n 3:Peek \n 4:Exit"); 11 | printf("\nEnter choice : "); 12 | scanf("%d",&ch); 13 | switch(ch){ 14 | case 1:{ 15 | int a; 16 | if(top==n-1){ 17 | printf("\nStack Overflow"); 18 | break; 19 | } 20 | top++; 21 | printf("Enter value : "); 22 | scanf("%d",&a); 23 | stack[top]=a; 24 | break;} 25 | case 2:{ 26 | int b; 27 | if(top==-1){ 28 | printf("\nStack Underflow "); 29 | break; 30 | } 31 | b = stack[top]; 32 | printf("Element Popped : %d ",b); 33 | top--; 34 | break;} 35 | case 3:{ 36 | if(top==-1){ 37 | printf("\nStack UnderFlow "); 38 | break; 39 | } 40 | for(int i=top;i>=0;i--) 41 | { 42 | printf("%d ",stack[i]); 43 | } 44 | break;} 45 | case 4: 46 | printf("\n Tata\n"); 47 | 48 | break; 49 | default: 50 | printf("\nInvalid"); 51 | break; 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Algorithms/file-manipulation/file.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | char s[100]; 7 | int i, choose; 8 | printf("\nEnter a string : "); 9 | gets(s); 10 | printf("\nchoose any one option: \n"); 11 | printf("1) convert to lowercase\n"); 12 | printf("2) convert to uppercase\n"); 13 | printf("3) convert uppercase letters to lowercase and vice versa\n"); 14 | scanf("%d", &choose); 15 | switch (choose) 16 | { 17 | case 1: 18 | strlwr(s); 19 | printf("\nString in Lower Case = %s", s); 20 | break; 21 | case 2: 22 | strupr(s); 23 | printf("\nString in Upper Case = %s", s); 24 | break; 25 | case 3: 26 | for (i = 0; s[i] != '\0'; i++) 27 | { 28 | if (s[i] >= 'A' && s[i] <= 'Z') 29 | { 30 | s[i] = s[i] + 32; 31 | } 32 | else if (s[i] >= 'a' && s[i] <= 'z') 33 | { 34 | s[i] = s[i] - 32; 35 | } 36 | } 37 | printf("\nstring in their opposite case = %s", s); 38 | break; 39 | case 4: 40 | printf("you have chosen wrong option."); 41 | break; 42 | } 43 | return 0; 44 | } -------------------------------------------------------------------------------- /structure/1.c: -------------------------------------------------------------------------------- 1 | #include 2 | struct date{ 3 | int day,month,year; 4 | }dt; 5 | int main() { 6 | printf("Enter in dd-mm-yy\n"); 7 | scanf("%d%d%d", &dt.day, &dt.month, &dt.year); 8 | if (dt.year / 10000 < 1 && dt.month < 13 && dt.day < 32) 9 | { 10 | if (dt.month % 2 == 0 && dt.day == 30 && dt.month != 12) { 11 | dt.month++; 12 | } else if (dt.month == 12 && dt.day == 30 && dt.month % 2 == 0) { 13 | dt.month++; 14 | dt.year++; 15 | } else if (dt.month % 2 != 0 && dt.day == 31 && dt.month != 12) { 16 | dt.month++; 17 | } else if (dt.month % 2 != 0 && dt.day == 31 && dt.month == 12) { 18 | dt.month++; 19 | dt.year++; 20 | } else if (dt.month == 12) { 21 | dt.year++; 22 | } else if (dt.month == 2 && (dt.year / 100 == 0 || dt.year / 400 == 0 || dt.year / 4 == 0) && dt.day == 29) { 23 | dt.month++; 24 | } else if (dt.month == 2 && dt.day == 28) { 25 | dt.month++; 26 | } else { 27 | dt.day++; 28 | } 29 | printf("Tomorrow's date is : %d-%d-%d ", dt.day, dt.month, dt.year); 30 | } 31 | else{ 32 | printf("Please reconsider what u have typed"); 33 | } 34 | return 0; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /data-structure/graph/Tree.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | struct node 8 | { 9 | int data; 10 | struct node *left; 11 | struct node *right; 12 | }; 13 | 14 | 15 | struct node* newNode(int data) 16 | { 17 | 18 | struct node* node = (struct node*)malloc(sizeof(struct node)); 19 | 20 | 21 | node->data = data; 22 | 23 | 24 | node->left = NULL; 25 | node->right = NULL; 26 | return(node); 27 | } 28 | 29 | insert(int key, struct node **leaf) 30 | { 31 | if (*leaf == 0) 32 | { 33 | *leaf = (struct node*) malloc(sizeof(struct node)); 34 | (*leaf)->data = key; 35 | /* initialize the children to null */ 36 | (*leaf)->left = 0; 37 | (*leaf)->right = 0; 38 | } 39 | else if (key <= (*leaf)->data) 40 | { 41 | insert(key, &(*leaf)->left); 42 | } 43 | else if (key >(*leaf)->data) 44 | { 45 | insert(key, &(*leaf)->right); 46 | } 47 | } 48 | 49 | int main() 50 | { 51 | srand(time(NULL)); 52 | /*create root*/ 53 | struct node *root = NULL; 54 | 55 | int tree_size = 100; 56 | int new_leaf_value; 57 | 58 | root = newNode(50); 59 | for (int i = 1; i <= tree_size; i++) 60 | { 61 | new_leaf_value = rand() % 100; 62 | insert(new_leaf_value, &root); 63 | } 64 | getchar(); 65 | return 0; 66 | } -------------------------------------------------------------------------------- /fun_types.c: -------------------------------------------------------------------------------- 1 | //Programs of types of functions 2 | 3 | /**********NO ARG & NO RETURN VALUE***********/ 4 | #include 5 | fun1(); 6 | int main() 7 | { 8 | fun1(); 9 | return 0; 10 | } 11 | fun1() 12 | { 13 | int a=3,b=5,sum; 14 | 15 | sum=a+b; 16 | printf("Addition is %d",sum); 17 | return 0; 18 | } 19 | 20 | /**********WITH ARG & NO RETURN VALUE***********/ 21 | 22 | #include 23 | fun1(int); 24 | int main() 25 | { 26 | int a; 27 | fun1(a); 28 | return 0; 29 | } 30 | fun1(a) 31 | { 32 | int x=2,y=6,dif; 33 | 34 | dif=y-x; 35 | printf("Diff is %d",dif); 36 | return 0; 37 | } 38 | 39 | /**********WITH NO ARG & RETURN VALUE***********/ 40 | 41 | #include 42 | int fun1(); 43 | int main() 44 | { 45 | int a; 46 | printf("Mul is %d",fun1()); 47 | return 0; 48 | } 49 | int fun1() 50 | { 51 | int w=10,m=3,mul; 52 | 53 | mul=w*m; 54 | return mul; 55 | } 56 | 57 | /**********WITH ARG & RETURN VALUE***********/ 58 | 59 | #include 60 | int fun1(int,int); 61 | int main() 62 | { 63 | int a,b; 64 | printf("Div is %d",fun1( a, b)); 65 | return 0; 66 | } 67 | int fun1(int a,int b) 68 | { 69 | int q=300,r=30,div; 70 | 71 | div=q/r; 72 | return div; 73 | } 74 | -------------------------------------------------------------------------------- /Algorithms/matrix/multiplication_of_matrix.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int m, n, p, q, c, d, k, sum = 0; 6 | int first[10][10], second[10][10], multiply[10][10]; 7 | 8 | printf("Enter number of rows and columns of first matrix\n"); 9 | scanf("%d%d", &m, &n); 10 | printf("Enter elements of first matrix\n"); 11 | 12 | for (c = 0; c < m; c++) 13 | for (d = 0; d < n; d++) 14 | scanf("%d", &first[c][d]); 15 | 16 | printf("Enter number of rows and columns of second matrix\n"); 17 | scanf("%d%d", &p, &q); 18 | 19 | if (n != p) 20 | printf("The multiplication isn't possible.\n"); 21 | else 22 | { 23 | printf("Enter elements of second matrix\n"); 24 | 25 | for (c = 0; c < p; c++) 26 | for (d = 0; d < q; d++) 27 | scanf("%d", &second[c][d]); 28 | 29 | for (c = 0; c < m; c++) { 30 | for (d = 0; d < q; d++) { 31 | for (k = 0; k < p; k++) { 32 | sum = sum + first[c][k]*second[k][d]; 33 | } 34 | 35 | multiply[c][d] = sum; 36 | sum = 0; 37 | } 38 | } 39 | 40 | printf("Product of the matrices:\n"); 41 | 42 | for (c = 0; c < m; c++) { 43 | for (d = 0; d < q; d++) 44 | printf("%d\t", multiply[c][d]); 45 | 46 | printf("\n"); 47 | } 48 | } 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Algorithms/matrix/Transpose of a matrix.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int i,j,a[3][3],b[3][3], isSymmetric; 5 | printf("Write the 9 element of matrix\n"); 6 | for(i=0;i<3;i++) 7 | { 8 | for(j=0;j<3;j++) 9 | { 10 | scanf("%d",&a[i][j]); 11 | } 12 | } 13 | 14 | /* 15 | * Find transpose of matrix A 16 | */ 17 | for(i=0;i<3;i++) 18 | { 19 | for(j=0;j<3;j++) 20 | { 21 | b[i][j]=a[j][i]; 22 | } 23 | } 24 | 25 | 26 | /* Check whether matrix A is equal to its transpose or not 27 | */ 28 | isSymmetric = 1; 29 | for(i=0; i<3 && isSymmetric; i++) 30 | { 31 | for(j=0; j<3; j++) 32 | { 33 | /* If matrix A is not equal to its transpose */ 34 | if(a[i][j] != b[i][j]) 35 | { 36 | isSymmetric = 0; 37 | break; 38 | } 39 | } 40 | } 41 | 42 | if(isSymmetric == 1) 43 | { 44 | printf("\nThe given matrix is Symmetric matrix: \n"); 45 | 46 | for(i=0; i<3; i++) 47 | { 48 | for(j=0; j<3; j++) 49 | { 50 | printf("%d ", a[i][j]); 51 | } 52 | 53 | printf("\n"); 54 | } 55 | } 56 | else 57 | { 58 | printf("\nThe given matrix is not Symmetric matrix."); 59 | } 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /data-structure/list/circular_singly_linked_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node{ 5 | int data; 6 | struct node* link; 7 | }; 8 | 9 | //adding element in empty list 10 | struct node* addToEmpty(int data) 11 | { 12 | struct node* temp=malloc(sizeof(struct node)); 13 | temp->data=data; 14 | temp->link=temp; 15 | return temp; 16 | } 17 | //adding element at beginning 18 | struct node* addToBeg(struct node*tail,int data) 19 | { 20 | struct node *newP=malloc(sizeof(struct node)); 21 | newP->data=data; 22 | newP->link=tail->link; 23 | tail->link=newP; 24 | return tail; 25 | } 26 | 27 | //adding element at end 28 | 29 | struct node* addAtEnd(struct node* tail,int data) 30 | { 31 | struct node* newP=malloc(sizeof(struct node)); 32 | newP->data=data; 33 | newP->link=NULL; 34 | newP->link=tail->link; 35 | tail->link=newP; 36 | tail=tail->link; 37 | return tail; 38 | } 39 | 40 | void print(struct node* tail) 41 | { 42 | struct node* p=tail->link; 43 | do{ 44 | printf("%d\n",p->data); 45 | p=p->link; 46 | }while(p!=tail->link); 47 | 48 | } 49 | 50 | int main() 51 | {struct node*tail; 52 | tail=addToEmpty(45); 53 | tail=addToBeg(tail,34); 54 | tail=addAtEnd(tail,50); 55 | print(tail); 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Algorithms/queuequestion.c.txt: -------------------------------------------------------------------------------- 1 | //given a queue and a int k , print all the numbers greater than k 2 | 3 | #include 4 | int queue[100]; 5 | int f=-1; 6 | int r=-1; 7 | 8 | void enqueue(int n,int item){ 9 | // printf("%d ",item); 10 | if(r>=n-1){ 11 | return; 12 | } 13 | if(f==-1 && r==-1){ 14 | f=f+1; 15 | r=r+1; 16 | queue[r]=item; 17 | 18 | // printf("%d inintial \n",item); 19 | } 20 | else{ 21 | if(rr)){ 34 | return -1; 35 | } 36 | return queue[f++]; 37 | 38 | } 39 | int main(){ 40 | 41 | 42 | int n; 43 | int x; 44 | scanf("%d",&n); 45 | // printf("%d",f); 46 | for (int i=0;ip){ 62 | c=1; 63 | printf("%d ",x); 64 | } 65 | } 66 | 67 | if(c==0){ 68 | printf("no"); 69 | } 70 | 71 | // by Mohammed Aadils -------------------------------------------------------------------------------- /3Left-Right-Rotation.cpp: -------------------------------------------------------------------------------- 1 | // 3 // Left Rotation and Right Rotation of a string. 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | string Lrot, Rrot; 7 | int Rotate; 8 | char temp; 9 | cout << "\nEnter a string : "; 10 | getline(cin, Lrot); 11 | Rrot = Lrot; 12 | cout << "\nHow many times u want to rotate : "; 13 | cin >> Rotate; 14 | 15 | int n = Rrot.size()-1; 16 | for(int i=0; i=0; k--) 34 | { 35 | if(k==Rrot.size()-1) 36 | { 37 | temp = Rrot[k]; 38 | } 39 | else 40 | { 41 | Rrot[k+1] = Rrot[k]; 42 | if(k == 0) 43 | { 44 | Rrot[k] = temp; 45 | } 46 | } 47 | } 48 | } 49 | cout << "\nLeft Rotation : " << Lrot; 50 | cout << "\n\nRight Rotation : " << Rrot << "\n\n"; 51 | } -------------------------------------------------------------------------------- /heapsort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void create(int []); 4 | void down_adjust(int [],int); 5 | 6 | int main() 7 | { 8 | int heap[30],n,i,last,temp; 9 | 10 | printf("Enter no. of elements:"); 11 | scanf("%d",&n); 12 | 13 | printf("\nEnter elements:"); 14 | for(i=1;i<=n;i++) 15 | scanf("%d",&heap[i]); 16 | 17 | 18 | heap[0]=n; 19 | create(heap); 20 | 21 | 22 | while(heap[0] > 1) 23 | { 24 | 25 | last=heap[0]; 26 | temp=heap[1]; 27 | heap[1]=heap[last]; 28 | heap[last]=temp; 29 | heap[0]--; 30 | down_adjust(heap,1); 31 | } 32 | 33 | 34 | printf("\nArray after sorting:\n"); 35 | for(i=1;i<=n;i++) 36 | printf("%d ",heap[i]); 37 | 38 | return 0; 39 | } 40 | 41 | void create(int heap[]) 42 | { 43 | int i,n; 44 | n=heap[0]; 45 | 46 | for(i=n/2;i>=1;i--) 47 | down_adjust(heap,i); 48 | } 49 | 50 | void down_adjust(int heap[],int i) 51 | { 52 | int j,temp,n,flag=1; 53 | n=heap[0]; 54 | 55 | while(2*i<=n && flag==1) 56 | { 57 | j=2*i; 58 | if(j+1<=n && heap[j+1] > heap[j]) 59 | j=j+1; 60 | if(heap[i] > heap[j]) 61 | flag=0; 62 | else 63 | { 64 | temp=heap[i]; 65 | heap[i]=heap[j]; 66 | heap[j]=temp; 67 | i=j; 68 | } 69 | } 70 | } 71 | 72 | 73 | -------------------------------------------------------------------------------- /Algorithms/string/Convert_case_of_letters.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | char s[100]; 7 | int i,choose; 8 | printf("\nEnter a string : "); 9 | gets(s); 10 | printf("\nchoose any one option: \n"); 11 | printf("1) convert to lowercase\n"); 12 | printf("2) convert to uppercase\n"); 13 | printf("3) convert uppercase letters to lowercase and vice versa\n"); 14 | scanf("%d",&choose); 15 | switch(choose) 16 | { 17 | case 1: 18 | for (i = 0; s[i]!='\0'; i++) 19 | { 20 | if(s[i] >= 'A' && s[i] <= 'Z') 21 | { 22 | s[i] = s[i] + 32; 23 | } 24 | } 25 | printf("\nString in Lower Case = %s", s); 26 | break; 27 | case 2: 28 | for (i = 0; s[i]!='\0'; i++) 29 | { 30 | if(s[i] >= 'a' && s[i] <= 'z') 31 | { 32 | s[i] = s[i] - 32; 33 | } 34 | } 35 | printf("\nString in Upper Case = %s", s); 36 | break; 37 | case 3: 38 | for (i = 0; s[i]!='\0'; i++) 39 | { 40 | if(s[i] >= 'A' && s[i] <= 'Z') 41 | { 42 | s[i] = s[i] + 32; 43 | } 44 | else if(s[i] >= 'a' && s[i] <= 'z') 45 | { 46 | s[i] = s[i] - 32; 47 | } 48 | } 49 | printf("\nstring in their opposite case = %s", s); 50 | break; 51 | case 4: 52 | printf("you have chosen wrong option."); 53 | break; 54 | } 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /Algorithms/set/sum_of_subset_algo_implementation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define TRUE 1 3 | #define FALSE 0 4 | int inc[50],w[50],m,n; 5 | int recsos(int i,int wt,int total) { 6 | return(((wt+total)>=m)&&((wt==m)||(wt+w[i+1]<=m))); 7 | } 8 | 9 | void main() { 10 | int i,j,n,temp,total=0; 11 | 12 | printf("\n Enter how many numbers:\n"); 13 | scanf("%d",&n); 14 | printf("\n Enter %d numbers to the set:\n",n); 15 | for (i=0;iw[j+1]) { 24 | temp=w[j]; 25 | w[j]=w[j+1]; 26 | w[j+1]=temp; 27 | } 28 | 29 | if((total 2 | 3 | int min(int,int); 4 | void floyds(int p[10][10],int n) 5 | { 6 | int i,j,k; 7 | for(k=1;k<=n;k++) 8 | for(i=1;i<=n;i++) 9 | for(j=1;j<=n;j++) 10 | if(i==j) 11 | p[i][j]=0; 12 | else 13 | p[i][j]=min(p[i][j],p[i][k]+p[k][j]); 14 | } 15 | int min(int a,int b) 16 | { 17 | if(a=%d",i,j,p[i][j]); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /data-structure/list/Circular_Linked_List.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* structure node Created */ 5 | struct Node 6 | { 7 | int data; 8 | struct Node *next; 9 | }; 10 | 11 | /* insert the node at the stating of a Circular 12 | linked list */ 13 | void push(struct Node **head_ref, int data) 14 | { 15 | struct Node *ptr1 = (struct Node *)malloc(sizeof(struct Node)); 16 | struct Node *temp = *head_ref; 17 | ptr1->data = data; 18 | ptr1->next = *head_ref; 19 | 20 | /* If linked list is not NULL then set the next of last node */ 21 | if (*head_ref != NULL) 22 | { 23 | while (temp->next != *head_ref) 24 | temp = temp->next; 25 | temp->next = ptr1; 26 | } 27 | else 28 | ptr1->next = ptr1; /*For the first node */ 29 | 30 | *head_ref = ptr1; 31 | } 32 | 33 | /* Function to print nodes in a given Circular linked list */ 34 | void printList(struct Node *head) 35 | { 36 | struct Node *temp = head; 37 | if (head != NULL) 38 | { 39 | do 40 | { 41 | printf("%d ", temp->data); 42 | temp = temp->next; 43 | } while (temp != head); 44 | } 45 | } 46 | 47 | /* Driver program to test above functions */ 48 | int main() 49 | { 50 | /* Initialize lists as empty */ 51 | struct Node *head = NULL; 52 | 53 | /* Created linked list will be 11->2->56->12 */ 54 | push(&head, 12); 55 | push(&head, 56); 56 | push(&head, 2); 57 | push(&head, 11); 58 | 59 | printf(" Contents of Circular Linked List \n "); 60 | printList(head); 61 | 62 | getchar(); 63 | return 0; 64 | } -------------------------------------------------------------------------------- /data-structure/graph/dfs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct node 5 | { 6 | struct node *next; 7 | int vertex; 8 | }node; 9 | node *G[20]; 10 | //heads of linked list 11 | int visited[20]; 12 | int n; 13 | void read_graph(); 14 | void insert(int,int); 15 | void DFS(int); 16 | void main() 17 | { 18 | int i; 19 | read_graph(); 20 | 21 | for(i=0;ivertex; 35 | 36 | if(!visited[i]) 37 | DFS(i); 38 | p=p->next; 39 | } 40 | } 41 | void read_graph() 42 | { 43 | int i,vi,vj,no_of_edges; 44 | printf("Enter number of vertices:"); 45 | 46 | scanf("%d",&n); 47 | for(i=0;ivertex=vj; 67 | q->next=NULL; 68 | if(G[vi]==NULL) 69 | G[vi]=q; 70 | else 71 | { 72 | p=G[vi]; 73 | 74 | while(p->next!=NULL) 75 | p=p->next; 76 | p->next=q; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /Algorithms/string/Reverse String.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define MAX 1000 5 | 6 | int top=-1; 7 | int item; 8 | 9 | char stack_string[MAX]; 10 | 11 | void pushChar(char item); 12 | 13 | //function to pop character (item) 14 | char popChar(void); 15 | 16 | //function to check stack is empty or not 17 | int isEmpty(void); 18 | 19 | //function to check stack is full or not 20 | int isFull(void); 21 | 22 | int main() 23 | { 24 | char str[MAX]; 25 | 26 | int i; 27 | 28 | printf("Enter a string: "); 29 | scanf("%[^\n]s",str); //read string with spaces 30 | 31 | for(i=0;i 2 | 3 | void SetUnion(int * a, int * b, int n1, int n2) { 4 | int k = 0, i, j; 5 | int c[20]; 6 | 7 | for (i = 0; i < n1; i++) { 8 | for (j = 0; j < k; j++) { 9 | if (c[j] == a[i]) 10 | break; 11 | } 12 | if (j == k) { 13 | c[k] = a[i]; 14 | k++; 15 | } 16 | } 17 | for (i = 0; i < n2; i++) { 18 | for (j = 0; j < k; j++) { 19 | if (c[j] == b[i]) 20 | break; 21 | } 22 | if (j == k) { 23 | c[k] = b[i]; 24 | k++; 25 | } 26 | } 27 | for (int i = 0; i < k; i++) 28 | printf("%d ", c[i]); 29 | 30 | } 31 | 32 | int symDiff(int * a, int * b, int * c, int m, int n) { 33 | int flag, len = 0; 34 | 35 | for (int i = 0; i < n; i++) { 36 | flag = 0; 37 | for (int j = 0; j < m; j++) { 38 | if (a[i] == b[j]) { 39 | flag = 1; 40 | break; 41 | } 42 | } 43 | if (flag == 0) { 44 | c[len++] = a[i]; 45 | 46 | } 47 | } 48 | return len; 49 | } 50 | 51 | int main() { 52 | int a[20], b[20], a_b[20], b_a[20]; 53 | 54 | int n, m, flag = 0, len = 0; 55 | printf("Enter the number of elements in the set A: "); 56 | scanf("%d", &n); 57 | for (int i = 0; i < n; i++) { 58 | scanf("%d", &a[i]); 59 | } 60 | 61 | printf("Enter the number of elements in the set B: "); 62 | scanf("%d", &m); 63 | for (int i = 0; i < m; i++) { 64 | scanf("%d", &b[i]); 65 | } 66 | 67 | int l1 = symDiff(&a, &b, &a_b, m, n); 68 | int l2 = symDiff(&b, &a, &b_a, n, m); 69 | SetUnion(&a_b, &b_a, l1, l2); 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /Algorithms/matrix/matrixmultiply.c: -------------------------------------------------------------------------------- 1 | // Program to find matrix multiplication 2 | #include 3 | int main() 4 | { 5 | int c[3][3], i[3][3], j = 0, d, e, f; 6 | printf("Enter the first matrix\n"); 7 | for (d = 0; d < 3; d++) 8 | { 9 | for (e = 0; e < 3; e++) 10 | 11 | { 12 | scanf("%d", &c[d][e]); 13 | } 14 | } 15 | 16 | printf("Enter the second matrix\n"); 17 | for (d = 0; d < 3; d++) 18 | { 19 | for (e = 0; e < 3; e++) 20 | { 21 | scanf("%d", &i[d][e]); 22 | } 23 | } 24 | printf("So your first matrix is\n"); 25 | for (d = 0; d < 3; d++) 26 | { 27 | for (e = 0; e < 3; e++) 28 | { 29 | printf("%d\t", c[d][e]); 30 | } 31 | printf("\n"); 32 | } 33 | printf("And your second matrix is \n"); 34 | for (d = 0; d < 3; d++) 35 | { 36 | for (e = 0; e < 3; e++) 37 | { 38 | printf("%d\t", i[d][e]); 39 | } 40 | printf("\n"); 41 | } 42 | printf("\n\n"); 43 | printf("Press one to confirm it or press 0 to terminate it\n"); 44 | scanf("%d", &f); 45 | if (f == 1) 46 | { 47 | printf("So your answer is \n"); 48 | for (d = 0; d < 3; d++) 49 | { 50 | for (e = 0; e < 3; e++) 51 | { 52 | for (f = 0; f < 3; f++) 53 | { 54 | 55 | j = j + (c[d][e] * i[f][e]); 56 | } 57 | 58 | c[d][e] = j; 59 | printf("%d\t", c[d][e]); 60 | j = 0; 61 | } 62 | printf("\n"); 63 | } 64 | } 65 | return 0; 66 | } -------------------------------------------------------------------------------- /ref_swap.c: -------------------------------------------------------------------------------- 1 | #include 2 | struct data 3 | { 4 | int year; 5 | int month,day; 6 | }date; 7 | int main() 8 | { 9 | int var=45-date.day; 10 | printf("Enter the day month and year"); 11 | scanf("\n%d%d%d",&date.day,&date.month,&date.year); 12 | printf("\n%d-%d-%d",date.day,date.month,date.year); 13 | printf("\nAfter 45 days"); 14 | if(date.day+15>30 && date.month%2==0 && date.month !=2) 15 | { 16 | printf("\n%d-%d-%d",date.day+15,date.month+2,date.year); 17 | 18 | } 19 | else if(date.day+15<30 && date.month%2==0 &&date.month !=2) 20 | { 21 | printf("\n%d-%d-%d",date.day+15,date.month+1,date.year); 22 | } 23 | if(date.day+14>31 && date.month%2!=0 && date.month !=2) 24 | { 25 | printf("\n%d-%d-%d",date.day+15,date.month+2,date.year); 26 | } 27 | else if(date.day+14<31 && date.month%2!=0 &&date.month !=2) 28 | { 29 | printf("\n%d-%d-%d",date.day+14,date.month+1,date.year); 30 | } 31 | else if(date.day+17>28 && date.month ==2) 32 | { 33 | printf("\n%d-%d-%d",date.day+17,date.month+2,date.year); 34 | } 35 | else if(date.day+16>29 && date.month ==2 && date.year %100==0||date.year %4==0||date.year %400==0) 36 | { 37 | printf("\n LEAP YEAR \n%d-%d-%d",date.day+16,date.month+2,date.year); 38 | } 39 | else if(date.day+17<29 && date.month ==2 && date.year %100==0||date.year %4==0||date.year %400==0) 40 | { 41 | printf("\n LEAP YEAR \n %d-%d-%d",date.day+16,date.month+1,date.year); 42 | } 43 | else if(date.day+16<28 && date.month ==2) 44 | { 45 | printf("\n%d-%d-%d",date.day+var,date.month,date.year); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /matrix multiplaction.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | int m,n,m2,n2,sum=0; 6 | printf("No of coloum of first matrix must be equal to the no. of rows in second matrix\n "); 7 | printf("Enter the number of rows and coloum of your first matrix\n"); 8 | scanf("%d %d",&m,&n); 9 | printf("Enter the number of rows and coloum of your first matrix\n"); 10 | scanf("%d %d",&m2,&n2); 11 | if(n==m2){ 12 | int a[m][n],b[m2][n2],result[m][n2]; 13 | printf("Enter your First matrix\n"); 14 | 15 | for (int i = 0; i 2 | 3 | void swap(int *a,int *b) //Time complexity of heapify is O(Logn). 4 | { //Time complexity of createAndBuildHeap() is O(n) and 5 | int t; //overall time complexity of Heap Sort is O(nLogn). 6 | t=*a; 7 | *a=*b; 8 | *b=t; 9 | } 10 | 11 | void heapify(int arr[], int n, int i) 12 | { 13 | int largest = i;// Initialize largest as root 14 | int l = 2*i + 1; // left = 2*i + 1 15 | int r = 2*i + 2; // right = 2*i + 2 16 | 17 | // If left child is larger than root 18 | if (l < n && arr[l] > arr[largest]) 19 | largest = l; 20 | 21 | // If right child is larger than largest so far 22 | if (r < n && arr[r] > arr[largest]) 23 | largest = r; 24 | 25 | // If largest is not root 26 | if (largest != i) 27 | { 28 | swap(&arr[i], &arr[largest]); 29 | 30 | // Recursively heapify the affected sub-tree 31 | heapify(arr, n, largest); 32 | } 33 | } 34 | 35 | // main function to do heap sort 36 | void heapSort(int arr[], int n) 37 | { 38 | // Build heap (rearrange array) 39 | for (int i = n / 2 - 1; i >= 0; i--) 40 | heapify(arr, n, i); 41 | 42 | // One by one extract an element from heap 43 | for (int i=n-1; i>=0; i--) 44 | { 45 | // Move current root to end 46 | swap(&arr[0], &arr[i]); 47 | 48 | // call max heapify on the reduced heap 49 | heapify(arr, i, 0); 50 | } 51 | } 52 | 53 | void printArray(int arr[], int n) 54 | { 55 | for (int i=0; i 2 | #include 3 | 4 | struct Node 5 | { 6 | int data; 7 | struct Node *next; 8 | }*top = NULL; 9 | 10 | void push(int); 11 | void pop(); 12 | void display(); 13 | 14 | void main() 15 | { 16 | int choice, value; 17 | clrscr(); 18 | printf("\n:: Stack using Linked List ::\n"); 19 | while(1){ 20 | printf("\n****** MENU ******\n"); 21 | printf("1. Push\n2. Pop\n3. Display\n4. Exit\n"); 22 | printf("Enter your choice: "); 23 | scanf("%d",&choice); 24 | switch(choice){ 25 | case 1: printf("Enter the value to be insert: "); 26 | scanf("%d", &value); 27 | push(value); 28 | break; 29 | case 2: pop(); break; 30 | case 3: display(); break; 31 | case 4: exit(0); 32 | default: printf("\nWrong selection!!! Please try again!!!\n"); 33 | } 34 | } 35 | } 36 | void push(int value) 37 | { 38 | struct Node *newNode; 39 | newNode = (struct Node*)malloc(sizeof(struct Node)); 40 | newNode->data = value; 41 | if(top == NULL) 42 | newNode->next = NULL; 43 | else 44 | newNode->next = top; 45 | top = newNode; 46 | printf("\nInsertion is Success!!!\n"); 47 | } 48 | void pop() 49 | { 50 | if(top == NULL) 51 | printf("\nStack is Empty!!!\n"); 52 | else{ 53 | struct Node *temp = top; 54 | printf("\nDeleted element: %d", temp->data); 55 | top = temp->next; 56 | free(temp); 57 | } 58 | } 59 | void display() 60 | { 61 | if(top == NULL) 62 | printf("\nStack is Empty!!!\n"); 63 | else{ 64 | struct Node *temp = top; 65 | while(temp->next != NULL){ 66 | printf("%d--->",temp->data); 67 | temp = temp -> next; 68 | } 69 | printf("%d--->NULL",temp->data); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Algorithms/string/reverseofstringusingstack.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define SIZE 10 4 | #define TRUE 1 5 | #define FALSE 0 6 | /**********************************************/ 7 | struct stack 8 | { 9 | int TOP; 10 | int Item [SIZE]; 11 | }; 12 | /**********************************************/ 13 | 14 | struct stack S; 15 | 16 | /**********************************************/ 17 | void Initalize(void) 18 | { 19 | S.TOP=-1; 20 | } 21 | /**********************************************/ 22 | 23 | int Empty(void) 24 | { 25 | if(S.TOP==-1) 26 | return TRUE; 27 | else 28 | return FALSE; 29 | } 30 | /**********************************************/ 31 | void Push( int x) 32 | { 33 | 34 | if(S.TOP== SIZE-1) 35 | { 36 | printf("Stack Overflow"); 37 | exit (1); 38 | } 39 | S.TOP=S.TOP+1; 40 | S.Item[S.TOP]=x; 41 | } 42 | /**********************************************/ 43 | 44 | int Pop() 45 | { 46 | int x; 47 | if(Empty()) 48 | { 49 | 50 | printf("Stack Underflow"); 51 | exit(1); 52 | } 53 | x=S.Item[S.TOP]; 54 | S.TOP=S.TOP-1; 55 | return x; 56 | } 57 | /**********************************************/ 58 | rev( char str[]) 59 | { 60 | Initalize(); 61 | int i=0; 62 | char x; 63 | while(str[i]!='\0') 64 | { 65 | Push(str[i]); 66 | i++; 67 | } 68 | while(!Empty()) 69 | { 70 | x=Pop(); 71 | printf("%c",x); 72 | } 73 | } 74 | /**********************************************/ 75 | void main() 76 | { 77 | char str[10]; 78 | printf("ENTER THE STRING: "); 79 | gets(str); 80 | rev(str); 81 | 82 | } 83 | 84 | -------------------------------------------------------------------------------- /Algorithms/string/Longest_Common_subsequence.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | 7 | int max(int a, int b) 8 | { 9 | return (a > b)? a : b; 10 | } 11 | 12 | 13 | void lcs( char *X, char *Y, int m, int n ) 14 | { 15 | int L[m+1][n+1],i,j; 16 | 17 | //build L[m+1][n+1] in bottom up fashion 18 | for (i=0; i<=m; i++) 19 | { 20 | for (j=0; j<=n; j++) 21 | { 22 | if (i == 0 || j == 0) 23 | L[i][j] = 0; 24 | else if (X[i-1] == Y[j-1]) 25 | L[i][j] = L[i-1][j-1] + 1; 26 | else 27 | L[i][j] = max(L[i-1][j], L[i][j-1]); 28 | } 29 | } 30 | 31 | 32 | int index = L[m][n]; 33 | 34 | char lcs[index+1]; 35 | lcs[index] = '\0'; 36 | 37 | // Start from the right-most-bottom-most corner and store characters in lcs[] 38 | int a = m, b = n; 39 | while (a > 0 && b > 0) 40 | { 41 | // If current character in X[] and Y are same, then 42 | // current character is part of LCS 43 | if (X[a-1] == Y[b-1]) 44 | { 45 | lcs[index-1] = X[a-1]; 46 | a--; b--; index--; // reduce values of i, j and index 47 | } 48 | 49 | 50 | //find larger go, in its direction 51 | else if (L[a-1][b] > L[a][b-1]) 52 | a--; 53 | else 54 | b--; 55 | } 56 | 57 | 58 | printf("LCS of "); 59 | for(i=0; i 9 | #include 10 | #include 11 | #define size 100 12 | int arr[size]; 13 | int front=-1,rear=-1; 14 | void enqueue(int x) 15 | { 16 | if (rear==size-1) 17 | { 18 | printf("queues is full"); 19 | } 20 | else if (front==-1&&rear==-1) 21 | { 22 | front=0,rear=0; 23 | arr[rear]=x; 24 | } 25 | else 26 | { 27 | ++rear; 28 | arr[rear]=x; 29 | } 30 | } 31 | void dequeue() 32 | { 33 | if (front==-1&&rear==-1) 34 | { 35 | printf("queue is empty"); 36 | return; 37 | } 38 | else if (front==rear) 39 | { 40 | front=-1;rear=-1; 41 | } 42 | else 43 | { 44 | ++front; 45 | } 46 | } 47 | bool is_empty() 48 | { 49 | if (front==-1&&rear==-1) 50 | { 51 | return true; 52 | } 53 | else false; 54 | } 55 | void print() 56 | { 57 | printf("\n"); 58 | for (int i = front; i <= rear; i++) 59 | { 60 | printf("%d ",arr[i]); 61 | } 62 | } 63 | int intmain() 64 | { 65 | int x,n; 66 | while(1) 67 | { 68 | printf("\n\n***** MENU *****\n"); 69 | printf("1. insert\n2. remove\n3. DIsplay\n4. Exit"); 70 | printf("\nEnter your choice: "); 71 | scanf("%d",&n); 72 | switch(n) 73 | { 74 | case 1: printf("Enter the value to be inserted: "); 75 | scanf("%d",&x); 76 | enqueue(x); 77 | break; 78 | case 2: dequeue(); 79 | break; 80 | case 3: print(); 81 | break; 82 | case 4: exit(0); 83 | default:printf("\nWrong selection!!! Try again!!!"); 84 | 85 | } 86 | } 87 | } 88 | 89 | 90 | -------------------------------------------------------------------------------- /data-structure/graph/Travelling Salesman Problem.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int ary[10][10],completed[10],n,cost=0,st,et; 4 | 5 | void takeInput() 6 | { 7 | int i,j; 8 | 9 | printf("Enter the number of villages: "); 10 | scanf("%d",&n); 11 | 12 | printf("\nEnter the Cost Matrix\n"); 13 | 14 | for(i=0;i < n;i++) 15 | { 16 | printf("\nEnter Elements of Row: %d\n",i+1); 17 | 18 | for( j=0;j < n;j++) 19 | scanf("%d",&ary[i][j]); 20 | 21 | completed[i]=0; 22 | } 23 | 24 | printf("\n\nThe cost list is:"); 25 | 26 | for( i=0;i < n;i++) 27 | { 28 | printf("\n"); 29 | 30 | for(j=0;j < n;j++) 31 | printf("\t%d",ary[i][j]); 32 | } 33 | } 34 | 35 | void mincost(int city) 36 | { 37 | int i,ncity; 38 | 39 | completed[city]=1; 40 | 41 | printf("%d--->",city+1); 42 | ncity=least(city); 43 | 44 | if(ncity==999) 45 | { 46 | ncity=0; 47 | printf("%d",ncity+1); 48 | cost+=ary[city][ncity]; 49 | 50 | return; 51 | } 52 | 53 | mincost(ncity); 54 | } 55 | 56 | int least(int c) 57 | { 58 | int i,nc=999; 59 | int min=999,kmin; 60 | 61 | for(i=0;i < n;i++) 62 | { 63 | if((ary[c][i]!=0)&&(completed[i]==0)) 64 | if(ary[c][i]+ary[i][c] < min) 65 | { 66 | min=ary[i][0]+ary[c][i]; 67 | kmin=ary[c][i]; 68 | nc=i; 69 | } 70 | } 71 | 72 | if(min!=999) 73 | cost+=kmin; 74 | 75 | return nc; 76 | } 77 | 78 | int main() 79 | { st=clock(); 80 | takeInput(); 81 | 82 | printf("\n\nThe Path is:\n"); 83 | mincost(0); //passing 0 because starting vertex 84 | 85 | printf("\n\nMinimum cost is %d\n ",cost); 86 | et=clock(); 87 | printf("Start Time =%d\nEnd Time =%d",st,et); 88 | return 0; 89 | } 90 | -------------------------------------------------------------------------------- /stack using linked list: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 5 4 | 5 | typedef struct node 6 | { 7 | int data; 8 | struct node *next; 9 | }node; 10 | 11 | node *top; 12 | 13 | void push() 14 | { 15 | int num; 16 | node *temp; 17 | printf("\n Enter the number : "); 18 | scanf("%d",&num); 19 | temp=(node*)malloc(sizeof(node)); 20 | temp->data=num; 21 | temp->next=NULL; 22 | 23 | if(top==NULL) 24 | top=temp; 25 | else 26 | { 27 | temp->next=top; 28 | top=temp; 29 | } 30 | 31 | } 32 | 33 | void pop() 34 | { 35 | if(top==NULL) 36 | printf("\n U N D E R F L O W"); 37 | else 38 | { 39 | printf("\n Delete element %d ",top->data); 40 | top=top->next; 41 | } 42 | } 43 | void peep() 44 | { 45 | if(top==NULL) 46 | printf("\n Empty stack"); 47 | else 48 | { 49 | printf("\n Top most element %d",top->data); 50 | } 51 | } 52 | void display() 53 | { 54 | node *ptr; 55 | if(top==NULL) 56 | printf("\n Empty stack"); 57 | else 58 | { 59 | ptr=top; 60 | while(ptr!=NULL) 61 | { 62 | printf("\n %d",ptr->data); 63 | ptr=ptr->next; 64 | } 65 | } 66 | } 67 | int main() 68 | { 69 | 70 | int ch; 71 | top=NULL; // initialize 72 | while(1) 73 | { 74 | printf("\n 1 for PUSH "); 75 | printf("\n 2 for POP "); 76 | printf("\n 3 for PEEP"); 77 | printf("\n 4 for DISPLAY "); 78 | printf("\n 5 for EXIT "); 79 | printf("\n Enter the choice "); 80 | scanf("%d",&ch); 81 | 82 | switch(ch) 83 | { 84 | case 1: 85 | push(); 86 | break; 87 | 88 | case 2: 89 | pop(); 90 | break; 91 | 92 | case 3: 93 | peep(); 94 | break; 95 | 96 | case 4: 97 | display(); 98 | break; 99 | 100 | case 5: 101 | exit(1); 102 | 103 | default: 104 | printf("\n Oops! Wrong choice "); 105 | } 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /sorting/InsertionSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | void insert(int a[], int n) 3 | { 4 | int i, j, temp; 5 | for (i = 1; i < n; i++) 6 | { 7 | temp = a[i]; 8 | j = i - 1; 9 | 10 | while (j >= 0 && temp <= a[j]) 11 | { 12 | a[j + 1] = a[j]; 13 | j = j - 1; 14 | } 15 | a[j + 1] = temp; 16 | } 17 | } 18 | 19 | void printArr(int a[], int n) 20 | { 21 | int i; 22 | for (i = 0; i < n; i++) 23 | printf("%d ", a[i]); 24 | } 25 | 26 | int main() 27 | { 28 | 29 | int n; 30 | printf("Enter the total numbers you want to enter\n"); 31 | scanf("%d", &n); 32 | int a[n]; 33 | printf("Enter the numbers below \n"); 34 | for (int i = 0; i < n; i++) 35 | { 36 | scanf("%d", &a[i]); 37 | } 38 | 39 | printf("Before sorting array elements are - \n"); 40 | printArr(a, n); 41 | insert(a, n); 42 | printf("\nAfter sorting array elements are - \n"); 43 | printArr(a, n); 44 | 45 | return 0; 46 | } 47 | ======= 48 | #include 49 | void insert(int a[], int n) 50 | { 51 | int i, j, temp; 52 | for (i = 1; i < n; i++) { 53 | temp = a[i]; 54 | j = i - 1; 55 | 56 | while(j>=0 && temp <= a[j]) 57 | { 58 | a[j+1] = a[j]; 59 | j = j-1; 60 | } 61 | a[j+1] = temp; 62 | } 63 | } 64 | 65 | void printArr(int a[], int n) 66 | { 67 | int i; 68 | for (i = 0; i < n; i++) 69 | printf("%d ", a[i]); 70 | } 71 | 72 | int main() 73 | { 74 | int a[] = { 12, 31, 25, 8, 32, 17 }; 75 | int n = sizeof(a) / sizeof(a[0]); 76 | printf("Before sorting array elements are - \n"); 77 | printArr(a, n); 78 | insert(a, n); 79 | printf("\nAfter sorting array elements are - \n"); 80 | printArr(a, n); 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /sorting/MergeSort.c: -------------------------------------------------------------------------------- 1 | //this is the c program for merge sort 2 | #include 3 | #include 4 | 5 | void merge(int arr[], int l, int m, int r) 6 | { 7 | int i, j, k; 8 | int n1 = m - l + 1; 9 | int n2 = r - m; 10 | 11 | int L[n1], R[n2]; 12 | 13 | for (i = 0; i < n1; i++) 14 | L[i] = arr[l + i]; 15 | for (j = 0; j < n2; j++) 16 | R[j] = arr[m + 1 + j]; 17 | 18 | i = 0; 19 | j = 0; 20 | k = l; 21 | while (i < n1 && j < n2) 22 | { 23 | if (L[i] <= R[j]) 24 | { 25 | arr[k] = L[i]; 26 | i++; 27 | } 28 | else 29 | { 30 | arr[k] = R[j]; 31 | j++; 32 | } 33 | k++; 34 | } 35 | 36 | while (i < n1) 37 | { 38 | arr[k] = L[i]; 39 | i++; 40 | k++; 41 | } 42 | 43 | while (j < n2) 44 | { 45 | arr[k] = R[j]; 46 | j++; 47 | k++; 48 | } 49 | } 50 | 51 | void mergeSort(int arr[], int l, int r) 52 | { 53 | if (l < r) 54 | { 55 | int m = l + (r - l) / 2; 56 | 57 | mergeSort(arr, l, m); 58 | mergeSort(arr, m + 1, r); 59 | 60 | merge(arr, l, m, r); 61 | } 62 | } 63 | 64 | void printArray(int A[], int size) 65 | { 66 | int i; 67 | for (i = 0; i < size; i++) 68 | printf("%d ", A[i]); 69 | printf("\n"); 70 | } 71 | 72 | int main() 73 | { 74 | int arr_size; 75 | printf("Enter the size of the array\n"); 76 | scanf("%d", &arr_size); 77 | int arr[arr_size]; 78 | printf("Enter the elements inside the array\n"); 79 | for (int i = 0; i < arr_size; i++) 80 | { 81 | scanf("%d", &arr[i]); 82 | } 83 | 84 | printf("Given array is \n"); 85 | printArray(arr, arr_size); 86 | 87 | mergeSort(arr, 0, arr_size - 1); 88 | 89 | printf("\nSorted array is \n"); 90 | printArray(arr, arr_size); 91 | return 0; 92 | } -------------------------------------------------------------------------------- /data-structure/stack/Stack-Valid-Parenthesis.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define SIZE 15 3 | 4 | // Stack Implementation 5 | char stack[SIZE]; 6 | int top = -1; 7 | 8 | void push(char c) { 9 | if (top >= SIZE-1) { 10 | printf("Make stack bigger \n"); 11 | return; 12 | } 13 | top++; 14 | stack[top] = c; 15 | } 16 | 17 | char pop() { 18 | if (top == -1) { 19 | printf("Stack is empty \n"); 20 | return ' '; 21 | } 22 | top--; 23 | return stack[top+1]; 24 | } 25 | 26 | char peek() { 27 | if (top == -1) { 28 | printf("Stack is empty \n"); 29 | return '\0'; 30 | } 31 | printf("peek -> %c", stack[top]); 32 | return stack[top]; 33 | } 34 | 35 | void print() { 36 | if (top == -1) { 37 | printf("Nothing to print \n"); 38 | return; 39 | } 40 | int i = top; 41 | printf("\n"); 42 | while(i >= 0) { 43 | printf(" | %c |\n", stack[i]); 44 | i--; 45 | } 46 | printf(" '''''\n"); 47 | } 48 | 49 | int isValid(char *str) { 50 | if (str[0] == '\0') return 0; 51 | char c; 52 | for (int i = 0; str[i] != '\0'; i++) { 53 | if (str[i] == ')') { 54 | c = pop(); 55 | if ( c != '(') return 0; 56 | }else if (str[i] == ']') { 57 | c = pop(); 58 | if ( c != '[') return 0; 59 | }else if (str[i] == '}') { 60 | c = pop(); 61 | if ( c != '{') return 0; 62 | } else { 63 | push(str[i]); 64 | } 65 | } 66 | c = peek(); 67 | if(c == '\0') { 68 | return 1; 69 | } else { 70 | return 0; 71 | } 72 | } 73 | 74 | // Main Function 75 | int main() 76 | { 77 | char str[15] = "{[]}"; 78 | int flag = isValid(str); 79 | if (flag == 1) { 80 | printf("Valid Set\n"); 81 | } else { 82 | printf("Invalid Set\n"); 83 | } 84 | return 0; 85 | } 86 | 87 | -------------------------------------------------------------------------------- /magic_square.c: -------------------------------------------------------------------------------- 1 | //A program to print the magic square 2 | /* 3 | Conditions to check 4 | 1. The position of next number is calculated by decrementing row number of previous number by 1, and incrementing the column number of previous number by 1. At any time, if the calculated row position becomes -1, it will wrap around to n-1. Similarly, if the calculated column position becomes n, it will wrap around to 0. 5 | 6 | 2. If the magic square already contains a number at the calculated position, calculated column position will be decremented by 2, and calculated row position will be incremented by 1. 7 | 8 | 3. If the calculated row position is -1 & calculated column position is n, the new position would be: (0, n-2).*/ 9 | #include 10 | int main() 11 | { 12 | int n,sum,m[50][50],val=0; 13 | printf("Enter the size odd number only"); 14 | scanf("%d",&n); 15 | int i = n/2; 16 | int j = n-1; 17 | 18 | // One by one put all values in magic square 19 | for (int num=1; num <= n*n; ) 20 | { 21 | if (i==-1 && j==n) //3rd condition 22 | { 23 | j = n-2; 24 | i = 0; 25 | } 26 | else 27 | { 28 | // 1st condition helper if next number 29 | // goes to out of square's right side 30 | if (j == n) 31 | j = 0; 32 | 33 | // 1st condition helper if next number 34 | // is goes to out of square's upper side 35 | if (i < 0) 36 | i=n-1; 37 | } 38 | if (m[i][j]) //2nd condition 39 | { 40 | j -= 2; 41 | i++; 42 | continue; 43 | } 44 | else 45 | m[i][j] = num++; //set number 46 | 47 | j++; i--; //1st condition 48 | } 49 | 50 | // Print magic square 51 | printf("The Magic Square for n=%d:\nSum of " 52 | "each row or column %d:\n\n", n, n*(n*n+1)/2); 53 | for (i=0; i P[j]) 55 | { 56 | swap(&AT[i], &AT[j]); 57 | swap(&BT[i], &BT[j]); 58 | swap(&P[i], &P[j]); 59 | swap(&WT[i],&WT[j]); 60 | swap(&TAT[i],&TAT[j]); 61 | swap(&CT[i],&CT[j]); 62 | } 63 | } 64 | } 65 | 66 | printf("\nP\tAT\tBT\tCT\tTAT\tWT"); 67 | for(int i=0; i 2 | using namespace std; 3 | struct Interval 4 | { 5 | int low, high; 6 | }; 7 | struct ITNode 8 | { 9 | Interval *i; 10 | int max; 11 | ITNode *left, *right; 12 | }; 13 | ITNode * newNode(Interval i) 14 | { 15 | ITNode *temp = new ITNode; 16 | temp->i = new Interval(i); 17 | temp->max = i.high; 18 | temp->left = temp->right = NULL; 19 | return temp; 20 | }; 21 | ITNode *insert(ITNode *root, Interval i) 22 | { 23 | if (root == NULL) 24 | return newNode(i); 25 | int l = root->i->low; 26 | if (i.low < l) 27 | root->left = insert(root->left, i); 28 | else 29 | root->right = insert(root->right, i); 30 | if (root->max < i.high) 31 | root->max = i.high; 32 | 33 | return root; 34 | } 35 | bool doOVerlap(Interval i1, Interval i2) 36 | { 37 | if (i1.low <= i2.high && i2.low <= i1.high) 38 | return true; 39 | return false; 40 | } 41 | Interval *overlapSearch(ITNode *root, Interval i) 42 | { 43 | if (root == NULL) return NULL; 44 | if (doOVerlap(*(root->i), i)) 45 | return root->i; 46 | if (root->left != NULL && root->left->max >= i.low) 47 | return overlapSearch(root->left, i); 48 | return overlapSearch(root->right, i); 49 | } 50 | 51 | void inorder(ITNode *root) 52 | { 53 | if (root == NULL) return; 54 | 55 | inorder(root->left); 56 | 57 | cout << "[" << root->i->low << ", " << root->i->high << "]" 58 | << " max = " << root->max << endl; 59 | 60 | inorder(root->right); 61 | } 62 | int main() 63 | { 64 | Interval ints[] = {{15, 20}, {10, 30}, {17, 19}, 65 | {5, 20}, {12, 15}, {30, 40} 66 | }; 67 | int n = sizeof(ints)/sizeof(ints[0]); 68 | ITNode *root = NULL; 69 | for (int i = 0; i < n; i++) 70 | root = insert(root, ints[i]); 71 | 72 | cout << "Inorder traversal of constructed Interval Tree is\n"; 73 | inorder(root); 74 | 75 | Interval x = {6, 7}; 76 | 77 | cout << "\nSearching for interval [" << x.low << "," << x.high << "]"; 78 | Interval *res = overlapSearch(root, x); 79 | if (res == NULL) 80 | cout << "\nNo Overlapping Interval"; 81 | else 82 | cout << "\nOverlaps with [" << res->low << ", " << res->high << "]"; 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /prim's algorithm.c: -------------------------------------------------------------------------------- 1 | // 2 | // main.c 3 | // prims algo 4 | // 5 | // Created by Vikram kumar on 19/05/22. 6 | // 7 | 8 | // Prim's Algorithm in C 9 | 10 | #include 11 | #include 12 | 13 | #define INF 9999999 14 | 15 | // number of vertices in graph 16 | #define V 5 17 | 18 | // create a 2d array of size 5x5 19 | //for adjacency matrix to represent graph 20 | int G[V][V] = { 21 | {0, 9, 75, 0, 0}, 22 | {9, 0, 95, 19, 42}, 23 | {75, 95, 0, 51, 66}, 24 | {0, 19, 51, 0, 31}, 25 | {0, 42, 66, 31, 0}}; 26 | 27 | int main() { 28 | int no_edge; // number of edge 29 | 30 | // create a array to track selected vertex 31 | // selected will become true otherwise false 32 | int selected[V]; 33 | 34 | // set selected false initially 35 | memset(selected, false, sizeof(selected)); 36 | 37 | // set number of edge to 0 38 | no_edge = 0; 39 | 40 | // the number of egde in minimum spanning tree will be 41 | // always less than (V -1), where V is number of vertices in 42 | //graph 43 | 44 | // choose 0th vertex and make it true 45 | selected[0] = true; 46 | 47 | int x; // row number 48 | int y; // col number 49 | 50 | // print for edge and weight 51 | printf("Edge : Weight\n"); 52 | 53 | while (no_edge < V - 1) { 54 | //For every vertex in the set S, find the all adjacent vertices 55 | // , calculate the distance from the vertex selected at step 1. 56 | // if the vertex is already in the set S, discard it otherwise 57 | //choose another vertex nearest to selected vertex at step 1. 58 | 59 | int min = INF; 60 | x = 0; 61 | y = 0; 62 | 63 | for (int i = 0; i < V; i++) { 64 | if (selected[i]) { 65 | for (int j = 0; j < V; j++) { 66 | if (!selected[j] && G[i][j]) { // not in selected and there is an edge 67 | if (min > G[i][j]) { 68 | min = G[i][j]; 69 | x = i; 70 | y = j; 71 | } 72 | } 73 | } 74 | } 75 | } 76 | printf("%d - %d : %d\n", x, y, G[x][y]); 77 | selected[y] = true; 78 | no_edge++; 79 | } 80 | 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /data-structure/stack/Stack-Tower-of-Hanoi.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define SIZE 15 3 | 4 | // Stack Implementation 5 | char stack[SIZE]; 6 | int top = -1; 7 | 8 | void push(char c) { 9 | if (top >= SIZE-1) { 10 | printf("Make stack bigger \n"); 11 | return; 12 | } 13 | top++; 14 | stack[top] = c; 15 | } 16 | 17 | char pop() { 18 | if (top == -1) { 19 | printf("Stack is empty \n"); 20 | return ' '; 21 | } 22 | top--; 23 | return stack[top+1]; 24 | } 25 | 26 | char peek() { 27 | if (top == -1) { 28 | printf("Stack is empty \n"); 29 | return '\0'; 30 | } 31 | printf("peek -> %c", stack[top]); 32 | return stack[top]; 33 | } 34 | 35 | // Soln -> Tower of Hanoi 36 | void towerOfHanoi(int n, char fromRod, char toRod, char auxRod) { 37 | if (n == 1) { 38 | printf("\n Moved disk 1 from rod %c to rod %c", fromRod, toRod); 39 | return; 40 | } 41 | 42 | towerOfHanoi(n-1, fromRod, auxRod, toRod); 43 | printf("\n Moved disk %d from rod %c to rod %c", n, fromRod, toRod); 44 | towerOfHanoi(n-1, auxRod, toRod, fromRod); 45 | } 46 | 47 | // Main Function 48 | int main() 49 | { 50 | char fromRod = 'A'; 51 | char toRod = 'C'; 52 | char auxRod = 'B'; 53 | towerOfHanoi(4, fromRod, toRod, auxRod); 54 | // Answer to Compare the Results 55 | printf("\n==================================="); 56 | printf("\n Move disk 1 from rod A to rod B"); 57 | printf("\n Move disk 2 from rod A to rod C"); 58 | printf("\n Move disk 1 from rod B to rod C"); 59 | printf("\n Move disk 3 from rod A to rod B"); 60 | printf("\n Move disk 1 from rod C to rod A"); 61 | printf("\n Move disk 2 from rod C to rod B"); 62 | printf("\n Move disk 1 from rod A to rod B"); 63 | printf("\n Move disk 4 from rod A to rod C"); 64 | printf("\n Move disk 1 from rod B to rod C"); 65 | printf("\n Move disk 2 from rod B to rod A"); 66 | printf("\n Move disk 1 from rod C to rod A"); 67 | printf("\n Move disk 3 from rod B to rod C"); 68 | printf("\n Move disk 1 from rod A to rod B"); 69 | printf("\n Move disk 2 from rod A to rod C"); 70 | printf("\n Move disk 1 from rod B to rod C"); 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /sorting/merge_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | void merge(int array[], int const left, int const mid, int const right) 6 | { 7 | auto const subArrayOne = mid - left + 1; 8 | auto const subArrayTwo = right - mid; 9 | 10 | auto *leftArray = new int[subArrayOne], 11 | *rightArray = new int[subArrayTwo]; 12 | 13 | for (auto i = 0; i < subArrayOne; i++) 14 | leftArray[i] = array[left + i]; 15 | for (auto j = 0; j < subArrayTwo; j++) 16 | rightArray[j] = array[mid + 1 + j]; 17 | 18 | auto indexOfSubArrayOne = 0, 19 | indexOfSubArrayTwo = 0; 20 | int indexOfMergedArray = left; 21 | 22 | while (indexOfSubArrayOne < subArrayOne && indexOfSubArrayTwo < subArrayTwo) { 23 | if (leftArray[indexOfSubArrayOne] <= rightArray[indexOfSubArrayTwo]) { 24 | array[indexOfMergedArray] = leftArray[indexOfSubArrayOne]; 25 | indexOfSubArrayOne++; 26 | } 27 | else { 28 | array[indexOfMergedArray] = rightArray[indexOfSubArrayTwo]; 29 | indexOfSubArrayTwo++; 30 | } 31 | indexOfMergedArray++; 32 | } 33 | while (indexOfSubArrayOne < subArrayOne) { 34 | array[indexOfMergedArray] = leftArray[indexOfSubArrayOne]; 35 | indexOfSubArrayOne++; 36 | indexOfMergedArray++; 37 | } 38 | while (indexOfSubArrayTwo < subArrayTwo) { 39 | array[indexOfMergedArray] = rightArray[indexOfSubArrayTwo]; 40 | indexOfSubArrayTwo++; 41 | indexOfMergedArray++; 42 | } 43 | } 44 | 45 | void mergeSort(int array[], int const begin, int const end) 46 | { 47 | if (begin >= end) 48 | return; 49 | 50 | auto mid = begin + (end - begin) / 2; 51 | mergeSort(array, begin, mid); 52 | mergeSort(array, mid + 1, end); 53 | merge(array, begin, mid, end); 54 | } 55 | 56 | void printArray(int A[], int size) 57 | { 58 | for (auto i = 0; i < size; i++) 59 | cout << A[i] << " "; 60 | } 61 | 62 | int main() 63 | { 64 | int arr[] = { 12, 11, 13, 5, 6, 7 }; 65 | auto arr_size = sizeof(arr) / sizeof(arr[0]); 66 | 67 | cout << "Given array is \n"; 68 | printArray(arr, arr_size); 69 | 70 | mergeSort(arr, 0, arr_size - 1); 71 | 72 | cout << "\nSorted array is \n"; 73 | printArray(arr, arr_size); 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /sorting/bucket_sort.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | struct bucket 4 | { 5 | int count; 6 | int* value; 7 | }; 8 | void bucketSort(int array[],int n) 9 | { 10 | struct bucket buckets[3]; 11 | int i,j,k,t; 12 | for (i=0;i<3;i++) 13 | { 14 | buckets[i].count = 0; 15 | buckets[i].value = (int*)malloc(sizeof(int) * n); 16 | } 17 | 18 | for (i=0;i30) 25 | { 26 | buckets[2].value[buckets[2].count++]=array[i]; 27 | } 28 | else 29 | { 30 | buckets[1].value[buckets[1].count++] = array[i]; 31 | } 32 | } 33 | printf("\nBefore sorting:\n"); 34 | for(j=0;j<3;j++) 35 | { 36 | printf("Bucket %d contains:",j+1); 37 | for(i=0;ibuckets[k].value[j+1]) 55 | { 56 | t=buckets[k].value[j]; 57 | buckets[k].value[j]=buckets[k].value[j+1]; 58 | buckets[k].value[j+1]=t; 59 | } 60 | } 61 | } 62 | } 63 | 64 | 65 | for(i=0;i<3;i++) 66 | { 67 | printf("Bucket %d contains - ",i+1); 68 | for(j=0;j 2 | #include 3 | #define max 10 4 | struct node 5 | { 6 | int info; 7 | struct node *link; 8 | } *front=NULL,*rear=NULL; 9 | void enqueue(int n) 10 | { 11 | struct node *tmp; 12 | tmp=(struct node *)malloc(sizeof(struct node)); 13 | if(tmp==NULL) 14 | { 15 | printf("Memory not availabl"); 16 | return ; 17 | } 18 | tmp->info=n; 19 | tmp->link=NULL; 20 | if(front==NULL) 21 | front=tmp; 22 | else 23 | rear->link=tmp; 24 | rear=tmp; 25 | } 26 | int dequeue() 27 | { 28 | struct node *tmp; 29 | int item; 30 | if(front==NULL) 31 | { 32 | printf("Queue is empty"); 33 | return 0 ; 34 | } 35 | tmp=front; 36 | front=front->link; 37 | item=tmp->info; 38 | free(tmp); 39 | return item; 40 | } 41 | int peek() 42 | { 43 | int item; 44 | if(front==NULL) 45 | { 46 | printf("queue is empty"); 47 | return 0; 48 | } 49 | item =front->info; 50 | return item; 51 | } 52 | void display() 53 | { 54 | struct node *tmp; 55 | tmp=front; 56 | if(isEmpty()) 57 | { 58 | printf("queue is empty"); 59 | return ; 60 | } 61 | printf("Queue is :"); 62 | while(tmp!=NULL) 63 | { 64 | printf("%d\t",tmp->info); 65 | tmp=tmp->link; 66 | } 67 | } 68 | int isEmpty() 69 | { 70 | if(rear==NULL) 71 | return 1; 72 | else return 0; 73 | } 74 | 75 | int main(void) { 76 | int choice,item ; 77 | while(1) 78 | { 79 | printf("\n1.push\n"); 80 | printf("2.pop\n"); 81 | printf("3.peek\n"); 82 | printf("4.display\n"); 83 | printf("5.EXIT"); 84 | printf("\nEnter your choice :"); 85 | scanf("%d",&choice); 86 | switch(choice) 87 | { 88 | case 1: printf("Enter the item to be inserted:"); 89 | scanf("%d",&item); 90 | enqueue(item); 91 | break; 92 | case 2: item =dequeue(); 93 | if(item==0) 94 | return 0; 95 | else 96 | printf("The deleted element is %d",item); 97 | break; 98 | case 3: item =peek(); 99 | printf("Element present at the top of the queue is %d",item); 100 | break; 101 | case 4: 102 | display(); 103 | break; 104 | case 5 : exit (1); 105 | default : printf("Wrong choice"); 106 | 107 | } 108 | 109 | } 110 | return 0; 111 | } 112 | -------------------------------------------------------------------------------- /sparseMatrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Element 4 | { 5 | public: 6 | int i,j,x; 7 | }; 8 | class Sparse 9 | { 10 | int m,n,num; 11 | Element *ele; 12 | public: 13 | Sparse(int m,int n,int num) 14 | { 15 | this->m=m; 16 | this->n=n; 17 | this->num=num; 18 | ele=new Element [this->num]; 19 | } 20 | ~Sparse() 21 | { 22 | delete []ele; 23 | } 24 | Sparse operator+(Sparse &s); 25 | friend istream & operator>>(istream &is,Sparse &s); 26 | friend ostream & operator<<(ostream &os,Sparse &s); 27 | }; 28 | Sparse Sparse:: operator+(Sparse &s) 29 | { 30 | int i,j,k; 31 | //if(m!=s.m or n!=s.n) 32 | // return nullptr; 33 | Sparse *sum=new Sparse(m,n,num+s.num); 34 | i=j=k=0; 35 | while(iele[k++]=ele[i++]; 39 | else if(ele[i].i>s.ele[j].i) 40 | sum->ele[k++]=s.ele[j++]; 41 | else 42 | { 43 | if(ele[i].jele[k++]=ele[i++]; 45 | else if(ele[i].j>s.ele[j].j) 46 | sum->ele[k++]=s.ele[j++]; 47 | else 48 | { 49 | sum->ele[k]=ele[i]; 50 | sum->ele[k++].x=ele[i++].x+s.ele[j++].x; 51 | } 52 | } 53 | } 54 | for(;iele[k++]=ele[i]; 56 | for(;jele[k++]=s.ele[j]; 58 | sum->num=k; 59 | return *sum; 60 | } 61 | istream & operator>>(istream &is,Sparse &s) 62 | { 63 | cout<<"enter non zero elements"<>s.ele[i].i>>s.ele[i].j>>s.ele[i].x; 66 | return is; 67 | } 68 | ostream & operator<<(ostream &os,Sparse &s) 69 | { 70 | int k=0; 71 | for(int i=0;i>s1; 89 | cin>>s2; 90 | Sparse s3=(s1+s2); 91 | cout< 2 | #include 3 | struct node{ 4 | int data; 5 | struct node *link; 6 | }; 7 | 8 | struct node *front=NULL; 9 | struct node *rear=NULL; 10 | void enqueue(int value) 11 | { 12 | struct node *new = malloc(sizeof(struct node)) ; 13 | new->data=value; 14 | new->link = NULL; 15 | if(rear==NULL) 16 | { 17 | front=rear=new; 18 | rear->link=front; 19 | } 20 | else 21 | { 22 | rear->link=new; 23 | rear=new; 24 | rear->link=front; 25 | } 26 | printf("Element %d has been inserted to the queue \n", new->data); 27 | } 28 | 29 | void dequeue() 30 | { 31 | struct node *temp; 32 | temp=front; 33 | if(front==NULL && rear==NULL) 34 | { 35 | printf("QUEUE is EMPTY \n"); 36 | } 37 | else if (front==rear) 38 | { 39 | front=rear=NULL; 40 | printf("Element %d has been deleted from the queue \n", temp->data); 41 | free(temp); 42 | } 43 | else 44 | { 45 | front=front->link; 46 | rear->link=front; 47 | printf("Element %d has been deleted from the queue \n", temp->data); 48 | free(temp); 49 | } 50 | 51 | 52 | } 53 | int peek() 54 | { 55 | if((front==NULL) && (rear==NULL)) 56 | { 57 | printf("queue is empty\n"); 58 | } 59 | else 60 | { 61 | printf("The front element is %d\n",front->data); 62 | } 63 | return 0; 64 | } 65 | void display() 66 | { 67 | struct node *temp; 68 | temp=front; 69 | printf("elements in the queue are :"); 70 | if ((front==NULL) && (rear==NULL)) 71 | { 72 | printf("queue is empty\n"); 73 | } 74 | else 75 | { 76 | while(temp->link!=front) 77 | { 78 | printf("%d\n",temp->data); 79 | temp=temp->link; 80 | } 81 | printf("%d\n",temp->data); 82 | } 83 | } 84 | 85 | void reverseq() // reversing queue function 86 | { 87 | if(front==NULL) 88 | { 89 | printf("QUEUE is EMPTY \n"); 90 | return; 91 | } 92 | 93 | struct node *current = front, *prev = rear, *next = front->link; 94 | 95 | do { 96 | current->link = prev; 97 | prev = current; 98 | current = next; 99 | next = next->link; 100 | } 101 | while (current != front); 102 | 103 | front = rear; 104 | rear = current; 105 | } 106 | int main(void) 107 | { 108 | enqueue(10); 109 | enqueue(20); 110 | enqueue(30); 111 | display(); 112 | reverseq(); // applying reverse queue function 113 | display(); // checking the queue that it is reversed or not 114 | return 0; 115 | } 116 | -------------------------------------------------------------------------------- /data-structure/stack/max-rectangle.cpp: -------------------------------------------------------------------------------- 1 | 2 | class Solution{ 3 | public: 4 | void ngl(vector &v,vector&l ,int n){ 5 | stack> st; 6 | l.push_back(-1); 7 | st.push(make_pair(v[0],0)); 8 | int i = 1; 9 | while(!st.empty() && i &v,vector&l ,int n){ 27 | stack> st; 28 | l.push_back(n); 29 | st.push(make_pair(v[n-1],n-1)); 30 | int i = n-2; 31 | while(!st.empty() && i>=0){ 32 | int curr = v[i]; 33 | if(st.top().first < curr){ 34 | l.push_back(st.top().second); 35 | //l[i] = st.top().second; 36 | st.push(make_pair(curr,i)); 37 | i--; 38 | } 39 | else{ 40 | st.pop(); 41 | if(st.empty()){ 42 | l.push_back(n); 43 | // l[i] = n; 44 | st.push(make_pair(curr,i)); 45 | i--; 46 | } 47 | } 48 | } 49 | reverse(l.begin(),l.end()); 50 | } 51 | int MAH(vector &v , int n){ 52 | vector l; 53 | vector r; 54 | ngl(v,l,n); 55 | ngr(v,r,n); 56 | int ans = 0; 57 | for(int i=0;i sum(m,0); 66 | for(int i=0;i v; 68 | for(int j=0;j 4 | #include 5 | int main() 6 | { 7 | int n; // Declaration of variable n 8 | printf("Enter the number of elements\n"); 9 | scanf("%d", &n); 10 | int arr[n]; // Declaring variable of size n 11 | printf("Enter the elements of the array\n"); 12 | for (int i = 0; i < n; i++) 13 | { 14 | scanf("%d", &arr[i]); // Storing elements of array using for loop 15 | } 16 | // Calculation of mean of the elements 17 | float sum = 0; 18 | for (int i = 0; i < n; i++) 19 | { 20 | sum += arr[i]; 21 | } 22 | printf("The mean of the elements are %.3f\n", sum / n); 23 | 24 | // Calculation of median of the elements 25 | int i, j, min, temp; 26 | for (i = 0; i < n - 1; i++) 27 | { 28 | min = i; 29 | for (j = i + 1; j < n; j++) 30 | { 31 | if (arr[j] < arr[min]) 32 | { 33 | min = j; 34 | } 35 | } 36 | temp = arr[i]; 37 | arr[i] = arr[min]; 38 | arr[min] = temp; 39 | } 40 | if (n % 2 == 0) 41 | { 42 | int a = arr[n / 2]; 43 | int b = arr[n / 2 - 1]; 44 | printf("The median of the elements is %.2f\n", (a + b) / 2.0); 45 | } 46 | else 47 | { 48 | printf("The median of the elements is %d\n", arr[(n + 1) / 2 - 1]); 49 | } 50 | 51 | // Calculation of mode of the elements 52 | int e[100] = {0}, mode, max = 0, k = 0, f = 1; 53 | for (int x = 0; x < n - 1; x++) 54 | { 55 | mode = 0; 56 | for (int y = x + 1; y < n; y++) 57 | { 58 | if (arr[x] == arr[y]) 59 | { 60 | mode++; 61 | } 62 | } 63 | if ((mode > max) && (mode != 0)) 64 | { 65 | k = 0; 66 | max = mode; 67 | e[k] = arr[x]; 68 | k++; 69 | } 70 | else if (mode == max) 71 | { 72 | e[k] = arr[x]; 73 | k++; 74 | } 75 | } 76 | for (int z = 0; z < n; z++) 77 | { 78 | if (arr[z] == e[z]) 79 | f++; 80 | } 81 | if (f == n) 82 | { 83 | printf("The mode of this data does not exist as every number has one frequency"); 84 | } 85 | else 86 | { 87 | printf("The mode of the data : "); 88 | for (int x = 0; x < k; x++) 89 | { 90 | printf(" %d ", e[x]); 91 | } 92 | } 93 | return 0; 94 | } 95 | -------------------------------------------------------------------------------- /structure/3.c: -------------------------------------------------------------------------------- 1 | //A program to find the row and column sum 2 | #include 3 | #include 4 | struct emp{ 5 | char name[100][100]; 6 | char item[50][50]; 7 | int mon[50][50]; 8 | 9 | }e; 10 | void main() 11 | { 12 | char ch; 13 | int nop,mon[50],j,i,prd; 14 | printf("Enter the number of sales persons --> "); 15 | scanf("%d",&nop); 16 | printf("Enter how many products are there (!!!Should be less than number of persons!!!)--> "); 17 | scanf("%d",&prd); 18 | printf("------------------------------------------\n"); 19 | for(i=0;i ",i+1); 22 | scanf("%s",e.name[i]); 23 | } 24 | printf("------------------------------------------\n"); 25 | for(i=0;i ",i+1); 28 | scanf("%s",e.item[i]); 29 | } 30 | printf("------------------------------------------\n"); 31 | printf("!!!Displaying the results!!!"); 32 | for(i=0;i=prd) 43 | { 44 | for(i=0;i ",i+1); 49 | scanf("%s",&e.item[j]); 50 | printf("Enter the sales money of sales person --> ",i+1); 51 | scanf("%d",&e.mon[j]); 52 | printf("\nDo you want to conform changes (y/n)"); 53 | scanf(" %c",&ch); 54 | }while(ch == 'N'||ch == 'n'); 55 | j++; 56 | } 57 | } 58 | else 59 | { 60 | printf("\n!!!! WARNING !!!! Make sure no of products is greater than no of sales persons"); 61 | } 62 | //Displaying 63 | printf("\n ----------------------------------------------"); 64 | printf("\n\t\tThe sales and product report\n"); 65 | printf(" ----------------------------------------------\n"); 66 | if(nop>=prd) 67 | { 68 | printf("\tSales person \tProduct \tMoney\n"); 69 | for(i=0;i Make sure no of persons is greater than no of products"); 79 | } 80 | printf("\n ----------------------------------------------\n"); 81 | 82 | } 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /data-structure/queue/circular_queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define max 5 3 | int a_queue[max]; 4 | int front=-1; 5 | int rear=-1; 6 | void enqueue(); 7 | void dequeue(); 8 | int isfull(); 9 | int isempty(); 10 | void peek(); 11 | void tranversal(); 12 | int main(){ 13 | int ch,x; 14 | do{ 15 | printf("\n1.insert\t2.delete\t3.checkfull\t4.check empty\t5.front element\t6.display queue\t7.exit\n"); 16 | printf("enter your choice:\t"); 17 | scanf("%d",&ch); 18 | switch(ch){ 19 | case 1:enqueue(); 20 | break; 21 | case 2:dequeue(); 22 | break; 23 | case 3:x=isfull(); 24 | if(x==1) 25 | printf("\nqueue is full"); 26 | else 27 | printf("\nnot full"); 28 | break; 29 | case 4:x=isempty(); 30 | if(x==1) 31 | printf("\nqueue is empty"); 32 | else 33 | printf("\nnot empty"); 34 | break; 35 | case 5:peek(); 36 | break; 37 | case 6:tranversal(); 38 | break; 39 | } 40 | }while(ch!=7); 41 | } 42 | void enqueue(){ 43 | int item; 44 | if(!isfull()){ 45 | printf("\nenter the element to insert:\n"); 46 | scanf("%d",&item); 47 | if(front==-1 && rear==-1) 48 | { 49 | front=rear=0; 50 | } 51 | else{ 52 | rear=(rear+1)%max; 53 | } 54 | a_queue[rear]=item; 55 | } 56 | } 57 | void dequeue(){ 58 | int value; 59 | if(!isempty()){ 60 | value=a_queue[front]; 61 | printf("\ndeleted element is:%d",value); 62 | if(front==rear){ 63 | front=rear=-1; 64 | } 65 | else{ 66 | front=(front+1)%max; 67 | } 68 | } 69 | } 70 | int isfull(){ 71 | if(rear==(front+1)%max){ 72 | printf("queue is full\n"); 73 | return 1; 74 | } 75 | else{ 76 | return 0; 77 | } 78 | } 79 | int isempty(){ 80 | if(front==-1 && rear==-1){ 81 | printf("queue is empty\n"); 82 | return 1; 83 | } 84 | else{ 85 | return 0; 86 | } 87 | } 88 | void peek(){ 89 | if(!isempty()) 90 | { 91 | printf("front element is:%d",a_queue[front]); 92 | } 93 | } 94 | void tranversal(){ 95 | if(!isempty()){ 96 | printf("queue element is:"); 97 | if(front 8 | #include 9 | 10 | 11 | 12 | struct node { 13 | int data; 14 | struct node *next; 15 | } *head; 16 | 17 | 18 | 19 | void createList(int n); 20 | void deleteList(); 21 | void displayList(); 22 | 23 | 24 | 25 | int main() 26 | { 27 | int n, choice; 28 | 29 | 30 | printf("Enter the total number of nodes: "); 31 | scanf("%d", &n); 32 | createList(n); 33 | 34 | printf("\nData in the list \n"); 35 | displayList(); 36 | 37 | printf("\nPress 1 to delete entire list: "); 38 | scanf("%d", &choice); 39 | 40 | /* Delete all nodes of list */ 41 | deleteList(); 42 | 43 | printf("\nData in the list \n"); 44 | displayList(); 45 | 46 | return 0; 47 | } 48 | 49 | 50 | void createList(int n) 51 | { 52 | struct node *newNode, *temp; 53 | int data, i; 54 | 55 | head = (struct node *)malloc(sizeof(struct node)); 56 | 57 | if(head == NULL) 58 | { 59 | printf("Unable to allocate memory."); 60 | } 61 | else 62 | { 63 | printf("Enter the data of node 1: "); 64 | scanf("%d", &data); 65 | 66 | head->data = data; 67 | head->next = NULL; 68 | 69 | temp = head; 70 | 71 | 72 | for(i=2; i<=n; i++) 73 | { 74 | newNode = (struct node *)malloc(sizeof(struct node)); 75 | 76 | 77 | if(newNode == NULL) 78 | { 79 | printf("Unable to allocate memory."); 80 | break; 81 | } 82 | else 83 | { 84 | printf("Enter the data of node %d: ", i); 85 | scanf("%d", &data); 86 | 87 | newNode->data = data; 88 | newNode->next = NULL; 89 | 90 | temp->next = newNode; 91 | temp = temp->next; 92 | } 93 | } 94 | 95 | printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n"); 96 | } 97 | } 98 | 99 | 100 | 101 | void deleteList() 102 | { 103 | struct node *temp; 104 | 105 | while(head != NULL) 106 | { 107 | temp = head; 108 | head = head->next; 109 | 110 | free(temp); 111 | } 112 | 113 | printf("SUCCESSFULLY DELETED ALL NODES OF LINKED LIST\n"); 114 | } 115 | 116 | 117 | void displayList() 118 | { 119 | struct node *temp; 120 | 121 | 122 | if(head == NULL) 123 | { 124 | printf("List is empty."); 125 | } 126 | else 127 | { 128 | temp = head; 129 | while(temp != NULL) 130 | { 131 | printf("Data = %d\n", temp->data); 132 | temp = temp->next; 133 | } 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /data-structure/queue/queue_linkedlist.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Queue Data Structure with Linked List 3 | * @author Ramon Almeida 4 | * @date 2021-10-09 5 | */ 6 | 7 | 8 | #include "stdio.h" 9 | #include "stdlib.h" 10 | #include "string.h" 11 | 12 | #define LENNAME 15 13 | 14 | typedef struct _Queue Queue; 15 | 16 | typedef struct _Item Item; 17 | 18 | struct _Item { 19 | char name[LENNAME]; 20 | struct _Item *next; 21 | }; 22 | 23 | struct _Queue { 24 | struct _Item head, *last; 25 | }; 26 | 27 | Queue *create_queue(); 28 | 29 | int is_empty(Queue *queue); 30 | 31 | int enqueue(Queue *queue, char *name); 32 | 33 | char *dequeue(Queue *queue); 34 | 35 | void queue_show(Queue *queue); 36 | 37 | Item *create_item(char *name); 38 | 39 | int main(int argc, char const *argv[]) { 40 | 41 | Queue *queue = create_queue(5); 42 | 43 | enqueue(queue, "Joseph"); 44 | enqueue(queue, "Maria"); 45 | enqueue(queue, "Bob"); 46 | queue_show(queue); 47 | 48 | printf("DEQUEUE: %s\n\n", dequeue(queue)); 49 | printf("DEQUEUE: %s\n\n", dequeue(queue)); 50 | printf("DEQUEUE: %s\n\n", dequeue(queue)); 51 | queue_show(queue); 52 | 53 | enqueue(queue, "Robert"); 54 | enqueue(queue, "Kirk"); 55 | queue_show(queue); 56 | 57 | printf("DEQUEUE: %s\n\n", dequeue(queue)); 58 | queue_show(queue); 59 | 60 | return 0; 61 | } 62 | 63 | Queue *create_queue() { 64 | 65 | Queue *queue = malloc(sizeof(Queue)); 66 | 67 | if (!queue) 68 | return NULL; 69 | 70 | queue->head.next = NULL; 71 | queue->last = &queue->head; 72 | 73 | return queue; 74 | } 75 | 76 | int is_empty(Queue *queue) { return !queue->head.next; } 77 | 78 | int enqueue(Queue *queue, char *name) { 79 | 80 | Item *item = create_item(name); 81 | 82 | if (!item) 83 | return 0; 84 | 85 | queue->last->next = item; 86 | queue->last = queue->last->next; 87 | 88 | return 1; 89 | } 90 | 91 | char *dequeue(Queue *queue) { 92 | 93 | if (is_empty(queue)) 94 | return NULL; 95 | 96 | Item *item = queue->head.next; 97 | queue->head.next = item->next; 98 | 99 | if (!queue->head.next) 100 | queue->last = &queue->head; 101 | 102 | char *name = malloc(LENNAME); 103 | strncpy(name, item->name, LENNAME); 104 | free(item); 105 | 106 | return name; 107 | } 108 | 109 | void queue_show(Queue *queue) { 110 | 111 | printf("Queue Status\n"); 112 | 113 | if (is_empty(queue)) { 114 | printf(" -> Is Empty <-\n\n"); 115 | return; 116 | } 117 | 118 | printf(" -> "); 119 | for (Item *item = queue->head.next; item; item = item->next) 120 | printf("%s, ", item->name); 121 | printf("\n\n"); 122 | } 123 | 124 | Item *create_item(char *name) { 125 | 126 | Item *item = malloc(sizeof(Item)); 127 | 128 | if (!item) 129 | return NULL; 130 | 131 | strncpy(item->name, name, LENNAME); 132 | item->next = NULL; 133 | 134 | return item; 135 | } -------------------------------------------------------------------------------- /sorting/quick-sort.c: -------------------------------------------------------------------------------- 1 | #define CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | 5 | void quickSort(int arr[], int firstIndex, int lastIndex) { 6 | // Quick sort: Selects an index (pivot) and sorts around it, meaning that all left items are lesser and 7 | // all right items are larger. This is performed recursively on either side of the pivot 8 | // Time Complexity: Best - O(n log2 n) Average - O(n log2 n) Worst - O(n^2) 9 | 10 | // Declare i, j, pivot, and temp as integers 11 | int i, j, pivot, temp; 12 | 13 | // Verify that a valid range has been given/ that the indices do not match 14 | // When the indices match, there will only be 1 item in the array to sort 15 | if (firstIndex < lastIndex) { 16 | // Select a value to pivot around, in this case the first index is selected 17 | // This is not always ideal, however in many cases this is fine 18 | pivot = firstIndex; 19 | 20 | // Set values for iterators i and j 21 | i = firstIndex; 22 | j = lastIndex; 23 | 24 | // Whilst i is less than j, make comparisons that close around the pivot 25 | while (i < j) { 26 | // Whilst the value at index i is less than or equal to that of the pivot 27 | // and i is less than the end of the array, increment i 28 | while (arr[i] <= arr[pivot] && i < lastIndex) { 29 | i++; 30 | } 31 | // Whilst the value at index j is larger than that at the pivot, decrement j 32 | while (arr[j] > arr[pivot]) { 33 | j--; 34 | } 35 | // If i is less than j, swap the values at the index locations 36 | // As prior loops filter until a swap is needed, this is where the swap takes place 37 | if (i < j) { 38 | arr[i] = arr[i] + arr[j]; 39 | arr[j] = arr[i] - arr[j]; 40 | arr[i] = arr[i] - arr[j]; 41 | } 42 | } 43 | 44 | // Update the value of the pivot index 45 | // NOTE: Arithmetic swapping here causes erroneous results, possibly due to overlapping values 46 | temp = arr[pivot]; 47 | arr[pivot] = arr[j]; 48 | arr[j] = temp; 49 | 50 | // As the pivot should now be placed correctly, sort the subarrays on either side of it 51 | quickSort(arr, firstIndex, j - 1); 52 | quickSort(arr, j + 1, lastIndex); 53 | 54 | } 55 | } 56 | 57 | // Driver program for testing the quick sort 58 | int main() { 59 | int arr[100], arrLen, i; 60 | 61 | printf("Enter number of elements in the array: "); 62 | scanf("%d", &arrLen); 63 | 64 | printf("Enter %d integers\n\r", arrLen); 65 | 66 | for (i = 0; i < arrLen; i++) { 67 | scanf("%d", &arr[i]); 68 | } 69 | 70 | quickSort(arr, 0, arrLen); 71 | 72 | printf("Printing the sorted array:\n\r"); 73 | 74 | for (i = 0; i < arrLen; i++) { 75 | printf("%d\n\r", arr[i]); 76 | } 77 | 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /data-structure/queue/queue_static.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Queue Data Structure 3 | * @author Ramon Almeida 4 | * @date 2021-10-09 5 | */ 6 | 7 | 8 | #include "stdio.h" 9 | #include "stdlib.h" 10 | #include "string.h" 11 | 12 | #define LENNAME 15 13 | 14 | #define NEXTPOSI(v, m) ((v) % (m)) 15 | 16 | typedef struct _Queue Queue; 17 | 18 | typedef struct _Item Item; 19 | 20 | struct _Item { 21 | char name[LENNAME]; 22 | }; 23 | 24 | struct _Queue { 25 | int p_s, p_e, size; 26 | struct _Item *items; 27 | }; 28 | 29 | Queue *create_queue(int size); 30 | 31 | int is_empty(Queue *queue); 32 | 33 | int is_full(Queue *queue); 34 | 35 | int enqueue(Queue *queue, char *name); 36 | 37 | char *dequeue(Queue *queue); 38 | 39 | void queue_show(Queue *queue); 40 | 41 | int main(int argc, char const *argv[]) { 42 | 43 | Queue *queue = create_queue(5); 44 | 45 | enqueue(queue, "Joseph"); 46 | enqueue(queue, "Maria"); 47 | enqueue(queue, "Bob"); 48 | queue_show(queue); 49 | 50 | printf("DEQUEUE: %s\n\n", dequeue(queue)); 51 | printf("DEQUEUE: %s\n\n", dequeue(queue)); 52 | printf("DEQUEUE: %s\n\n", dequeue(queue)); 53 | queue_show(queue); 54 | 55 | enqueue(queue, "Robert"); 56 | enqueue(queue, "Kirk"); 57 | queue_show(queue); 58 | 59 | printf("DEQUEUE: %s\n\n", dequeue(queue)); 60 | queue_show(queue); 61 | 62 | return 0; 63 | } 64 | 65 | Queue *create_queue(int size) { 66 | 67 | if (size < 1) 68 | return NULL; 69 | 70 | size++; 71 | Queue *queue = malloc(sizeof(Queue)); 72 | 73 | if (!queue) 74 | return NULL; 75 | 76 | queue->items = malloc((sizeof(Item) * size)); 77 | 78 | if (!queue->items) { 79 | free(queue); 80 | return NULL; 81 | } 82 | 83 | queue->p_s = queue->p_e = 0; 84 | queue->size = size; 85 | 86 | return queue; 87 | } 88 | 89 | int is_empty(Queue *queue) { return queue->p_s == queue->p_e; } 90 | 91 | int is_full(Queue *queue) { 92 | return NEXTPOSI(queue->p_e + 1, queue->size) == queue->p_s; 93 | } 94 | 95 | int enqueue(Queue *queue, char *name) { 96 | 97 | if (is_full(queue)) 98 | return 0; 99 | 100 | strncpy(queue->items[queue->p_e].name, name, LENNAME); 101 | queue->p_e = NEXTPOSI(queue->p_e + 1, queue->size); 102 | 103 | return 1; 104 | } 105 | 106 | char *dequeue(Queue *queue) { 107 | 108 | if (is_empty(queue)) 109 | return NULL; 110 | 111 | char *name = malloc(LENNAME); 112 | strncpy(name, queue->items[queue->p_s].name, LENNAME); 113 | queue->p_s = NEXTPOSI(queue->p_s + 1, queue->size); 114 | 115 | return name; 116 | } 117 | 118 | void queue_show(Queue *queue) { 119 | 120 | printf("Queue Status\n"); 121 | 122 | if (is_empty(queue)) { 123 | printf(" -> Is Empty <-\n\n"); 124 | return; 125 | } 126 | 127 | printf(" -> "); 128 | for (int i = queue->p_s; i != queue->p_e; i = NEXTPOSI(i + 1, queue->size)) 129 | printf("%s, ", queue->items[i].name); 130 | printf("\n\n"); 131 | } 132 | -------------------------------------------------------------------------------- /data-structure/stack/Infix_to_postfix.c: -------------------------------------------------------------------------------- 1 | // C program to convert infix expression to postfix 2 | #include 3 | #include 4 | #include 5 | 6 | // Stack type 7 | struct Stack 8 | { 9 | int top; 10 | unsigned capacity; 11 | int* array; 12 | }; 13 | 14 | // Stack Operations 15 | struct Stack* createStack( unsigned capacity ) 16 | { 17 | struct Stack* stack = (struct Stack*) 18 | malloc(sizeof(struct Stack)); 19 | 20 | if (!stack) 21 | return NULL; 22 | 23 | stack->top = -1; 24 | stack->capacity = capacity; 25 | 26 | stack->array = (int*) malloc(stack->capacity * 27 | sizeof(int)); 28 | 29 | return stack; 30 | } 31 | int isEmpty(struct Stack* stack) 32 | { 33 | return stack->top == -1 ; 34 | } 35 | char peek(struct Stack* stack) 36 | { 37 | return stack->array[stack->top]; 38 | } 39 | char pop(struct Stack* stack) 40 | { 41 | if (!isEmpty(stack)) 42 | return stack->array[stack->top--] ; 43 | return '$'; 44 | } 45 | void push(struct Stack* stack, char op) 46 | { 47 | stack->array[++stack->top] = op; 48 | } 49 | 50 | 51 | // A utility function to check if 52 | // the given character is operand 53 | int isOperand(char ch) 54 | { 55 | return (ch >= 'a' && ch <= 'z') || 56 | (ch >= 'A' && ch <= 'Z'); 57 | } 58 | 59 | // A utility function to return 60 | // precedence of a given operator 61 | // Higher returned value means 62 | // higher precedence 63 | int Prec(char ch) 64 | { 65 | switch (ch) 66 | { 67 | case '+': 68 | case '-': 69 | return 1; 70 | 71 | case '*': 72 | case '/': 73 | return 2; 74 | 75 | case '^': 76 | return 3; 77 | } 78 | return -1; 79 | } 80 | 81 | 82 | // The main function that 83 | // converts given infix expression 84 | // to postfix expression. 85 | int infixToPostfix(char* exp) 86 | { 87 | int i, k; 88 | 89 | // Create a stack of capacity 90 | // equal to expression size 91 | struct Stack* stack = createStack(strlen(exp)); 92 | if(!stack) // See if stack was created successfully 93 | return -1 ; 94 | 95 | for (i = 0, k = -1; exp[i]; ++i) 96 | { 97 | 98 | // If the scanned character is 99 | // an operand, add it to output. 100 | if (isOperand(exp[i])) 101 | exp[++k] = exp[i]; 102 | 103 | // If the scanned character is an 104 | // ‘(‘, push it to the stack. 105 | else if (exp[i] == '(') 106 | push(stack, exp[i]); 107 | 108 | // If the scanned character is an ‘)’, 109 | // pop and output from the stack 110 | // until an ‘(‘ is encountered. 111 | else if (exp[i] == ')') 112 | { 113 | while (!isEmpty(stack) && peek(stack) != '(') 114 | exp[++k] = pop(stack); 115 | if (!isEmpty(stack) && peek(stack) != '(') 116 | return -1; // invalid expression 117 | else 118 | pop(stack); 119 | } 120 | else // an operator is encountered 121 | { 122 | while (!isEmpty(stack) && 123 | Prec(exp[i]) <= Prec(peek(stack))) 124 | exp[++k] = pop(stack); 125 | push(stack, exp[i]); 126 | } 127 | 128 | } 129 | 130 | // pop all the operators from the stack 131 | while (!isEmpty(stack)) 132 | exp[++k] = pop(stack ); 133 | 134 | exp[++k] = '\0'; 135 | printf( "%s", exp ); 136 | } 137 | 138 | // Driver program to test above functions 139 | int main() 140 | { 141 | char exp[] = "a+b*(c^d-e)^(f+g*h)-i"; 142 | infixToPostfix(exp); 143 | return 0; 144 | } 145 | -------------------------------------------------------------------------------- /string.c: -------------------------------------------------------------------------------- 1 | //A program to simulate string length copy ,concatenation ,reverse, replace and substring 2 | #include 3 | #include 4 | #include 5 | int main() 6 | { 7 | char str[50],choice,concat[50],rep[50],sub[50],name[50],rev[50],rp[50],new[50]; 8 | int i=0,j=0,ch,pos,len,index; 9 | printf("Enter the string "); 10 | gets(str); 11 | do 12 | { 13 | printf("\t\tString Menu\n"); 14 | printf("------------------------------------------\n"); 15 | printf("1.String-length\n"); 16 | printf("2.Copy\n"); 17 | printf("3.Concatenate\n"); 18 | printf("4.Reverse\n"); 19 | printf("5.Replace\n"); 20 | printf("6.Substring\n"); 21 | printf("------------------------------------------\n"); 22 | printf("!!!Please enter a choice(1-6)!!!\n"); 23 | scanf("%d",&ch); 24 | //String length 25 | if(ch==1) 26 | { 27 | printf("\nThe length of the string '%s' is %d --> ",str,strlen(str)); 28 | } 29 | //String copy 30 | else if(ch==2) 31 | { 32 | printf("\nEnter a the word that should be copied to %s --> ",str); 33 | scanf("%s",name); 34 | strcpy(str,name); 35 | printf("\nAfter copying '%s'",name); 36 | 37 | } 38 | //String Concatenate 39 | else if(ch==3) 40 | { 41 | printf("Enter a new string for performing concatenation --> "); 42 | scanf("%s",concat); 43 | strncat(str,concat,strlen(str)-1); 44 | printf("The concatenated string is '%s'",str); 45 | } 46 | //String Reverse 47 | else if(ch==4) 48 | { 49 | for(i=0;i<=strlen(str);i++) 50 | { 51 | rev[i]=str[strlen(str)-1-i]; 52 | } 53 | printf("The reversed string is %s",rev); 54 | } 55 | //String Replace 56 | else if(ch==5) 57 | { 58 | printf("------------------------------------------\n"); 59 | printf("The orginal string is --> %s",str); 60 | printf("\nEnter the word to be replaced %s --> ",&str); 61 | scanf(" %[^\n]s",rep); 62 | printf("Enter the replacing word %s --> ",&str); 63 | scanf(" %[^\n]s",rp); 64 | 65 | if(strlen(rep)==strlen(rp)) 66 | { 67 | for (int i = 0; i "); 79 | puts(str); 80 | printf("------------------------------------------\n"); 81 | } 82 | else 83 | { 84 | printf("\n-------------------------------------------------------------"); 85 | printf("\nPlz Enter the new word same len as that of the replacing word\n") ; 86 | printf("-------------------------------------------------------------\n"); 87 | } 88 | } 89 | //Substring 90 | else if(ch==6) 91 | { 92 | printf("Enter the position and length of the substring of the string\n"); 93 | scanf("%d %d",&pos,&len); 94 | i=0; 95 | while(i "); 104 | scanf(" %c",&choice); 105 | 106 | }while(choice=='Y'||choice=='y'); 107 | } 108 | -------------------------------------------------------------------------------- /data-structure/list/linkedlist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct node 6 | { 7 | struct node *prev; 8 | struct node *next; 9 | int marks; 10 | int regno; 11 | char name[100]; 12 | }; 13 | struct node *head; 14 | 15 | void insertion_beginning() 16 | { 17 | struct node *ptr; 18 | int reg; 19 | char name[100]; 20 | int marks; 21 | ptr = (struct node *)malloc(sizeof(struct node)); 22 | if(ptr == NULL) 23 | { 24 | printf("\nOVERFLOW"); 25 | } 26 | else 27 | { 28 | printf("\nEnter Registration Number"); 29 | scanf("%d",®); 30 | printf("\nEnter Name"); 31 | scanf("%s",name); 32 | printf("\nEnter marks"); 33 | scanf("%d",&marks); 34 | 35 | if(head==NULL) 36 | { 37 | ptr->next = NULL; 38 | ptr->prev=NULL; 39 | ptr->regno=reg; 40 | strcpy(ptr->name,name); 41 | ptr->marks=marks; 42 | head=ptr; 43 | } 44 | else 45 | { 46 | ptr->regno=reg; 47 | strcpy(ptr->name,name); 48 | ptr->marks=marks; 49 | ptr->prev=NULL; 50 | ptr->next = head; 51 | head->prev=ptr; 52 | head=ptr; 53 | } 54 | printf("\nNode inserted\n"); 55 | } 56 | 57 | } 58 | void insertion_last() 59 | { 60 | struct node *ptr,*temp; 61 | int reg; 62 | char name[100]; 63 | int marks; 64 | ptr = (struct node *) malloc(sizeof(struct node)); 65 | if(ptr == NULL) 66 | { 67 | printf("\nOVERFLOW"); 68 | } 69 | else 70 | { 71 | printf("\nEnter Registration Number"); 72 | scanf("%d",®); 73 | printf("\nEnter Name"); 74 | scanf("%s",name); 75 | printf("\nEnter marks"); 76 | scanf("%d",&marks); 77 | if(head == NULL) 78 | { 79 | ptr->next = NULL; 80 | ptr->prev = NULL; 81 | head = ptr; 82 | } 83 | else 84 | { 85 | temp = head; 86 | while(temp->next!=NULL) 87 | { 88 | temp = temp->next; 89 | } 90 | temp->next = ptr; 91 | ptr ->prev=temp; 92 | ptr->next = NULL; 93 | } 94 | 95 | } 96 | printf("\nnode inserted\n"); 97 | } 98 | 99 | void display() 100 | { 101 | struct node *ptr; 102 | printf("\n printing values...\n"); 103 | ptr = head; 104 | while(ptr != NULL) 105 | { 106 | printf("%d\n",ptr->regno); 107 | printf("%s\n",ptr->name); 108 | printf("%d\n",ptr->marks); 109 | ptr=ptr->next; 110 | } 111 | } 112 | 113 | void main () 114 | { 115 | int choice =0; 116 | while(choice != 9) 117 | { 118 | printf("\n*********Main Menu*********\n"); 119 | printf("\nChoose one option from the following list ...\n"); 120 | printf("\n===============================================\n"); 121 | printf("\n1.Insert in begining\n2.Insert at last\n3.Show"); 122 | printf("\nEnter your choice?\n"); 123 | scanf("\n%d",&choice); 124 | switch(choice) 125 | { 126 | case 1: 127 | insertion_beginning(); 128 | break; 129 | case 2: 130 | insertion_last(); 131 | break; 132 | case 3: 133 | display(); 134 | break; 135 | exit(0); 136 | break; 137 | default: 138 | printf("Please enter valid choice.."); 139 | } 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /data-structure/graph/Tree-traversal.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node { 5 | int data; 6 | 7 | struct node *leftChild; 8 | struct node *rightChild; 9 | }; 10 | 11 | struct node *root = NULL; 12 | 13 | void insert(int data) { 14 | struct node *tempNode = (struct node*) malloc(sizeof(struct node)); 15 | struct node *current; 16 | struct node *parent; 17 | 18 | tempNode->data = data; 19 | tempNode->leftChild = NULL; 20 | tempNode->rightChild = NULL; 21 | 22 | //if tree is empty 23 | if(root == NULL) { 24 | root = tempNode; 25 | } else { 26 | current = root; 27 | parent = NULL; 28 | 29 | while(1) { 30 | parent = current; 31 | 32 | //go to left of the tree 33 | if(data < parent->data) { 34 | current = current->leftChild; 35 | 36 | //insert to the left 37 | if(current == NULL) { 38 | parent->leftChild = tempNode; 39 | return; 40 | } 41 | } //go to right of the tree 42 | else { 43 | current = current->rightChild; 44 | 45 | //insert to the right 46 | if(current == NULL) { 47 | parent->rightChild = tempNode; 48 | return; 49 | } 50 | } 51 | } 52 | } 53 | } 54 | 55 | struct node* search(int data) { 56 | struct node *current = root; 57 | printf("Visiting elements: "); 58 | 59 | while(current->data != data) { 60 | if(current != NULL) 61 | printf("%d ",current->data); 62 | 63 | //go to left tree 64 | if(current->data > data) { 65 | current = current->leftChild; 66 | } 67 | //else go to right tree 68 | else { 69 | current = current->rightChild; 70 | } 71 | 72 | //not found 73 | if(current == NULL) { 74 | return NULL; 75 | } 76 | } 77 | 78 | return current; 79 | } 80 | 81 | void pre_order_traversal(struct node* root) { 82 | if(root != NULL) { 83 | printf("%d ",root->data); 84 | pre_order_traversal(root->leftChild); 85 | pre_order_traversal(root->rightChild); 86 | } 87 | } 88 | 89 | void inorder_traversal(struct node* root) { 90 | if(root != NULL) { 91 | inorder_traversal(root->leftChild); 92 | printf("%d ",root->data); 93 | inorder_traversal(root->rightChild); 94 | } 95 | } 96 | 97 | void post_order_traversal(struct node* root) { 98 | if(root != NULL) { 99 | post_order_traversal(root->leftChild); 100 | post_order_traversal(root->rightChild); 101 | printf("%d ", root->data); 102 | } 103 | } 104 | 105 | int main() { 106 | int i; 107 | int array[7] = { 27, 14, 35, 10, 19, 31, 42 }; 108 | 109 | for(i = 0; i < 7; i++) 110 | insert(array[i]); 111 | 112 | i = 31; 113 | struct node * temp = search(i); 114 | 115 | if(temp != NULL) { 116 | printf("[%d] Element found.", temp->data); 117 | printf("\n"); 118 | }else { 119 | printf("[ x ] Element not found (%d).\n", i); 120 | } 121 | 122 | i = 15; 123 | temp = search(i); 124 | 125 | if(temp != NULL) { 126 | printf("[%d] Element found.", temp->data); 127 | printf("\n"); 128 | }else { 129 | printf("[ x ] Element not found (%d).\n", i); 130 | } 131 | 132 | printf("\nPreorder traversal: "); 133 | pre_order_traversal(root); 134 | 135 | printf("\nInorder traversal: "); 136 | inorder_traversal(root); 137 | 138 | printf("\nPost order traversal: "); 139 | post_order_traversal(root); 140 | 141 | return 0; 142 | } 143 | -------------------------------------------------------------------------------- /data-structure/array/array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | printf("Enter 10 Elemants of Array: "); 7 | int a[15]; 8 | int i,n,p,x,val,temp; 9 | int fai=9; 10 | for(int i=0;i<10;i++){ 11 | scanf("%d",&a[i]); 12 | 13 | } 14 | printf("\nEntered Array is: "); 15 | for(int i=0;i<10;i++){ 16 | printf("%d ",a[i]); 17 | } 18 | 19 | do{ 20 | printf("\n"); 21 | printf("1-> Insertion \n"); 22 | printf("2-> Deletion \n"); 23 | printf("3-> Search \n"); 24 | printf("4-> Display \n"); 25 | printf("5-> Sort \n"); 26 | printf("6-> Exit"); 27 | printf("\n\nSelection an option: "); 28 | scanf("%d",&n); 29 | 30 | 31 | switch(n) 32 | { 33 | case 1: printf("Enter index where do you want to insert number: "); 34 | scanf("%d",&p); 35 | printf("Enter Value: "); 36 | scanf("%d",&val); 37 | for(i=fai;i>=p;i--){ 38 | a[i+1]=a[i]; 39 | } 40 | fai++; 41 | a[p]=val; 42 | for(i=0;i<=fai;i++){ 43 | printf("%d ",a[i]); 44 | } 45 | break; 46 | 47 | case 2: printf("Enter index to be deleted: "); 48 | scanf("%d",&p); 49 | 50 | for(i=p+1;i<=fai;i++){ 51 | a[i-1]=a[i]; 52 | } 53 | fai--; 54 | break; 55 | 56 | case 3:for (i = 0; i < fai; i++) 57 | { 58 | for (int j = 0; j < (fai - i - 1); j++) 59 | { 60 | if (a[j] > a[j + 1]) 61 | { 62 | temp = a[j]; 63 | a[j] = a[j + 1]; 64 | a[j + 1] = temp; 65 | } 66 | } 67 | } 68 | 69 | printf("\nEnter the element to search:"); 70 | scanf("%d",&x); 71 | int flag=0; 72 | int first=0; 73 | int last=n-1; 74 | int mid; 75 | while(first<=last) 76 | { 77 | mid=(first+last)/2; 78 | 79 | if(x==a[mid]){ 80 | flag=1; 81 | break; 82 | } 83 | else 84 | if(x>a[mid]) 85 | first=mid+1; 86 | else 87 | last=mid-1; 88 | } 89 | 90 | if(flag==1) 91 | printf("\nElement found at position %d",mid+1); 92 | else 93 | printf("\nElement not found"); 94 | break; 95 | 96 | case 4:for(i=0;i<=fai;i++){ 97 | printf("%d",a[i]); 98 | } 99 | break; 100 | case 5:for (i = 0; i < fai; i++) 101 | { 102 | for (int j = 0; j < (fai - i - 1); j++) 103 | { 104 | if (a[j] > a[j + 1]) 105 | { 106 | temp = a[j]; 107 | a[j] = a[j + 1]; 108 | a[j + 1] = temp; 109 | } 110 | } 111 | } 112 | break; 113 | 114 | 115 | } 116 | 117 | }while (n!= 6); 118 | return 0; 119 | } -------------------------------------------------------------------------------- /sorting/Bucket Sort.cpp: -------------------------------------------------------------------------------- 1 | // Bucket sort in C++ 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define NARRAY 7 // Array size 8 | #define NBUCKET 6 // Number of buckets 9 | #define INTERVAL 10 // Each bucket capacity 10 | 11 | struct Node { 12 | int data; 13 | struct Node *next; 14 | }; 15 | 16 | void BucketSort(int arr[]); 17 | struct Node *InsertionSort(struct Node *list); 18 | void print(int arr[]); 19 | void printBuckets(struct Node *list); 20 | int getBucketIndex(int value); 21 | 22 | // Sorting function 23 | void BucketSort(int arr[]) { 24 | int i, j; 25 | struct Node **buckets; 26 | 27 | // Create buckets and allocate memory size 28 | buckets = (struct Node **)malloc(sizeof(struct Node *) * NBUCKET); 29 | 30 | // Initialize empty buckets 31 | for (i = 0; i < NBUCKET; ++i) { 32 | buckets[i] = NULL; 33 | } 34 | 35 | // Fill the buckets with respective elements 36 | for (i = 0; i < NARRAY; ++i) { 37 | struct Node *current; 38 | int pos = getBucketIndex(arr[i]); 39 | current = (struct Node *)malloc(sizeof(struct Node)); 40 | current->data = arr[i]; 41 | current->next = buckets[pos]; 42 | buckets[pos] = current; 43 | } 44 | 45 | // Print the buckets along with their elements 46 | for (i = 0; i < NBUCKET; i++) { 47 | cout << "Bucket[" << i << "] : "; 48 | printBuckets(buckets[i]); 49 | cout << endl; 50 | } 51 | 52 | // Sort the elements of each bucket 53 | for (i = 0; i < NBUCKET; ++i) { 54 | buckets[i] = InsertionSort(buckets[i]); 55 | } 56 | 57 | cout << "-------------" << endl; 58 | cout << "Bucktets after sorted" << endl; 59 | for (i = 0; i < NBUCKET; i++) { 60 | cout << "Bucket[" << i << "] : "; 61 | printBuckets(buckets[i]); 62 | cout << endl; 63 | } 64 | 65 | // Put sorted elements on arr 66 | for (j = 0, i = 0; i < NBUCKET; ++i) { 67 | struct Node *node; 68 | node = buckets[i]; 69 | while (node) { 70 | arr[j++] = node->data; 71 | node = node->next; 72 | } 73 | } 74 | 75 | for (i = 0; i < NBUCKET; ++i) { 76 | struct Node *node; 77 | node = buckets[i]; 78 | while (node) { 79 | struct Node *tmp; 80 | tmp = node; 81 | node = node->next; 82 | free(tmp); 83 | } 84 | } 85 | free(buckets); 86 | return; 87 | } 88 | 89 | // Function to sort the elements of each bucket 90 | struct Node *InsertionSort(struct Node *list) { 91 | struct Node *k, *nodeList; 92 | if (list == 0 || list->next == 0) { 93 | return list; 94 | } 95 | 96 | nodeList = list; 97 | k = list->next; 98 | nodeList->next = 0; 99 | while (k != 0) { 100 | struct Node *ptr; 101 | if (nodeList->data > k->data) { 102 | struct Node *tmp; 103 | tmp = k; 104 | k = k->next; 105 | tmp->next = nodeList; 106 | nodeList = tmp; 107 | continue; 108 | } 109 | 110 | for (ptr = nodeList; ptr->next != 0; ptr = ptr->next) { 111 | if (ptr->next->data > k->data) 112 | break; 113 | } 114 | 115 | if (ptr->next != 0) { 116 | struct Node *tmp; 117 | tmp = k; 118 | k = k->next; 119 | tmp->next = ptr->next; 120 | ptr->next = tmp; 121 | continue; 122 | } else { 123 | ptr->next = k; 124 | k = k->next; 125 | ptr->next->next = 0; 126 | continue; 127 | } 128 | } 129 | return nodeList; 130 | } 131 | 132 | int getBucketIndex(int value) { 133 | return value / INTERVAL; 134 | } 135 | 136 | // Print buckets 137 | void print(int ar[]) { 138 | int i; 139 | for (i = 0; i < NARRAY; ++i) { 140 | cout << setw(3) << ar[i]; 141 | } 142 | cout << endl; 143 | } 144 | 145 | void printBuckets(struct Node *list) { 146 | struct Node *cur = list; 147 | while (cur) { 148 | cout << setw(3) << cur->data; 149 | cur = cur->next; 150 | } 151 | } 152 | 153 | // Driver code 154 | int main(void) { 155 | int array[NARRAY] = {42, 32, 33, 52, 37, 47, 51}; 156 | 157 | cout << "Initial array: " << endl; 158 | print(array); 159 | cout << "-------------" << endl; 160 | 161 | BucketSort(array); 162 | cout << "-------------" << endl; 163 | cout << "Sorted array: " << endl; 164 | print(array); 165 | } 166 | -------------------------------------------------------------------------------- /Algorithms/HEXADECIMAL_TO_OCTAL.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main() 5 | { 6 | int i=0, chk=0, len, rem, binDigit, octalDigit; 7 | long long binaryInt=0, temp=1, octalNum, binaryNum; 8 | char hexdec[11], binnum[40]=""; 9 | printf("Enter Hexadecimal Number: "); 10 | gets(hexdec); 11 | while(hexdec[i]) 12 | { 13 | switch(hexdec[i]) 14 | { 15 | case '0': 16 | strcat(binnum, "0000"); 17 | break; 18 | case '1': 19 | strcat(binnum, "0001"); 20 | break; 21 | case '2': 22 | strcat(binnum, "0010"); 23 | break; 24 | case '3': 25 | strcat(binnum, "0011"); 26 | break; 27 | case '4': 28 | strcat(binnum, "0100"); 29 | break; 30 | case '5': 31 | strcat(binnum, "0101"); 32 | break; 33 | case '6': 34 | strcat(binnum, "0110"); 35 | break; 36 | case '7': 37 | strcat(binnum, "0111"); 38 | break; 39 | case '8': 40 | strcat(binnum, "1000"); 41 | break; 42 | case '9': 43 | strcat(binnum, "1001"); 44 | break; 45 | case 'A': 46 | strcat(binnum, "1010"); 47 | break; 48 | case 'a': 49 | strcat(binnum, "1010"); 50 | break; 51 | case 'B': 52 | strcat(binnum, "1011"); 53 | break; 54 | case 'b': 55 | strcat(binnum, "1011"); 56 | break; 57 | case 'C': 58 | strcat(binnum, "1100"); 59 | break; 60 | case 'c': 61 | strcat(binnum, "1100"); 62 | break; 63 | case 'D': 64 | strcat(binnum, "1101"); 65 | break; 66 | case 'd': 67 | strcat(binnum, "1101"); 68 | break; 69 | case 'E': 70 | strcat(binnum, "1110"); 71 | break; 72 | case 'e': 73 | strcat(binnum, "1110"); 74 | break; 75 | case 'F': 76 | strcat(binnum, "1111"); 77 | break; 78 | case 'f': 79 | strcat(binnum, "1111"); 80 | break; 81 | default: 82 | chk = 1; 83 | break; 84 | } 85 | i++; 86 | } 87 | if(chk==0) 88 | { 89 | len = strlen(binnum); 90 | while(len!=0) 91 | { 92 | if(binnum[len-1]=='0') 93 | binDigit=0; 94 | else 95 | binDigit=1; 96 | binaryInt = binaryInt + (binDigit*temp); 97 | temp = temp*10; 98 | len--; 99 | } 100 | binaryNum = binaryInt; 101 | octalNum = 0; 102 | temp = 1; 103 | while(binaryNum>0) 104 | { 105 | rem = binaryNum%1000; 106 | switch(rem) 107 | { 108 | case 0: 109 | octalDigit = 0; 110 | break; 111 | case 1: 112 | octalDigit = 1; 113 | break; 114 | case 10: 115 | octalDigit = 2; 116 | break; 117 | case 11: 118 | octalDigit = 3; 119 | break; 120 | case 100: 121 | octalDigit = 4; 122 | break; 123 | case 101: 124 | octalDigit = 5; 125 | break; 126 | case 110: 127 | octalDigit = 6; 128 | break; 129 | case 111: 130 | octalDigit = 7; 131 | break; 132 | } 133 | octalNum = (octalDigit*temp) + octalNum; 134 | binaryNum = binaryNum/1000; 135 | temp = temp*10; 136 | } 137 | printf("\nEquivalent Octal Value = %lld", octalNum); 138 | } 139 | else 140 | printf("\nInvalid Hexadecimal Character/Digit"); 141 | getch(); 142 | return 0; 143 | } 144 | -------------------------------------------------------------------------------- /data-structure/graph/Tree_Traversal.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node 4 | { 5 | int data; 6 | struct node *left; 7 | struct node *right; 8 | }; 9 | struct node *tree=NULL; 10 | void create_tree(struct node *); 11 | struct node *insertElement(struct node *, int); 12 | void preorderTraversal(struct node *); 13 | void inorderTraversal(struct node *); 14 | void postorderTraversal(struct node *); 15 | struct node *deleteElement(struct node *, int); 16 | int main() 17 | { 18 | int option, val; 19 | struct node *ptr; 20 | do 21 | { 22 | printf("\n _________________MAIN MENU_____________________________ "); 23 | printf("\n _______________________________________________________ "); 24 | printf("\n 1. Insert Element"); 25 | printf("\n 2. Preorder Traversal"); 26 | printf("\n 3. Inorder Traversal"); 27 | printf("\n 4. Postorder Traversal"); 28 | printf("\n 5. Delete an element"); 29 | printf("\n 6. Exit"); 30 | printf("\n\n Enter your option : "); 31 | scanf("%d", &option); 32 | switch(option) 33 | { 34 | case 1: 35 | printf("\n Enter the value of the new node : "); 36 | scanf("%d", &val); 37 | tree = insertElement(tree, val); 38 | break; 39 | case 2: 40 | printf("\n The elements of the tree are : \n"); 41 | preorderTraversal(tree); 42 | break; 43 | case 3: 44 | printf("\n The elements of the tree are : \n"); 45 | inorderTraversal(tree); 46 | break; 47 | case 4: 48 | printf("\n The elements of the tree are : \n"); 49 | postorderTraversal(tree); 50 | break; 51 | case 5: 52 | printf("\n Enter the element to be deleted : "); 53 | scanf("%d", &val); 54 | tree = deleteElement(tree, val); 55 | break; 56 | } 57 | } 58 | while(option!=6); 59 | return 0; 60 | } 61 | struct node *insertElement(struct node *tree, int val) 62 | { 63 | struct node *ptr, *nodeptr, *parentptr; 64 | ptr = (struct node*)malloc(sizeof(struct node)); 65 | ptr->data = val; 66 | ptr->left = NULL; 67 | ptr->right = NULL; 68 | if(tree==NULL) 69 | { 70 | tree=ptr; 71 | tree->left=NULL; 72 | tree->right=NULL; 73 | } 74 | else 75 | { 76 | parentptr=NULL; 77 | nodeptr=tree; 78 | while(nodeptr!=NULL) 79 | { 80 | parentptr=nodeptr; 81 | if(valdata) 82 | nodeptr=nodeptr->left; 83 | else 84 | nodeptr = nodeptr->right; 85 | } 86 | if(valdata) 87 | parentptr->left = ptr; 88 | else 89 | parentptr->right = ptr; 90 | } 91 | return tree; 92 | } 93 | void preorderTraversal(struct node *tree) 94 | { 95 | if(tree != NULL) 96 | { 97 | printf("%d\t", tree->data); 98 | preorderTraversal(tree->left); 99 | preorderTraversal(tree->right); 100 | } 101 | 102 | } 103 | void inorderTraversal(struct node *tree) 104 | { 105 | if(tree != NULL) 106 | { 107 | inorderTraversal(tree->left); 108 | printf("%d\t", tree->data); 109 | inorderTraversal(tree->right); 110 | } 111 | 112 | } 113 | void postorderTraversal(struct node *tree) 114 | { 115 | if(tree != NULL) 116 | { 117 | postorderTraversal(tree->left); 118 | postorderTraversal(tree->right); 119 | printf("%d\t", tree->data); 120 | } 121 | 122 | } 123 | struct node *deleteElement(struct node *tree, int val) 124 | { 125 | struct node *cur, *parent, *suc, *psuc, *ptr; 126 | if(tree->left==NULL) 127 | { 128 | printf("\n The tree is empty "); 129 | return(tree); 130 | } 131 | parent = tree; 132 | cur = tree->left; 133 | while(cur!=NULL && val!= cur->data) 134 | { 135 | parent = cur; 136 | cur = (valdata)? cur->left:cur->right; 137 | } 138 | if(cur == NULL) 139 | { 140 | printf("\n The value to be deleted is not present in the tree"); 141 | return(tree); 142 | } 143 | if(cur->left == NULL) 144 | ptr = cur->right; 145 | else if(cur->right == NULL) 146 | ptr = cur->left; 147 | else 148 | { 149 | psuc = cur; 150 | cur = cur->left; 151 | while(suc->left!=NULL) 152 | { 153 | psuc = suc; 154 | suc = suc->left; 155 | } 156 | if(cur==psuc) 157 | { 158 | suc->left = cur->right; 159 | } 160 | else 161 | { 162 | suc->left = cur->left; 163 | psuc->left = suc->right; 164 | suc->right = cur->right; 165 | } 166 | ptr = suc; 167 | } 168 | if(parent->left == cur) 169 | parent->left=ptr; 170 | else 171 | parent->right=ptr; 172 | free(cur); 173 | return tree; 174 | } -------------------------------------------------------------------------------- /Algorithms/matrix/matrix-calculator.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void read_and_display() 4 | { 5 | int i,j,row,col,p; 6 | printf("input the number of rows : "); 7 | scanf("%d",&row); 8 | printf("input the number of columns : "); 9 | scanf("%d",&col); 10 | p=col*row; 11 | int matrix[row][col]; 12 | printf("input the %d elements of matrix row wise : ",p); 13 | for(i=0;i