├── Y.c ├── carray.c ├── cstrings.c ├── Second.c ├── ccomment.c ├── sum ├── cforloop.c ├── elseifexample.c ├── shorthandif.c ├── cwhileloop.c ├── fisrstex.c ├── hello.c ├── memoryaDDRESS.C ├── memoryaddress.c ├── cbreakandcont.c ├── sum.c ├── multiple line comment.c ├── cprem.c ├── cstring.c ├── carrayexa.c ├── helloworld.c ├── cpointer.c ├── forloopex.c ├── whileloop.c ├── README.md ├── cfunctiondel.c ├── To_find_keywords.txt ├── XOR Operation.c ├── bitwise_operations ├── swap_numbers.c ├── get_bit.c └── bit_manipulation.c ├── crecurseion.c ├── pointer_1.c ├── recursionfun.c ├── pointers_practice_1.c ├── printinput.c ├── cswitch.c ├── sumloop.cpp ├── Add2Integers.c ├── Pattern.c ├── even odd.c ├── fibonacci_dp.c ├── TypeCasting.c ├── twointeger.c ├── FibonacciRecursion.c ├── floating.c ├── Swap_Pointer.c ├── Addition of n numbers ├── pyramid.c ├── Armstrong.c ├── ArmstrongNumber.c ├── C Program Display Its Code as Output ├── happy_number.c ├── simpleexample.c ├── userinput.c ├── getPIDs.c ├── GCD_recursive.c ├── GCD_iterative.c ├── magicNumber.c ├── pointers_prac_2.c ├── size.c ├── number triangle .c ├── pointers_prac_3.c ├── vowelandcons.c ├── compute.c ├── sum_of_diagonal_in_array.c ├── cstructure.c ├── count_number_of_words.c ├── Pascal Triangle Pattern.c ├── PrimeOrNot.c ├── SelectionSort.c ├── ascii.c ├── SpecialNumber.c ├── keyworddemo.c ├── file.c ├── matrix multiplication.c ├── poly.c ├── reversestring.c ├── SimpleInterest.c ├── decimal_to_binary.c ├── Check_array_sorted.cpp ├── adc.c ├── C Program to Demonstrate the Working of Keyword long.c ├── emi.c ├── ArrayInputAndPrint.c ├── Reverse ├── occurance_of_string.c ├── fibonacci.c ├── Alphabet triangle .C ├── uppercase.c ├── Fibonacci.c ├── amstrongnumber.c ├── swag.c ├── sd.c ├── Standard Deviation.c ├── compare.c ├── twisted prime.c ├── .gitignore ├── pointer.c ├── Binarysearch.c ├── compoundintrest.c ├── largestelement.c ├── primenumberbettwo.c ├── duck no. .c ├── duck_number.c ├── recursive.c ├── sort.c ├── piglatin in c.c ├── Calculator.c ├── most_fre_array.c ├── abundant_number.c ├── bubblesort.c ├── Recursive_Bubble_Sort.c ├── insertion_sort.c ├── disarium_number.c ├── Display prime numbers between two intervals.c ├── CONTRIBUTING.md ├── addition_of_matrices.c ├── password_generator.c ├── switchcase.c ├── Binary_Search.c ├── kaprekar_number.c ├── binarySearch.cpp ├── quardatic.c ├── 2DArray_Transpose.c ├── Matmul.c ├── Linear_Search.c ├── counting_sort.c ├── primepalin.c ├── LICENSE ├── Multiplication_of_Matrices.c ├── lexicographic_sort.c ├── polynomial_eval.c ├── XOR_Decryptor.c ├── TO_find_keywords.c ├── To_find_keywords.c ├── Binary Addition and Binary Subtraction.c ├── comm.c ├── Guess_a_Number.c ├── intersection_of_sets.c ├── Quick Sort Problem ├── binary_exponentiation.c ├── String_To_Integer.cpp ├── Merge Sort Program in C ├── Radix_Sort.c ├── XOR_Encryptor.c ├── GaussianElimination.c ├── Integer_To_Roman.cpp ├── sparse_representation.c ├── heap_sort.c ├── find_in_doublyll.c ├── Reverse_Linkedlist.c ├── mean_median_mode.c ├── Matrix_Determinant.c ├── fibonacci_and_factotial_with_recursion.c ├── Selection_Sort.c ├── get_size_doubly_linkedlist.c ├── 2darray.c ├── circular_queue.c ├── Roman_To_Integer.cpp ├── Filling_Cans_Problem.c ├── polynomial_addition.c ├── Program for validating username and password in C.c ├── Tree Traversal.c ├── Cyclic_Sort.c ├── Weekday of given date.c ├── product_of_2_matrices.c ├── Infix_to_postfix.c ├── doubly linked list.c ├── queue.c ├── traversals_in_binarytree.c ├── delete_in_doubly_linkedlist.c ├── insert_in_doubly_linkedlist.c ├── Game.c ├── priority_queue_adt_with_example.c ├── stack.c ├── klee-algo.c ├── BST_deletion.c ├── Tower_of_Hanoi.c ├── DynamicArray.c ├── DoubleLinkedList.c ├── DQueueLinkedList.c ├── DoublyLinkedList.c ├── d_linked_list.c ├── hashtable.c ├── roots.c └── sliding_window_maximum_using_deque /Y.c: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /carray.c: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /cstrings.c: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Second.c: -------------------------------------------------------------------------------- 1 | c orogram in c 2 | -------------------------------------------------------------------------------- /ccomment.c: -------------------------------------------------------------------------------- 1 | // This is a comment 2 | printf("Hello World!"); -------------------------------------------------------------------------------- /sum: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suman-shah/c-program-example/HEAD/sum -------------------------------------------------------------------------------- /cforloop.c: -------------------------------------------------------------------------------- 1 | for (i = 0; i <= 10; i = i + 2) { 2 | printf("%d\n", i); 3 | } -------------------------------------------------------------------------------- /elseifexample.c: -------------------------------------------------------------------------------- 1 | if (20 > 18) { 2 | printf("20 is greater than 18"); 3 | } -------------------------------------------------------------------------------- /shorthandif.c: -------------------------------------------------------------------------------- 1 | int time = 20; 2 | (time < 18) ? printf("Good day.") : printf("Good evening."); -------------------------------------------------------------------------------- /cwhileloop.c: -------------------------------------------------------------------------------- 1 | int i = 0; 2 | 3 | do { 4 | printf("%d\n", i); 5 | i++; 6 | } 7 | while (i < 5); -------------------------------------------------------------------------------- /fisrstex.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | printf("Hello World!"); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /hello.c: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | cout << "Hello World!"; 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /memoryaDDRESS.C: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int myAge = 43; 5 | printf("%p", &myAge); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /memoryaddress.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int myAge = 43; 5 | printf("%p", &myAge); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /cbreakandcont.c: -------------------------------------------------------------------------------- 1 | int i; 2 | 3 | for (i = 0; i < 10; i++) { 4 | if (i == 4) { 5 | break; 6 | } 7 | printf("%d\n", i); 8 | } -------------------------------------------------------------------------------- /sum.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int a,b; 5 | scanf("%d %d",&a,&b); 6 | printf("%d\n",a+b); 7 | return 0; 8 | } -------------------------------------------------------------------------------- /multiple line comment.c: -------------------------------------------------------------------------------- 1 | /* The code below will print the words Hello World! 2 | to the screen, and it is amazing */ 3 | printf("Hello World!"); -------------------------------------------------------------------------------- /cprem.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | /* my first program in C */ 5 | printf("Hello, World! \n"); 6 | 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /cstring.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | char greetings[] = "Hello World!"; 5 | printf("%c", greetings[0]); 6 | 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /carrayexa.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int myNumbers[] = {25, 50, 75, 100}; 5 | printf("%d", myNumbers[0]); 6 | 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /helloworld.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | // printf() displays the string inside quotation 4 | printf("Hello, World!"); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /cpointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int myAge = 43; 5 | 6 | printf("%d\n", myAge); 7 | printf("%p\n", &myAge); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /forloopex.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int i; 5 | 6 | for (i = 0; i <= 10; i = i + 2) { 7 | printf("%d\n", i); 8 | } 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /whileloop.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int i = 0; 5 | 6 | do { 7 | printf("%d\n", i); 8 | i++; 9 | } 10 | while (i < 5); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # c-program-example 2 | C is a general-purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, with a static ... Write a c programa nd create a file 3 | -------------------------------------------------------------------------------- /cfunctiondel.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Create a function 4 | void myFunction() { 5 | printf("I just got executed!"); 6 | } 7 | 8 | int main() { 9 | myFunction(); // call the function 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /To_find_keywords.txt: -------------------------------------------------------------------------------- 1 | extern,return,union,const,float, short, auto, double, int, struct, break, else, long, goto, sizeof, voltile, do ,if, static, while, unsigned, continue, for, signed, void, default, switch, case, enum, register, typedef, char 2 | -------------------------------------------------------------------------------- /XOR Operation.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int x = 10; 6 | int y = 10; 7 | if ( !(x ^ y) ) 8 | printf(" x is equal to y "); 9 | else 10 | printf(" x is not equal to y "); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /bitwise_operations/swap_numbers.c: -------------------------------------------------------------------------------- 1 | // this program to swap numbers using bitwise operators 2 | 3 | #include 4 | 5 | int main(){ 6 | int a = 1; 7 | int b = 3; 8 | 9 | a = a^b; 10 | b = a^b; 11 | a = a^b; 12 | printf("\na = %d\nb = %d\n",a,b); 13 | } -------------------------------------------------------------------------------- /crecurseion.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int sum(int k); 4 | 5 | int main() { 6 | int result = sum(10); 7 | printf("%d", result); 8 | return 0; 9 | } 10 | 11 | int sum(int k) { 12 | if (k > 0) { 13 | return k + sum(k - 1); 14 | } else { 15 | return 0; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /pointer_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a=5; 5 | int *j,**k; 6 | j=&a; 7 | k=&j; 8 | printf("%d\n",a); 9 | printf("%u\n",j); 10 | printf("%u\n",&j); 11 | printf("%d\n",*&j); 12 | printf("%d\n",k); 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /recursionfun.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int sum(int k); 4 | 5 | int main() { 6 | int result = sum(10); 7 | printf("%d", result); 8 | return 0; 9 | } 10 | 11 | int sum(int k) { 12 | if (k > 0) { 13 | return k + sum(k - 1); 14 | } else { 15 | return 0; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /pointers_practice_1.c: -------------------------------------------------------------------------------- 1 | // write a program to print the address of a variable. use this address to get the value of this variable 2 | #include 3 | 4 | int main(){ 5 | int a; 6 | printf("the address of a is%u\n",&a); 7 | scanf("%d",&a); 8 | printf("%d\n",a); 9 | return 0; 10 | } -------------------------------------------------------------------------------- /printinput.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int number; 4 | 5 | printf("Enter an integer: "); 6 | 7 | // reads and stores input 8 | scanf("%d", &number); 9 | 10 | // displays output 11 | printf("You entered: %d", number); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /cswitch.c: -------------------------------------------------------------------------------- 1 | int day = 4; 2 | 3 | switch (day) { 4 | case 6: 5 | printf("Today is Saturday"); 6 | break; 7 | case 7: 8 | printf("Today is Sunday"); 9 | break; 10 | default: 11 | printf("Looking forward to the Weekend"); 12 | } 13 | 14 | // Outputs "Looking forward to the Weekend" -------------------------------------------------------------------------------- /sumloop.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int n, sum = 0; 6 | 7 | cout << "Enter a positive integer: "; 8 | cin >> n; 9 | 10 | for (int i = 1; i <= n; ++i) { 11 | sum += i; 12 | } 13 | 14 | cout << "Sum = " << sum; 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /Add2Integers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int num1,num2,sum = 0; 6 | 7 | printf("Enter the 2 integers of your choice : \n"); 8 | 9 | scanf("%d%d",&num1,&num2); 10 | 11 | sum = num1+num2; 12 | 13 | printf("The sum of the 2 integers is : %d",sum); 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Pattern.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n,i,j; 5 | printf("Enter number of lines "); 6 | scanf("%d",&n); 7 | for(i=1;i<=n;i++) 8 | { 9 | for(j=0;j<2*(n-i);j++) 10 | printf(" "); 11 | for(j=0;j<2*i-1;j++) 12 | printf("* "); 13 | printf("\n"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /even odd.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int num; 4 | printf("Enter an integer: "); 5 | scanf("%d", &num); 6 | 7 | // true if num is perfectly divisible by 2 8 | if(num % 2 == 0) 9 | printf("%d is even.", num); 10 | else 11 | printf("%d is odd.", num); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /fibonacci_dp.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // 1D DP example 4 | 5 | int main(){ 6 | int n = 4; 7 | 8 | int fib_dp[n+1]; 9 | fib_dp[0] = fib_dp[1] = 1; 10 | 11 | for (int i = 2; i<=n; i++){ 12 | fib_dp[i] = fib_dp[i-1] + fib_dp[i-2]; 13 | } 14 | 15 | printf("%d\n", fib_dp[n]); 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /twointeger.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | 4 | int number1, number2, sum; 5 | 6 | printf("Enter two integers: "); 7 | scanf("%d %d", &number1, &number2); 8 | 9 | // calculating sum 10 | sum = number1 + number2; 11 | 12 | printf("%d + %d = %d", number1, number2, sum); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /FibonacciRecursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | void fibo(int a,int b,int n); 3 | void fibo(int a,int b,int n){ 4 | if(n>0){ 5 | int c=a+b; 6 | printf("%d\n",c); 7 | fibo(b,c,n-1); 8 | } 9 | } 10 | int main(){ 11 | int n,a=-1,b=1; 12 | printf("ENTER NUMBER OF TERMS\n"); 13 | scanf("%d",&n); 14 | fibo(a,b,n); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /floating.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | double a, b, product; 4 | printf("Enter two numbers: "); 5 | scanf("%lf %lf", &a, &b); 6 | 7 | // Calculating product 8 | product = a * b; 9 | 10 | // %.2lf displays number up to 2 decimal point 11 | printf("Product = %.2lf", product); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Swap_Pointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int x,y,t; 5 | int * a; 6 | int * b; 7 | printf("Enter first number\n"); 8 | scanf("%d",&x); 9 | printf("Enter second number\n"); 10 | scanf("%d",&y); 11 | a=&x; 12 | b=&y; 13 | t=*a; 14 | *a=*b; 15 | *b=t; 16 | printf("%d",*a); 17 | printf("%d",*b); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Addition of n numbers: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int n; 5 | int sum=0; 6 | printf("Enter the number of numbers you need to add:\n"); 7 | scanf("%d",&n); 8 | int arr[n]; 9 | printf("Enter the elements one by one:\n"); 10 | for(int i=0;i 2 | 3 | int main(){ 4 | for(int i=0;i<5;i++) 5 | { 6 | for(int j=0;j<=9;j++) 7 | { 8 | if(j>=5-i&&j<=5+i) 9 | 10 | printf("*"); 11 | 12 | else 13 | printf(" "); 14 | 15 | } 16 | printf("\n"); 17 | } 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /ArmstrongNumber.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 | -------------------------------------------------------------------------------- /C Program Display Its Code as Output: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | FILE *fp; 4 | int c; 5 | 6 | // open the current input file 7 | fp = fopen(__FILE__,"r"); 8 | 9 | do { 10 | c = getc(fp); // read character 11 | putchar(c); // display character 12 | } 13 | while(c != EOF); // loop until the end of file is reached 14 | 15 | fclose(fp); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /happy_number.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | main() 4 | { 5 | int i,j,num,temp,sum=0; 6 | printf("Enter number\n"); 7 | scanf("%d",&num); 8 | while(sum!=1 && sum!=4) 9 | { 10 | sum=0; 11 | while(num>0) 12 | { 13 | j=num%10; 14 | sum+=(j*j); 15 | num=num/10; 16 | } 17 | num=sum; 18 | } 19 | 20 | if(sum==1) 21 | printf("Happy Number\n"); 22 | else 23 | printf("UnHappy Number\n"); 24 | } 25 | -------------------------------------------------------------------------------- /simpleexample.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define PI 3.141592 5 | 6 | int main() 7 | { 8 | float radius, area, circum; 9 | clrscr(); // Clears the screen 10 | printf("Enter radius of circle: "); 11 | scanf("%f", &radius); 12 | area = PI*radius*radius; 13 | circum = 2*PI*r; 14 | printf("Area = %f\n",area); 15 | printf("Circumference = %f",circum); 16 | getch(); // Holds the output 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /userinput.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // Create an integer variable that will store the number we get from the user 5 | int myNum; 6 | 7 | // Ask the user to type a number 8 | printf("Type a number and press enter: \n"); 9 | 10 | // Get and save the number the user types 11 | scanf("%d", &myNum); 12 | 13 | // Print the number the user typed 14 | printf("Your number is: %d", myNum); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /getPIDs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void main(){ 6 | pid_t pid; //variable to store process id 7 | pid_t ppid; //variable to store parent process id 8 | 9 | sleep(5); 10 | 11 | pid = getpid(); // returns pid 12 | ppid = getppid(); //returns parent pid 13 | 14 | printf("The process id is : %d\n" , pid); 15 | printf("The parent of process id is : %d\n" , ppid); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /GCD_recursive.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int gcd(int a, int b) 4 | { 5 | if (a < b) 6 | { 7 | int temp = a; 8 | a = b; 9 | b = temp; 10 | } 11 | if (b == 0) 12 | return a; 13 | return gcd(b, a % b); 14 | } 15 | 16 | int main(void) 17 | { 18 | int a, b; 19 | printf("Enter two numbers : "); 20 | scanf("%d %d", &a, &b); 21 | printf("GCD of %d and %d is %d", a, b, gcd(a, b)); 22 | return 0; 23 | } -------------------------------------------------------------------------------- /GCD_iterative.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int gcd(int a, int b) 4 | { 5 | int min = (a > b) ? b : a; 6 | while (min > 0) 7 | { 8 | if (a % min == 0 && b % min == 0) 9 | break; 10 | --min; 11 | } 12 | return min; 13 | } 14 | 15 | int main(void) 16 | { 17 | int a, b; 18 | printf("Enter two numbers : "); 19 | scanf("%d %d", &a, &b); 20 | printf("GCD of %d and %d is %d", a, b, gcd(a, b)); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /magicNumber.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int m, n,s; 5 | printf("Enter Number:"); 6 | scanf("%d",&m); 7 | n=m; 8 | while(n>9) 9 | { 10 | s=0; 11 | while(n>0) 12 | { 13 | int d=n%10; 14 | s=s+d; 15 | n=n/10; 16 | } 17 | n=s; 18 | } 19 | if(n==1) 20 | printf("%d is a Magic Number.",m); 21 | else 22 | printf("%d is not a Magic Number.",m); 23 | } 24 | -------------------------------------------------------------------------------- /pointers_prac_2.c: -------------------------------------------------------------------------------- 1 | // write a program having a variable i , print the address of i .pass this variable 2 | // to a function and print its address. Are these addresses same? why? 3 | #include 4 | void addrs(int); 5 | int main(){ 6 | int i; 7 | printf("address is %u\n",&i); 8 | // c=addrs(i); 9 | // printf("the address for fn :%u"); 10 | addrs(i); 11 | return 0; 12 | } 13 | 14 | void addrs(int a) 15 | { 16 | printf("the aredd in fn is %u",&a); 17 | } -------------------------------------------------------------------------------- /size.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int intType; 4 | float floatType; 5 | double doubleType; 6 | char charType; 7 | 8 | // sizeof evaluates the size of a variable 9 | printf("Size of int: %zu bytes\n", sizeof(intType)); 10 | printf("Size of float: %zu bytes\n", sizeof(floatType)); 11 | printf("Size of double: %zu bytes\n", sizeof(doubleType)); 12 | printf("Size of char: %zu byte\n", sizeof(charType)); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /number triangle .c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(){ 4 | int i,j,k,l,n; 5 | system("cls"); 6 | printf("enter the range="); 7 | scanf("%d",&n); 8 | for(i=1;i<=n;i++) 9 | { 10 | for(j=1;j<=n-i;j++) 11 | { 12 | printf(" "); 13 | } 14 | for(k=1;k<=i;k++) 15 | { 16 | printf("%d",k); 17 | } 18 | for(l=i-1;l>=1;l--) 19 | { 20 | printf("%d",l); 21 | } 22 | printf("\n"); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /pointers_prac_3.c: -------------------------------------------------------------------------------- 1 | // write a program to change the value of a variable 2 | //to 10 times ,use call by ref. 3 | #include 4 | int ptr(int*); 5 | int main(){ 6 | int a=6; 7 | 8 | printf("enter a number: \n"); 9 | scanf("%d",&a); 10 | a=ptr(&a); 11 | printf("the new value is %d",a); 12 | return 0; 13 | } 14 | int ptr(int *i) 15 | { 16 | int p=*i*10; 17 | return(p); // this concept is helpful also if we want to return more then one things 18 | 19 | 20 | } -------------------------------------------------------------------------------- /vowelandcons.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | char ch; 5 | bool isVowel = false; 6 | 7 | printf("Enter an alphabet: "); 8 | scanf("%c",&ch); 9 | 10 | if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I' 11 | ||ch=='o'||ch=='O'||ch=='u'||ch=='U') 12 | { 13 | isVowel = true; 14 | 15 | } 16 | if (isVowel == true) 17 | printf("%c is a Vowel", ch); 18 | else 19 | printf("%c is a Consonant", ch); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /compute.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int dividend, divisor, quotient, remainder; 4 | printf("Enter dividend: "); 5 | scanf("%d", ÷nd); 6 | printf("Enter divisor: "); 7 | scanf("%d", &divisor); 8 | 9 | // Computes quotient 10 | quotient = dividend / divisor; 11 | 12 | // Computes remainder 13 | remainder = dividend % divisor; 14 | 15 | printf("Quotient = %d\n", quotient); 16 | printf("Remainder = %d", remainder); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /sum_of_diagonal_in_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int i,j,n; 4 | float a[10][10], sum=0.0; 5 | 6 | printf("Enter order of matrix:\n"); 7 | scanf("%d", &n); 8 | printf("Enter matrix elements:\n"); 9 | for(i=0;i< n;i++) { 10 | for(j=0;j< n;j++) { 11 | scanf("%f", &a[i][j]); 12 | } } 13 | 14 | for(i=0;i< n;i++) { 15 | for(j=0;j< n;j++) { 16 | if(i==j || i+j==n-1) { 17 | sum = sum + a[i][j]; 18 | } } } 19 | printf("Sum = %f\n", sum); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /cstructure.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Create a structure called myStructure 4 | struct myStructure { 5 | int myNum; 6 | char myLetter; 7 | }; 8 | 9 | int main() { 10 | // Create a structure variable of myStructure called s1 11 | struct myStructure s1; 12 | 13 | // Assign values to members of s1 14 | s1.myNum = 13; 15 | s1.myLetter = 'B'; 16 | 17 | // Print values 18 | printf("My number: %d\n", s1.myNum); 19 | printf("My letter: %c\n", s1.myLetter); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /count_number_of_words.c: -------------------------------------------------------------------------------- 1 | /* 2 | * C Program to Count Number of Words in a given Text Or Sentence 3 | */ 4 | #include 5 | #include 6 | 7 | void main() 8 | { 9 | char s[200]; 10 | int count = 0, i; 11 | 12 | printf("Enter the string:\n"); 13 | scanf("%[^\n]s", s); 14 | for (i = 0;s[i] != '\0';i++) 15 | { 16 | if (s[i] == ' ' && s[i+1] != ' ') 17 | count++; 18 | } 19 | printf("Number of words in given string are: %d\n", count + 1); 20 | } 21 | -------------------------------------------------------------------------------- /Pascal Triangle Pattern.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int i, j, s, n, c; 4 | printf("Enter the number of rows"); 5 | scanf("%d", &n); 6 | for(i=0; i 2 | int main() { 3 | int n, i, flag = 1; 4 | printf("Enter a positive number: "); 5 | scanf("%d", &n); 6 | 7 | for (i = 2; i <= n / 2; ++i) { 8 | if (n % i == 0) { 9 | flag++; 10 | } 11 | } 12 | 13 | /* if (n == 1) { 14 | printf("1 is neither prime nor composite."); 15 | } 16 | */ 17 | 18 | if (flag > 1) 19 | printf("%d is not a prime number.", n); 20 | if(flag == 0) 21 | printf("%d is not a prime number.", n); 22 | } 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /SelectionSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main (){ 3 | int arr[7] = {1, 9, -6, -8, -9, 4, 4}; 4 | int largest, temp; 5 | 6 | for(int i = 6; i > 0; i--){ 7 | largest = 0; 8 | for(int j = 0; j<= i; j++){ 9 | if(arr[j] > arr[largest]){ 10 | largest = j; 11 | } 12 | } 13 | temp = arr[i]; 14 | arr[i] = arr[largest]; 15 | arr[largest] = temp; 16 | } 17 | 18 | //printing the sorted array now 19 | for(int i = 0; i < 7; i++){ 20 | printf("%d ", arr[i]); 21 | } 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /ascii.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | char ch; 5 | printf("Enter any character:"); 6 | 7 | /* Reads the entered character and stores it 8 | * into the char variable ch 9 | */ 10 | scanf("%c", &ch); 11 | 12 | /* Using the format specifiers we can get the ASCII code 13 | * of a character. When we use %d format specifier for a 14 | * char variable then it displays the ASCII value of a char 15 | */ 16 | printf("ASCII value of character %c is: %d", ch, ch); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /SpecialNumber.c: -------------------------------------------------------------------------------- 1 | #include 2 | int fact(int x){ 3 | if(x == 0 || x == 1){ 4 | return 1; 5 | 6 | } 7 | return x*(fact(x-1)); 8 | } 9 | int main(){ 10 | int x, num, digit, sum = 0; 11 | printf("Enter the number: "); 12 | scanf("%d", &x); 13 | num = x; 14 | while(num > 0){ 15 | digit = num%10; 16 | sum += fact(digit); 17 | num = num/10; 18 | } 19 | if(sum == x){ 20 | printf("%d is a Special number.", x); 21 | }else{ 22 | printf("%d is not a Special number.", x); 23 | } 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /keyworddemo.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int a; 4 | long b; // equivalent to long int b; 5 | long long c; // equivalent to long long int c; 6 | double e; 7 | long double f; 8 | 9 | printf("Size of int = %zu bytes \n", sizeof(a)); 10 | printf("Size of long int = %zu bytes\n", sizeof(b)); 11 | printf("Size of long long int = %zu bytes\n", sizeof(c)); 12 | printf("Size of double = %zu bytes\n", sizeof(e)); 13 | printf("Size of long double = %zu bytes\n", sizeof(f)); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /file.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | char sentence[1000]; 6 | 7 | // creating file pointer to work with files 8 | FILE *fptr; 9 | 10 | // opening file in writing mode 11 | fptr = fopen("program.txt", "w"); 12 | 13 | // exiting program 14 | if (fptr == NULL) { 15 | printf("Error!"); 16 | exit(1); 17 | } 18 | printf("Enter a sentence:\n"); 19 | fgets(sentence, sizeof(sentence), stdin); 20 | fprintf(fptr, "%s", sentence); 21 | fclose(fptr); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /matrix multiplication.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int arr[2][3], total[2]; 5 | for(int i = 0; i < 2; i++) { 6 | for(int j = 0; j < 3; j++){ 7 | scanf("%d", &arr[i][j]); 8 | } 9 | } 10 | for(int i = 0; i < 2; i++) { 11 | int sum = 0; 12 | for(int j = 0; j < 3; j++){ 13 | sum += arr[i][j]; 14 | } 15 | total[i] = sum; 16 | } 17 | for(int i = 0; i < 2; i++) 18 | printf("%d : %d\n", i + 1, total[i]); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /poly.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | // Base class 6 | class Animal { 7 | public: 8 | void animalSound() { 9 | cout << "The animal makes a sound \n" ; 10 | } 11 | }; 12 | 13 | // Derived class 14 | class Pig : public Animal { 15 | public: 16 | void animalSound() { 17 | cout << "The pig says: wee wee \n" ; 18 | } 19 | }; 20 | 21 | // Derived class 22 | class Dog : public Animal { 23 | public: 24 | void animalSound() { 25 | cout << "The dog says: bow wow \n" ; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /reversestring.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Reversing a given string with C Language 5 | 6 | int main() 7 | { 8 | char string[100], temp; 9 | 10 | printf("Enter any string: "); 11 | gets(string); 12 | 13 | int beg = 0, end = strlen(string) - 1; 14 | while(beg < end) 15 | { 16 | temp = string[beg]; 17 | string[beg] = string[end]; 18 | string[end] = temp; 19 | 20 | beg++; 21 | end--; 22 | } 23 | 24 | printf("The reversed string is: %s", string); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /SimpleInterest.c: -------------------------------------------------------------------------------- 1 | // 1. A program to calculate simple interest 2 | #include 3 | #include 4 | 5 | int main(){ 6 | int p ,r,t,si; 7 | char name[20]; 8 | printf("please enter your Name \n"); 9 | gets(name); 10 | printf("Hello Mr.%s please enter Principle , rate of interet and time:\n",name); 11 | scanf("%d%d%d",&p,&r,&t); 12 | 13 | si=p+((p*r*t)/100); 14 | 15 | printf("the Simple interest on %d amount for %d years As per %d rate is: %d",p,r,t,si); 16 | fflush(stdin); 17 | 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /decimal_to_binary.c: -------------------------------------------------------------------------------- 1 | // program to convert a decimal no. to its binary equivalent 2 | 3 | //hacktoberfest2021 4 | #include 5 | #include 6 | 7 | void main() 8 | { 9 | 10 | int n,rem,arr[20],i=0; 11 | 12 | printf("Enter a decimal number"); 13 | scanf("%d",&n); 14 | 15 | while(n>0){ 16 | 17 | rem=n%2; 18 | n=n/2; 19 | arr[i]=rem; 20 | 21 | i++; 22 | 23 | 24 | } 25 | for(int j=i-1;j>=0;j--) 26 | { 27 | printf("%d\t",arr[j]); 28 | } 29 | getch(); 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Check_array_sorted.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool check_sorted(int a[], int n) 5 | { 6 | for (int i = 1; i < n; i++) 7 | { 8 | if (a[i] > a[i - 1]) 9 | { 10 | continue; 11 | } 12 | else 13 | { 14 | return false; 15 | } 16 | } 17 | return true; 18 | } 19 | int main() 20 | { 21 | int n; 22 | cin >> n; 23 | int a[n]; 24 | for (int i = 0; i < n; i++) 25 | { 26 | cin >> a[i]; 27 | } 28 | cout << check_sorted(a, n); 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /adc.c: -------------------------------------------------------------------------------- 1 | // convert binary to decimal 2 | 3 | #include 4 | #include 5 | 6 | // function prototype 7 | int convert(long long); 8 | 9 | int main() { 10 | long long n; 11 | printf("Enter a binary number: "); 12 | scanf("%lld", &n); 13 | printf("%lld in binary = %d in decimal", n, convert(n)); 14 | return 0; 15 | } 16 | 17 | // function definition 18 | int convert(long long n) { 19 | int dec = 0, i = 0, rem; 20 | 21 | while (n!=0) { 22 | rem = n % 10; 23 | n /= 10; 24 | dec += rem * pow(2, i); 25 | ++i; 26 | } 27 | 28 | return dec; 29 | } 30 | -------------------------------------------------------------------------------- /C Program to Demonstrate the Working of Keyword long.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int a; 4 | long b; // equivalent to long int b; 5 | long long c; // equivalent to long long int c; 6 | double e; 7 | long double f; 8 | 9 | printf("Size of int = %zu bytes \n", sizeof(a)); 10 | printf("Size of long int = %zu bytes\n", sizeof(b)); 11 | printf("Size of long long int = %zu bytes\n", sizeof(c)); 12 | printf("Size of double = %zu bytes\n", sizeof(e)); 13 | printf("Size of long double = %zu bytes\n", sizeof(f)); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /emi.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main() 6 | { 7 | float p, R, r, emi; 8 | int n; 9 | 10 | /* Reading inputs */ 11 | printf("Enter principal amount: "); 12 | scanf("%f", &p); 13 | printf("Enter annual interest rate: "); 14 | scanf("%f", &R); 15 | printf("Enter number of months: "); 16 | scanf("%d", &n); 17 | 18 | /* Calculating interest rate per month */ 19 | r = R/(12*100); 20 | 21 | /* Calculating equated monthly intsallment (EMI) */ 22 | emi = p * r * pow(1+r,n)/(pow(1+r,n)-1); 23 | 24 | printf("Monthly EMI: %f", emi); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /ArrayInputAndPrint.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | //Array Declaration having size 100 4 | int a[100]; 5 | //SIZE INPUT 6 | int n; 7 | printf("ENTER NUMBER OF ELEMENTS YOU WANT TO INPUT :\n"); 8 | scanf("%d",&n); 9 | //ARRAY INPUT USING FOR LOOP 10 | printf("ENTER ARRAY :\n"); 11 | for(int i=0;i 9 | int main() 10 | { 11 | int n, reverse=0, rem; 12 | printf("Enter a number: "); 13 | scanf("%d", &n); 14 | while(n!=0) 15 | { 16 | rem=n%10; 17 | reverse=reverse*10+rem; 18 | n/=10; 19 | } 20 | printf("Reversed Number: %d",reverse); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /occurance_of_string.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void main() 5 | { 6 | char str[100]; 7 | int i, j, count = 0; 8 | printf("Enter the string: "); 9 | gets(str); 10 | for (i = 0; i < strlen(str); i++) 11 | { 12 | count = 1; 13 | for (j = i + 1; j < strlen(str); j++) 14 | { 15 | if (str[i] == str[j]) 16 | { 17 | count++; 18 | str[j] = '0'; 19 | } 20 | } 21 | 22 | if (str[i] != '0') 23 | printf("%c-%d ", str[i], count); 24 | } 25 | printf("\n"); 26 | } -------------------------------------------------------------------------------- /fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void fibonacci(int num); 5 | void main() 6 | { 7 | int num = 0; 8 | clrscr(); 9 | printf("Enter number of terms\t"); 10 | scanf("%d", &num); 11 | fibonacci(num); 12 | getch(); 13 | } 14 | 15 | void fibonacci(int num) 16 | { 17 | int a, b, c, i = 3; 18 | a = 0; 19 | b = 1; 20 | if(num == 1) 21 | printf("%d",a); 22 | 23 | if(num >= 2) 24 | printf("%d\t%d",a,b); 25 | 26 | while(i <= num) 27 | { 28 | c = a+b; 29 | printf("\t%d", c); 30 | a = b; 31 | b = c; 32 | i++; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Alphabet triangle .C: -------------------------------------------------------------------------------- 1 | #include     2 | #include   3 | int main() 4 | {     5 | int ch=65;         6 | int i,j,k,m;       7 | system("cls");       8 | for(i=1;i<=5;i++)         9 | {             10 | for(j=5;j>=i;j--)                 11 | printf(" ");             12 | for(k=1;k<=i;k++)                 13 | printf("%c",ch++);                 14 | ch--;             15 | for(m=1;m 7 | #include 8 | int main(){ 9 | /* This array can hold a string of upto 25 10 | * chars, if you are going to enter larger string 11 | * then increase the array size accordingly 12 | */ 13 | char str[25]; 14 | int i; 15 | printf("Enter the string: "); 16 | scanf("%s",str); 17 | 18 | for(i=0;i<=strlen(str);i++){ 19 | if(str[i]>=65&&str[i]<=90) 20 | str[i]=str[i]+32; 21 | } 22 | printf("\nLower Case String is: %s",str); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | 4 | int i, n; 5 | 6 | // initialize first and second terms 7 | int t1 = 0, t2 = 1; 8 | 9 | // initialize the next term (3rd term) 10 | int nextTerm = t1 + t2; 11 | 12 | // get no. of terms from user 13 | printf("Enter the number of terms: "); 14 | scanf("%d", &n); 15 | 16 | // print the first two terms t1 and t2 17 | printf("Fibonacci Series: %d, %d, ", t1, t2); 18 | 19 | // print 3rd to nth terms 20 | for (i = 3; i <= n; ++i) { 21 | printf("%d, ", nextTerm); 22 | t1 = t2; 23 | t2 = nextTerm; 24 | nextTerm = t1 + t2; 25 | } 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /amstrongnumber.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); 7 | int n,sum,i,t,a; 8 | printf("\n\n\nThe Armstrong numbers in between 1 to 500 are : \n\n\n"); 9 | 10 | for(i = 1; i <= 500; i++) 11 | { 12 | t = i; 13 | sum = 0; 14 | while(t != 0) 15 | { 16 | a = t%10; 17 | sum += a*a*a; 18 | t = t/10; 19 | } 20 | 21 | if(sum == i) 22 | printf("\n\t\t\t%d", i); 23 | } 24 | 25 | printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n"); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /swag.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | double first, second, temp; 4 | printf("Enter first number: "); 5 | scanf("%lf", &first); 6 | printf("Enter second number: "); 7 | scanf("%lf", &second); 8 | 9 | // value of first is assigned to temp 10 | temp = first; 11 | 12 | // value of second is assigned to first 13 | first = second; 14 | 15 | // value of temp (initial value of first) is assigned to second 16 | second = temp; 17 | 18 | // %.2lf displays number up to 2 decimal points 19 | printf("\nAfter swapping, first number = %.2lf\n", first); 20 | printf("After swapping, second number = %.2lf", second); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /sd.c: -------------------------------------------------------------------------------- 1 | // SD of a population 2 | #include 3 | #include 4 | float calculateSD(float data[]); 5 | int main() { 6 | int i; 7 | float data[10]; 8 | printf("Enter 10 elements: "); 9 | for (i = 0; i < 10; ++i) 10 | scanf("%f", &data[i]); 11 | printf("\nStandard Deviation = %.6f", calculateSD(data)); 12 | return 0; 13 | } 14 | 15 | float calculateSD(float data[]) { 16 | float sum = 0.0, mean, SD = 0.0; 17 | int i; 18 | for (i = 0; i < 10; ++i) { 19 | sum += data[i]; 20 | } 21 | mean = sum / 10; 22 | for (i = 0; i < 10; ++i) { 23 | SD += pow(data[i] - mean, 2); 24 | } 25 | return sqrt(SD / 10); 26 | } 27 | -------------------------------------------------------------------------------- /Standard Deviation.c: -------------------------------------------------------------------------------- 1 | // SD of a population 2 | #include 3 | #include 4 | float calculateSD(float data[]); 5 | int main() { 6 | int i; 7 | float data[10]; 8 | printf("Enter 10 elements: "); 9 | for (i = 0; i < 10; ++i) 10 | scanf("%f", &data[i]); 11 | printf("\nStandard Deviation = %.6f", calculateSD(data)); 12 | return 0; 13 | } 14 | 15 | float calculateSD(float data[]) { 16 | float sum = 0.0, mean, SD = 0.0; 17 | int i; 18 | for (i = 0; i < 10; ++i) { 19 | sum += data[i]; 20 | } 21 | mean = sum / 10; 22 | for (i = 0; i < 10; ++i) { 23 | SD += pow(data[i] - mean, 2); 24 | } 25 | return sqrt(SD / 10); 26 | } 27 | -------------------------------------------------------------------------------- /compare.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | char c; 4 | int lowercase_vowel, uppercase_vowel; 5 | printf("Enter an alphabet: "); 6 | scanf("%c", &c); 7 | 8 | // evaluates to 1 if variable c is a lowercase vowel 9 | lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); 10 | 11 | // evaluates to 1 if variable c is a uppercase vowel 12 | uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'); 13 | 14 | // evaluates to 1 (true) if c is a vowel 15 | if (lowercase_vowel || uppercase_vowel) 16 | printf("%c is a vowel.", c); 17 | else 18 | printf("%c is a consonant.", c); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /twisted prime.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n, reverse,sum=0 , flag; 5 | printf("Enter the prime number?"); 6 | scanf("%d",&n); 7 | while(n!=0) 8 | { 9 | reverse = n%10; 10 | sum = sum*10 + reverse; 11 | n= n/10; 12 | } 13 | 14 | printf("\n"); 15 | flag = 0; 16 | for (int j = 2; j <= sum / 2; j++) 17 | { 18 | if ((sum % j) == 0) 19 | { 20 | flag = 1; 21 | break; 22 | } 23 | } 24 | if (flag == 0) 25 | printf("Twisted Prime"); 26 | else 27 | printf("Not Twisted Prime"); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | 54 | #vs code 55 | *.vscode/ -------------------------------------------------------------------------------- /pointer.c: -------------------------------------------------------------------------------- 1 | //program to find power of one number to another and factorial value of a number using pointer 2 | void pow_fact(float x,int y,int num,float *power,int *fact) 3 | { 4 | float res=1; 5 | int i; 6 | for(i=1;i<=y;i++) 7 | res=res*x; 8 | *power=res; 9 | res=1; 10 | for(i=1;i<=num;i++) 11 | res=res*i; 12 | *fact=res; 13 | } 14 | void main() 15 | { 16 | float a; 17 | int b,num,fac; 18 | float pow; 19 | printf("enter a and b for calculating a raised to b: "); 20 | scanf("%f%d",&a,&b); 21 | printf("Enter number whose factorial is to be calculated:"); 22 | scanf("%d",&num); 23 | pow_fact(a,b,num,&pow,&fac); 24 | printf("power=%f factorial=%d",pow,fac); 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Binarysearch.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 | if(array[mid] < key) 17 | low = mid + 1; 18 | else if (array[mid] == key) { 19 | printf("%d found at location %d.n", key, mid+1); 20 | break; 21 | } 22 | else 23 | high = mid - 1; 24 | mid = (low + high)/2; 25 | } 26 | if(low > high) 27 | printf("Not found! %d isn't present in the list.n", key); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /compoundintrest.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | float PAmount, ROI, Time_Period, CIFuture, CI; 7 | 8 | printf("\nPlease enter the Principal Amount : \n"); 9 | scanf("%f", &PAmount); 10 | 11 | printf("Please Enter Rate Of Interest : \n"); 12 | scanf("%f", &ROI); 13 | 14 | printf("Please Enter the Time Period in Years : \n"); 15 | scanf("%f", &Time_Period); 16 | 17 | CIFuture = PAmount * (pow(( 1 + ROI/100), Time_Period)); 18 | CI = CIFuture - PAmount; 19 | 20 | printf("\nFuture Compound Interest for Principal Amount %.2f is = %.2f", PAmount, CIFuture); 21 | printf("\nCompound Interest for Principal Amount %.2f is = %.2f", PAmount, CI); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /largestelement.c: -------------------------------------------------------------------------------- 1 | // To find the largest element and the smallest element in the array 2 | #include 3 | int main() 4 | { 5 | int n; 6 | printf("Enter the number of elements you want to enter in this array\n"); 7 | scanf("%d",&n); 8 | int arr[n],i; 9 | printf("Enter the %d elements in the array\n",n); 10 | for(i=0;i<=n-1;i++) 11 | { 12 | scanf("%d",&arr[i]); 13 | } 14 | int max= arr[n-1]; 15 | int min=arr[0]; 16 | for(i=1;i<=n-1;i++) 17 | { 18 | if(min>arr[i]) 19 | { 20 | min=arr[i]; 21 | } 22 | } 23 | for(i=0;i<=n-2;i++) 24 | { 25 | if(max 2 | int checkPrimeNumber(int n); 3 | int main() { 4 | int n1, n2, i, flag; 5 | printf("Enter two positive integers: "); 6 | scanf("%d %d", &n1, &n2); 7 | printf("Prime numbers between %d and %d are: ", n1, n2); 8 | for (i = n1 + 1; i < n2; ++i) { 9 | 10 | // flag will be equal to 1 if i is prime 11 | flag = checkPrimeNumber(i); 12 | 13 | if (flag == 1) 14 | printf("%d ", i); 15 | } 16 | return 0; 17 | } 18 | 19 | // user-defined function to check prime number 20 | int checkPrimeNumber(int n) { 21 | int j, flag = 1; 22 | for (j = 2; j <= n / 2; ++j) { 23 | if (n % j == 0) { 24 | flag = 0; 25 | break; 26 | } 27 | } 28 | return flag; 29 | } 30 | -------------------------------------------------------------------------------- /duck no. .c: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | 4 | 5 | int main() 6 | { 7 | int dno,dkno,r,flg; 8 | flg=0; 9 | printf("\n\n Check whether a number is a Duck Number or not: \n"); 10 | printf(" ----------------------------------------------------\n"); 11 | printf(" Input a number: "); 12 | scanf("%d",&dkno); 13 | dno=dkno; 14 | while(dkno>0) 15 | { 16 | if(dkno % 10 == 0) 17 | { 18 | flg=1; 19 | break; 20 | } 21 | dkno/=10; 22 | } 23 | if(dno>0 && flg==1) 24 | { 25 | printf(" The given number is a Duck Number.\n"); 26 | } 27 | else 28 | { 29 | printf(" The given number is not a Duck Number.\n"); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /duck_number.c: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | 4 | 5 | int main() 6 | { 7 | int dno,dkno,r,flg; 8 | flg=0; 9 | printf("\n\n Check whether a number is a Duck Number or not: \n"); 10 | printf(" ----------------------------------------------------\n"); 11 | printf(" Input a number: "); 12 | scanf("%d",&dkno); 13 | dno=dkno; 14 | while(dkno>0) 15 | { 16 | if(dkno % 10 == 0) 17 | { 18 | flg=1; 19 | break; 20 | } 21 | dkno/=10; 22 | } 23 | if(dno>0 && flg==1) 24 | { 25 | printf(" The given number is a Duck Number.\n"); 26 | } 27 | else 28 | { 29 | printf(" The given number is not a Duck Number.\n"); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /recursive.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void reverse_string(char*, int, int); 4 | 5 | int main() 6 | { 7 | //This array would hold the string upto 150 char 8 | char string_array[150]; 9 | printf("Enter any string:"); 10 | scanf("%s", &string_array); 11 | 12 | //Calling our user defined function 13 | reverse_string(string_array, 0, strlen(string_array)-1); 14 | printf("\nReversed String is: %s",string_array); 15 | 16 | return 0; 17 | } 18 | 19 | void reverse_string(char *x, int start, int end) 20 | { 21 | char ch; 22 | if (start >= end) 23 | return; 24 | 25 | ch = *(x+start); 26 | *(x+start) = *(x+end); 27 | *(x+end) = ch; 28 | 29 | //Function calling itself: Recursion 30 | reverse_string(x, ++start, --end); 31 | } 32 | -------------------------------------------------------------------------------- /sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void sorting(int *x, int y); 5 | 6 | void main() 7 | { 8 | int a[20], i, c, n; 9 | clrscr(); 10 | printf("Enter number of elements you want to sort: "); 11 | scanf("%d", &n); 12 | 13 | for(i = 0; i < n; i++) 14 | scanf("%d", &a[i]); 15 | 16 | sorting(a, n); 17 | 18 | for(i = 0; i *(x+j+1)) 32 | { 33 | temp = *(x+j); 34 | *(x+j) = *(x+j+1); 35 | *(x+j+1) = temp; 36 | } 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /piglatin in c.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | void convert_to_pig_latin (char str[]); 5 | 6 | int main() 7 | { 8 | char str[100]; //defining to store input. 9 | 10 | printf("Enter a string :\n"); 11 | scanf("%s",&str); //takes input. 12 | 13 | convert_to_pig_latin (str); //calls function. 14 | return 0; 15 | } 16 | 17 | void convert_to_pig_latin (char str[]) 18 | { 19 | char nstr[100]; //defining to store answer. 20 | int l1=strlen(str); //strlen(str) will return length of string "str". 21 | 22 | for ( int j=0;j 2 | int main() { 3 | char op; 4 | double first, second; 5 | printf("Enter an operator (+, -, *, /): "); 6 | scanf("%c", &op); 7 | printf("Enter two operands: "); 8 | scanf("%lf %lf", &first, &second); 9 | 10 | switch (op) { 11 | case '+': 12 | printf("%.1lf + %.1lf = %.1lf", first, second, first + second); 13 | break; 14 | case '-': 15 | printf("%.1lf - %.1lf = %.1lf", first, second, first - second); 16 | break; 17 | case '*': 18 | printf("%.1lf * %.1lf = %.1lf", first, second, first * second); 19 | break; 20 | case '/': 21 | printf("%.1lf / %.1lf = %.1lf", first, second, first / second); 22 | break; 23 | // operator doesn't match any case constant 24 | default: 25 | printf("Error! operator is not correct"); 26 | } 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /most_fre_array.c: -------------------------------------------------------------------------------- 1 | // CPP program to find the most frequent element 2 | #include 3 | using namespace std; 4 | 5 | int mostFrequent(int arr[], int n) 6 | { 7 | sort(arr, arr + n); 8 | 9 | int max_count = 1, res = arr[0], curr_count = 1; 10 | for (int i = 1; i < n; i++) { 11 | if (arr[i] == arr[i - 1]) 12 | curr_count++; 13 | else { 14 | if (curr_count > max_count) { 15 | max_count = curr_count; 16 | res = arr[i - 1]; 17 | } 18 | curr_count = 1; 19 | } 20 | } 21 | 22 | // If last element is most frequent 23 | if (curr_count > max_count) 24 | { 25 | max_count = curr_count; 26 | res = arr[n - 1]; 27 | } 28 | 29 | return res; 30 | } 31 | 32 | // driver program 33 | int main() 34 | { 35 | int arr[] = { 1, 5, 2, 1, 3, 2, 1 }; 36 | int n = sizeof(arr) / sizeof(arr[0]); 37 | cout << mostFrequent(arr, n); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /abundant_number.c: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | # include 4 | # include 5 | int getSum(int n) 6 | { 7 | int sum = 0; 8 | for (int i=1; i<=sqrt(n); i++) 9 | { 10 | if (n%i==0) 11 | { 12 | if (n/i == i) 13 | sum = sum + i; 14 | else 15 | { 16 | sum = sum + i; 17 | sum = sum + (n / i); 18 | } 19 | } 20 | } 21 | sum = sum - n; 22 | return sum; 23 | } 24 | bool checkAbundant(int n) 25 | { 26 | return getSum(n) > n; 27 | } 28 | int main() 29 | { 30 | int n; 31 | printf("Enter the number\n"); 32 | scanf("%d",&n); 33 | checkAbundant(n)? printf("The number is Abundant.\n") : printf("The number is not Abundant.\n"); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /bubblesort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | int arr[50], i, j, n, temp; 6 | printf("Enter total number of elements to store: "); 7 | scanf("%d", &n); 8 | printf("Enter %d elements:", n); 9 | for(i=0; iarr[j+1]) 17 | { 18 | temp = arr[j]; 19 | arr[j] = arr[j+1]; 20 | arr[j+1] = temp; 21 | } 22 | } 23 | } 24 | printf("All Array elements sorted successfully!\n"); 25 | printf("Array elements in ascending order:\n\n"); 26 | for(i=0; i 2 | 3 | void swap ( int *x, int *y) 4 | { 5 | int temp = *x; 6 | *x = *y; 7 | *y = temp; 8 | return ; 9 | } 10 | 11 | void bubble_sort_recursion (int arr[], int r, int n) 12 | { 13 | // Base case 14 | if ( r == n ) 15 | return ; 16 | 17 | for ( int i = 0; i < n-r-1; ++i) 18 | if( arr[i] > arr[1+i] ) 19 | swap( &arr[i], &arr[i+1]); 20 | 21 | return bubble_sort_recursion (arr, r+1, n); 22 | } 23 | 24 | int main() 25 | { 26 | int n; 27 | 28 | printf("Enter the number of elements : "); 29 | scanf("%d",&n); 30 | 31 | int arr[n]; 32 | puts("\nEnter the element : "); 33 | for(int i = 0; i < n; ++i) 34 | scanf("%d",&arr[i]); 35 | 36 | bubble_sort_recursion( arr, 0, n ); 37 | 38 | printf("The sorted elements are as follows : \n"); 39 | for(int i = 0; i < n; ++i) 40 | printf("%d ",arr[i]); 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /insertion_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void printArray(int array[], int size); 4 | void insertionSort(int array[], int size); 5 | 6 | // Driver code 7 | int main() { 8 | int data[] = {9, 5, 1, 4, 3}; 9 | int size = sizeof(data) / sizeof(data[0]); 10 | insertionSort(data, size); 11 | printf("Sorted array in ascending order:\n"); 12 | printArray(data, size); 13 | } 14 | 15 | // Function to print an array 16 | void printArray(int array[], int size) { 17 | for (int i = 0; i < size; i++) { 18 | printf("%d ", array[i]); 19 | } 20 | printf("\n"); 21 | } 22 | // Function for Insertion Sort 23 | void insertionSort(int array[], int size) { 24 | for (int step = 1; step < size; step++) { 25 | int key = array[step]; 26 | int j = step - 1; 27 | while (key < array[j] && j >= 0) { 28 | array[j + 1] = array[j]; 29 | --j; 30 | } 31 | array[j + 1] = key; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /disarium_number.c: -------------------------------------------------------------------------------- 1 | //Program to check a number is Disarium or not 2 | #include 3 | //CalculateLength() will count the digits present in a number 4 | int calculateLength(int n) 5 | { 6 | int length = 0; 7 | while(n != 0) 8 | { 9 | length = length + 1; 10 | n = n/10; 11 | } 12 | return length; 13 | } 14 | int main() 15 | { 16 | int num , sum = 0, rem = 0, n; 17 | printf( "\n Enter any positive number : "); 18 | scanf("%d",&num); 19 | int len = calculateLength(num); 20 | //Makes a copy of the original number num 21 | n = num; 22 | //Calculates the sum of digits powered with their respective position 23 | while(num > 0) 24 | { 25 | rem = num%10; 26 | sum = sum + (int)pow(rem,len); 27 | num = num/10; 28 | len--; 29 | } 30 | //Checks whether the sum is equal to the number itself 31 | if(sum == n) 32 | printf("\n %d is a disarium number", n); 33 | else 34 | printf( "\n %d is not a disarium number", n); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Display prime numbers between two intervals.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int low, high, i, flag; 5 | printf("Enter two numbers(intervals): "); 6 | scanf("%d %d", &low, &high); 7 | printf("Prime numbers between %d and %d are: ", low, high); 8 | 9 | // iteration until low is not equal to high 10 | while (low < high) { 11 | flag = 0; 12 | 13 | // ignore numbers less than 2 14 | if (low <= 1) { 15 | ++low; 16 | continue; 17 | } 18 | 19 | // if low is a non-prime number, flag will be 1 20 | for (i = 2; i <= low / 2; ++i) { 21 | 22 | if (low % i == 0) { 23 | flag = 1; 24 | break; 25 | } 26 | } 27 | 28 | if (flag == 0) 29 | printf("%d ", low); 30 | 31 | // to check prime for the next number 32 | // increase low by 1 33 | ++low; 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | welcome to open source project for absolute begineer 2 | 3 | Please go through following step by step process to contribute in this project 4 | 5 | This is c++ programming examples projects here we need to follow these steps 6 | 7 | step1: give a star to this project(optional) 8 | 9 | step2: click on fork button on top right wait few second for completing forking of this project 10 | 11 | step3: click on add new file button and select create new file or upload file option 12 | 13 | step4: give a suitable name of your file and write a complete example 14 | 15 | step5: commit yours file 16 | 17 | step6: click contribute button and click on create pull request option and click on create pull request and finally click on commit 18 | 19 | Congratulation ! 20 | 21 | Note: please do not submit same example which are all ready available use diffrent name of your file too 22 | 23 | Link: https://github.com/suman-shah/c-program-example 24 | -------------------------------------------------------------------------------- /addition_of_matrices.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n, m; 5 | scanf("%d", &m); 6 | scanf("%d", &n); 7 | long long int arr[m][n]; 8 | long long int arr1[m][n]; 9 | long long int arr2[m][n]; 10 | for (int i = 0; i < m; i++) 11 | { 12 | for (int j = 0; j < n; j++) 13 | { 14 | scanf("%lld", &arr[i][j]); 15 | } 16 | } 17 | for (int i = 0; i < m; i++) 18 | { 19 | for (int j = 0; j < n; j++) 20 | { 21 | scanf("%lld", &arr1[i][j]); 22 | } 23 | } 24 | for (int i = 0; i < m; i++) 25 | { 26 | for (int j = 0; j < n; j++) 27 | { 28 | arr2[i][j] = arr[i][j] + arr1[i][j]; 29 | } 30 | } 31 | for (int i = 0; i < m; i++) 32 | { 33 | for (int j = 0; j < n; j++) 34 | { 35 | printf("%lld ", arr2[i][j]); 36 | } 37 | printf("\n"); 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /password_generator.c: -------------------------------------------------------------------------------- 1 | /* A program to generate strong passwords. */ 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main(int argc, char** argv) { 9 | char* characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%%^&*()\\/"; 10 | 11 | srand(time(NULL)); 12 | 13 | int password_length; 14 | 15 | // check if password length is in the program arguments. 16 | if (argc == 2) { 17 | // convert argument to int. 18 | password_length = atoi(argv[1]); 19 | } else { 20 | // get password length from user 21 | printf("Enter password length: "); 22 | scanf("%d", &password_length); 23 | } 24 | 25 | // generate password with given length 26 | for (int i = 0; i < password_length; i++) { 27 | char c = characters[rand() % strlen(characters)]; 28 | printf("%c", c); 29 | } 30 | 31 | printf("\n"); 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /switchcase.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); 6 | 7 | // Local Variable Definition 8 | char grade; 9 | printf("Enter your grade:\n"); 10 | scanf("%c", &grade); 11 | 12 | switch(grade) 13 | { 14 | case 'A': 15 | printf("Excellent\n"); 16 | break; 17 | case 'B': 18 | printf("Keep it up!\n\n"); 19 | break; 20 | case 'C': 21 | printf("Well done\nbreak keyword takes execution to exit the switch case\n\n"); 22 | break; 23 | case 'D': 24 | printf("You passed\n"); 25 | break; 26 | case 'F': 27 | printf("Better luck next time\n"); 28 | break; 29 | default: 30 | printf("Invalid grade\n"); 31 | } 32 | printf("Your grade is %c\n",grade); 33 | printf("\n\n\t\t\tCoding is Fun !\n\n\n"); 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Binary_Search.c: -------------------------------------------------------------------------------- 1 | #include 2 | int binary_search(int arr[],int n,int x){ 3 | int start = 0; 4 | int end = n-1; 5 | while(start<=end){ 6 | int mid = (start+end)/2; 7 | if(arr[mid]==x) 8 | return mid; 9 | else if(arr[mid] 2 | # include 3 | # include 4 | 5 | bool chkkaprekar(int n) 6 | { 7 | if (n == 1) 8 | return true; 9 | int sqr_n = n * n; 10 | int ctr_digits = 0; 11 | while (sqr_n) 12 | { 13 | ctr_digits++; 14 | sqr_n /= 10; 15 | } 16 | sqr_n = n*n; 17 | for (int r_digits=1; r_digits 2 | 3 | 4 | int binarySearch(int arr[], int left, int right, int x) { 5 | while (left <= right) { 6 | int mid = left + (right - left) / 2; 7 | 8 | if (arr[mid] == x) { 9 | return mid; 10 | } else if (arr[mid] < x) { 11 | left = mid + 1; 12 | } else { 13 | right = mid - 1; 14 | } 15 | } 16 | 17 | return -1; 18 | } 19 | 20 | int main() { 21 | int myarr[10]; 22 | int num; 23 | int output; 24 | 25 | std::cout << "Please enter 10 elements ASCENDING order" << std::endl; 26 | for (int i = 0; i < 10; i++) { 27 | std::cin >> myarr[i]; 28 | } 29 | 30 | std::cout << "Please enter an element to search" << std::endl; 31 | std::cin >> num; 32 | 33 | output = binarySearch(myarr, 0, 9, num); 34 | 35 | if (output == -1) { 36 | 37 | std::cout << "No Match Found" << std::endl; 38 | } else { 39 | std::cout << "Match found at position: " << output << std::endl; 40 | } 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /quardatic.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() { 4 | double a, b, c, discriminant, root1, root2, realPart, imagPart; 5 | printf("Enter coefficients a, b and c: "); 6 | scanf("%lf %lf %lf", &a, &b, &c); 7 | 8 | discriminant = b * b - 4 * a * c; 9 | 10 | // condition for real and different roots 11 | if (discriminant > 0) { 12 | root1 = (-b + sqrt(discriminant)) / (2 * a); 13 | root2 = (-b - sqrt(discriminant)) / (2 * a); 14 | printf("root1 = %.2lf and root2 = %.2lf", root1, root2); 15 | } 16 | 17 | // condition for real and equal roots 18 | else if (discriminant == 0) { 19 | root1 = root2 = -b / (2 * a); 20 | printf("root1 = root2 = %.2lf;", root1); 21 | } 22 | 23 | // if roots are not real 24 | else { 25 | realPart = -b / (2 * a); 26 | imagPart = sqrt(-discriminant) / (2 * a); 27 | printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart); 28 | } 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /2DArray_Transpose.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // The transpose of a matrix using 2D Array 4 | 5 | int main() 6 | { 7 | int arr[10][10], transpose[10][10], row, col, i, j; 8 | printf("Enter rows and columns: "); 9 | scanf("%d %d", &row, &col); 10 | printf("Enter the matrix elements: "); 11 | for (i = 0; i < row; ++i) 12 | for (j = 0; j < col; ++j) 13 | { 14 | printf("Enter element for a%d%d: ", i + 1, j + 1); 15 | scanf("%d", &arr[i][j]); 16 | } 17 | 18 | printf("The entered matrix is: \n"); 19 | for (i = 0; i < row; ++i) 20 | for (j = 0; j < col; ++j) 21 | { 22 | printf("%d ", arr[i][j]); 23 | if (j == col - 1) 24 | printf("\n"); 25 | } 26 | 27 | for (i = 0; i < row; ++i) 28 | for (j = 0; j < col; ++j) 29 | { 30 | transpose[j][i] = arr[i][j]; 31 | } 32 | 33 | printf("The transpose of the matrix is:\n"); 34 | for (i = 0; i < col; ++i) 35 | for (j = 0; j < row; ++j) 36 | { 37 | printf("%d ", transpose[i][j]); 38 | if (j == row - 1) 39 | printf("\n"); 40 | } 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Matmul.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(){ 4 | int a[10][10],b[10][10],mul[10][10],r,c,i,j,k; 5 | system("cls"); 6 | printf("enter the number of row="); 7 | scanf("%d",&r); 8 | printf("enter the number of column="); 9 | scanf("%d",&c); 10 | printf("enter the first matrix element=\n"); 11 | for(i=0;i 7 | void main() 8 | { 9 | int m, i, n, count = 0; 10 | 11 | //Entering number of elements in the array 12 | printf("Enter the no. of elements: "); 13 | scanf("%d", &m); 14 | int a[m]; 15 | 16 | //Entering the elements 17 | printf("Enter the elements: "); 18 | for (i = 0; i < m; i++) 19 | { 20 | scanf("%d", &a[i]); 21 | } 22 | 23 | //Linear search begins 24 | printf("Enter the number you want to search for: "); 25 | scanf("%d", &n); 26 | for (i = 0; i < m; i++) 27 | { 28 | if (a[i] == n) 29 | { 30 | printf("Your number %d is at location %d", n, i); 31 | count++; 32 | break; 33 | } 34 | } 35 | if (count == 0) 36 | { 37 | printf("Your Number %d is not found", n); 38 | } 39 | } -------------------------------------------------------------------------------- /counting_sort.c: -------------------------------------------------------------------------------- 1 | #include "stdio.h" 2 | 3 | // implementation of scounting sort 4 | void counting_sort(int arr[], int num_elems, int range){ 5 | int counting_arr[range + 1]; 6 | 7 | for (int i = 0; i=0; i--){ 23 | output_arr[counting_arr[arr[i]] - 1] = arr[i]; 24 | counting_arr[arr[i]]--; 25 | } 26 | 27 | for (int i = 0; i 2 | #include 3 | 4 | bool isprime(int n) 5 | { 6 | int c=0,i; 7 | for(i=2;i0) 23 | { 24 | r=n%10; 25 | sum=sum+10*r; 26 | n=n/10; 27 | } 28 | if(sum==orig) 29 | return true; 30 | else 31 | return false; 32 | } 33 | 34 | void main() 35 | { 36 | int num; 37 | bool pa,pr; 38 | printf("Enter the number \n"); 39 | scanf("%d",&num); 40 | pr=isprime(num); 41 | pa=ispalin(num); 42 | if(pa==true&&pr==true) 43 | printf("the number %d is prime-palindrome",num); 44 | else if(pa==true&&pr==false) 45 | printf("the number %d is palindrome but not prime",num); 46 | else if(pa==false&&pr==true) 47 | printf("the number %d is not palindrome but it is prime",num); 48 | else 49 | printf("the number %d is not prime-palindrome",num); 50 | } 51 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 suman-shah 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Multiplication_of_Matrices.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n, m, p = 1; 5 | scanf("%d", &m); 6 | scanf("%d", &n); 7 | long long int arr[m][n]; 8 | long long int arr1[n][p]; 9 | long long int arr2[m][p]; 10 | for (int i = 0; i < m; i++) 11 | { 12 | for (int j = 0; j < n; j++) 13 | { 14 | scanf("%lld", &arr[i][j]); 15 | } 16 | } 17 | for (int i = 0; i < n; i++) 18 | { 19 | for (int j = 0; j < p; j++) 20 | { 21 | scanf("%lld", &arr1[i][j]); 22 | } 23 | } 24 | long long int sum = 0; 25 | for (int a = 0; a < p; a++) 26 | { 27 | for (int i = 0; i < m; i++) 28 | { 29 | for (int j = 0; j < n; j++) 30 | { 31 | sum = sum + arr[i][j] * arr1[j][a]; 32 | } 33 | arr2[i][a] = sum; 34 | sum = 0; 35 | } 36 | } 37 | for (int i = 0; i < m; i++) 38 | { 39 | for (int j = 0; j < p; j++) 40 | { 41 | printf("%4lld ", arr2[i][j]); 42 | } 43 | printf("\n"); 44 | } 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /lexicographic_sort.c: -------------------------------------------------------------------------------- 1 | // Lexicographic sorting is the way of sorting words based on the alphabetical order of their component letters. 2 | 3 | #include 4 | #include 5 | void main() 6 | { 7 | char str[20][20], temp[20]; 8 | int n, i, j; 9 | printf("Enter the Number of Strings:\n"); 10 | scanf("%d", &n); 11 | 12 | // Getting strings input 13 | printf("Enter the Strings:\n"); 14 | for (i = 0; i < n; i++) 15 | { 16 | scanf("%s", str[i]); 17 | } 18 | 19 | // storing strings in the lexicographical order 20 | for (i = 0; i < n - 1; i++) 21 | { 22 | for (j = 0; j < n - 1 - i; j++) 23 | { 24 | if (strcmp(str[j], str[j + 1]) > 0) 25 | { 26 | // swapping strings if they are not in the lexicographical order 27 | strcpy(temp, str[j]); 28 | strcpy(str[j], str[j + 1]); 29 | strcpy(str[j + 1], temp); 30 | } 31 | } 32 | } 33 | printf("Strings in the Lexicographical Order is:\n"); 34 | for (i = 0; i < n; i++) 35 | { 36 | puts(str[i]); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /polynomial_eval.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct poly 5 | { 6 | float coeff; 7 | int exp; 8 | }; 9 | struct poly a[50]; 10 | 11 | int pCalc(struct poly poly[], int n, int x) 12 | { 13 | int i, sum = 0; 14 | for (i = 0; i <= n; i++) 15 | { 16 | sum += poly[i].coeff * pow(x, poly[i].exp); 17 | } 18 | return sum; 19 | } 20 | int main() 21 | { 22 | int i=0,deg1,t,x,k=0,sum=0; 23 | printf("Enter the highest degree of polynomial : "); 24 | scanf("%d",°1); 25 | printf("\nEnter number of terms : "); 26 | scanf("%d",&t); 27 | for(i=t;i>0;i--) 28 | { 29 | printf("\n\nEnter the power of x : "); 30 | scanf("%d",&a[k].exp); 31 | printf("Enter the coefficient of x^%d : ",a[k].exp); 32 | scanf("%f",&a[k].coeff); 33 | k++; 34 | } 35 | printf("\n\nEnter the VALUE of x : "); 36 | scanf("%d",&x); 37 | printf("\nExpression = "); 38 | k=0; 39 | for(i=deg1;i>=0;i--) 40 | { 41 | printf("+ %.1fx^%d",a[k].coeff,a[k].exp); 42 | k++; 43 | } 44 | sum = pCalc(a,deg1,x); 45 | printf("\n\nThe value after evavluation = %d",sum); 46 | return 0; 47 | } -------------------------------------------------------------------------------- /XOR_Decryptor.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argv, char *argc[]) 6 | { 7 | // The file out contains the decrypted output 8 | // The file en contains the seed using which we will decrypt the input 9 | // The file ans contains the decrypted output 10 | FILE *out, *en, *ans; 11 | int x, y; 12 | out = fopen(argc[1], "r"); 13 | en = fopen(argc[2], "r"); 14 | ans = fopen(argc[3], "w"); 15 | int *B = malloc(sizeof(int) * 100000000); 16 | int j = 0; 17 | for (int i = 0; i < 127; i++) 18 | fscanf(out, "%1d", &B[i]); 19 | while (fscanf(en, "%1d", &y) != EOF) 20 | { 21 | int sum = 0, temp[8] = {0}; 22 | for (int i = j; i < j + 8; i++) 23 | { 24 | if (i % 8 >= 1) 25 | fscanf(en, "%1d", &y); 26 | B[i + 127] = B[i] ^ B[i + 126]; 27 | x = B[i] ^ B[i + 126]; 28 | temp[i - j] = (x ^ y) * pow(2, 7 + j - i); 29 | sum += temp[i - j]; 30 | } 31 | j += 8; 32 | unsigned char c = sum; 33 | fprintf(ans, "%c", c); 34 | } 35 | 36 | printf("\n"); 37 | 38 | fclose(ans); 39 | fclose(en); 40 | fclose(out); 41 | } 42 | -------------------------------------------------------------------------------- /TO_find_keywords.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | void main() 6 | { 7 | int i,j,count=0; 8 | char c='\n'; 9 | char ch; 10 | FILE *fp; 11 | char Keyword[32][15]={ 12 | "extern","return","union","const","float","short", 13 | "auto","double","int","struct","break","else","long", 14 | "goto","sizeof","voltile","do","if","static","while", 15 | "unsigned","continue","for","signed","void","default", 16 | "switch","case","enum","register","typedef","char" 17 | }; 18 | fp=fopen("file.txt","w"); 19 | if(fp==NULL) 20 | { 21 | printf("File not exist"); 22 | exit(1); 23 | } 24 | for(i=0;i<32;i++) 25 | { 26 | for(j=0;j 2 | #include 3 | #include 4 | #include 5 | void main() 6 | { 7 | int i,j,count=0; 8 | char c='\n'; 9 | char ch; 10 | FILE *fp; 11 | char Keyword[32][15]={ 12 | "extern","return","union","const","float","short", 13 | "auto","double","int","struct","break","else","long", 14 | "goto","sizeof","voltile","do","if","static","while", 15 | "unsigned","continue","for","signed","void","default", 16 | "switch","case","enum","register","typedef","char" 17 | }; 18 | fp=fopen("file.txt","w"); 19 | if(fp==NULL) 20 | { 21 | printf("File not exist"); 22 | exit(1); 23 | } 24 | for(i=0;i<32;i++) 25 | { 26 | for(j=0;j 2 | 3 | // function for Binary Addition 4 | int binAddition(int a, int b) 5 | { 6 | int c; //carry 7 | while (b != 0) { 8 | //find carry and shift it left 9 | c = (a & b) << 1; 10 | //find the sum 11 | a = a ^ b; 12 | b = c; 13 | } 14 | return a; 15 | } 16 | 17 | // function for Binary Subtraction 18 | int binSubtracton(int a, int b) 19 | { 20 | int carry; 21 | //get 2's compliment of b and add in a 22 | b = binAddition(~b, 1); 23 | 24 | while (b != 0) { 25 | //find carry and shift it left 26 | carry = (a & b) << 1; 27 | //find the sum 28 | a = a ^ b; 29 | b = carry; 30 | } 31 | return a; 32 | } 33 | 34 | int main() 35 | { 36 | int number1, number2, binAdd, binSub; 37 | 38 | printf("Input first integer value: "); 39 | scanf("%d", &number1); 40 | 41 | printf("Input second integer value: "); 42 | scanf("%d", &number2); 43 | 44 | binAdd = binAddition(number1, number2); 45 | binSub = binSubtracton(number1, number2); 46 | 47 | printf("Binary Addition: %d\n", binAdd); 48 | printf("Binary Subtraction: %d\n", binSub); 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /comm.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int a[100], b[100], com[100]; 5 | int n1, n2; 6 | 7 | printf("Enter the no of elements in A:\n"); 8 | scanf("%d", &n1); 9 | printf("Enter the elements in A:\n"); 10 | for (int i = 0; i < n1; i++) 11 | scanf("%d", &a[i]); 12 | 13 | printf("Enter the no of elements in B:\n"); 14 | scanf("%d", &n2); 15 | printf("Enter the elements in B:\n"); 16 | for (int i = 0; i < n2; i++) 17 | scanf("%d", &b[i]); 18 | 19 | int i,j,m,k = 0,flag=0; 20 | for (i = 0; i < n1; i++) 21 | { 22 | for (j = 0; j < n2; j++) 23 | { 24 | flag=0; 25 | if (a[i] == b[j]) 26 | { 27 | for(m=0;m 3 | #include 4 | #include 5 | int main() 6 | { 7 | 8 | int guess, number,attempt=1; 9 | srand(time(0)); 10 | number=rand()%100+1; //generats random number from 1 to 100; 11 | 12 | // printf("the number is %d\n",number); 13 | 14 | // keep running the loop untill number is gussed 15 | 16 | // you and your friends will guess a number between 1 to 100 untill correct number is guessed 17 | // the one who guesses in least no. of attempts is winner 18 | 19 | do //program must run at least once so do loop is used hre 20 | { 21 | printf("Guess a number between 1 to 100:\n"); 22 | scanf("%d", &guess); 23 | 24 | if(guess>number){ 25 | printf("! Smaller Number please:\n"); 26 | 27 | } 28 | else if(guess 2 | void main() 3 | { 4 | int a[100], b[100], com[100]; 5 | int n1, n2; 6 | 7 | printf("Enter the no of elements in A:\n"); 8 | scanf("%d", &n1); 9 | printf("Enter the elements in A:\n"); 10 | for (int i = 0; i < n1; i++) 11 | scanf("%d", &a[i]); 12 | 13 | printf("Enter the no of elements in B:\n"); 14 | scanf("%d", &n2); 15 | printf("Enter the elements in B:\n"); 16 | for (int i = 0; i < n2; i++) 17 | scanf("%d", &b[i]); 18 | 19 | int i,j,m,k = 0,flag=0; 20 | for (i = 0; i < n1; i++) 21 | { 22 | for (j = 0; j < n2; j++) 23 | { 24 | flag=0; 25 | if (a[i] == b[j]) 26 | { 27 | for(m=0;m 2 | // Function to swap two elements 3 | void swapElements(int* x, int* y) 4 | { 5 | int temp = *x; 6 | *x = *y; 7 | *y = temp; 8 | } 9 | // Partition function 10 | int partition (int arr[], int lowIndex, int highIndex) 11 | { 12 | int pivotElement = arr[highIndex]; 13 | int i = (lowIndex - 1); 14 | for (int j = lowIndex; j <= highIndex- 1; j++) 15 | { 16 | if (arr[j] <= pivotElement) 17 | { 18 | i++; 19 | swapElements(&arr[i], &arr[j]); 20 | } 21 | } 22 | swapElements(&arr[i + 1], &arr[highIndex]); 23 | return (i + 1); 24 | } 25 | // QuickSort Function 26 | void quickSort(int arr[], int lowIndex, int highIndex) 27 | { 28 | if (lowIndex < highIndex) 29 | { 30 | int pivot = partition(arr, lowIndex, highIndex); 31 | // Separately sort elements before & after partition 32 | quickSort(arr, lowIndex, pivot - 1); 33 | quickSort(arr, pivot + 1, highIndex); 34 | } 35 | } 36 | // Function to print array 37 | void printArray(int arr[], int size) 38 | { 39 | int i; 40 | for (i=0; i < size; i++) 41 | printf("%d ", arr[i]); 42 | } 43 | // Main Function 44 | int main() 45 | { 46 | int arr[] = {81, 27, 38, 99, 51, 5}; 47 | int n = sizeof(arr)/sizeof(arr[0]); 48 | quickSort(arr, 0, n-1); 49 | printf("Sorted array: "); 50 | printArray(arr, n); 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /binary_exponentiation.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // returns base * base * base ... (exponent times) modulo modulus 4 | // (base ^ exponent)%modulus recursively 5 | long long recursive_exponentiation(long long base, long long exponent, long long modulus){ 6 | // a number power 0 is 1 7 | if (exponent == 0){ 8 | return 1; 9 | } 10 | 11 | long long result = recursive_exponentiation(base, exponent / 2, modulus); 12 | 13 | if (exponent % 2){ // if the exponent is not divisible by 2 14 | result = (result * result * base) % modulus; 15 | } else { 16 | result = (result * result) % modulus; 17 | } 18 | 19 | return result; 20 | } 21 | 22 | // returns base * base * base ... (exponent times) modulo modulus 23 | // (base ^ exponent)%modulus iteratively 24 | long long iterative_exponentiation(long long base, long long exponent, long long modulus){ 25 | long long result = 1; 26 | 27 | while (exponent > 0){ 28 | if (exponent & 1){ // if exponent not divisible by 2 29 | result = (result * base) % modulus; 30 | } 31 | base = (base * base) % modulus; 32 | exponent >>= 1; 33 | } 34 | 35 | return result; 36 | } 37 | 38 | int main(){ 39 | printf("%lld\n", recursive_exponentiation(2, 6, __INT_MAX__)); 40 | printf("%lld\n", recursive_exponentiation(5, 4, __INT_MAX__)); 41 | } -------------------------------------------------------------------------------- /bitwise_operations/get_bit.c: -------------------------------------------------------------------------------- 1 | // this program will find LSB, MSB and nth bit 2 | 3 | #include 4 | 5 | // since one byte has 8 bits 6 | #define BITS 8 * sizeof(int) 7 | 8 | int findLSB(int num); 9 | int findMSB(int num); 10 | int findNthBit(int num, int pos); 11 | void showBit(int num); 12 | 13 | 14 | int main(){ 15 | int num = 0, pos = 0; 16 | printf("Enter number: "); 17 | scanf("%d",&num); 18 | printf("Enter nth pos to find bit: "); 19 | scanf(" %d",&pos); 20 | 21 | // showing output 22 | showBit(num); 23 | printf("lsb is %d",findLSB(num)); 24 | printf("\nmsb is %d",findMSB(num)); 25 | printf("\n4th bit is %d",findNthBit(num,2)); 26 | printf("\n"); 27 | return 0; 28 | } 29 | 30 | // this function will find LSB of number 31 | int findLSB(int num){ 32 | return num & 1; 33 | } 34 | 35 | // this function will find MSB of number 36 | int findMSB(int num){ 37 | int temp = num >> BITS - 1; 38 | return temp & 1; 39 | } 40 | 41 | // this function will return nth bit 42 | int findNthBit(int num, int pos){ 43 | int temp = num >> pos; 44 | return temp & 1; 45 | } 46 | 47 | // this function will show bits 48 | void showBit(int num){ 49 | int bit = 0; 50 | printf("\n%d => ",num); 51 | for(int i = BITS - 1; i >= 0; i--){ 52 | bit = num >> i; 53 | bit = bit & 1; 54 | printf("%d",bit); 55 | } 56 | printf("\n"); 57 | } -------------------------------------------------------------------------------- /String_To_Integer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Solution { 4 | public: 5 | int myAtoi(string s) { 6 | long long int r=0,sign=1,i=0; 7 | 8 | 9 | while(1) 10 | { 11 | if(s[i] == ' ') 12 | i++; 13 | else 14 | break; 15 | } 16 | if(s[i]=='-') 17 | { 18 | sign = -1; 19 | if(int(s[i+1])>=48&& int(s[i+1]) <=57) 20 | i++; 21 | else return 0; 22 | } 23 | if(s[i]=='+') 24 | { 25 | 26 | if(int(s[i+1])>=48 && int(s[i+1]) <=57) 27 | i++; 28 | else return 0; 29 | } 30 | while(1) 31 | { 32 | if(s[i]=='0') 33 | i++; 34 | else break; 35 | } 36 | while(1) 37 | { 38 | if(int(s[i])>=48&& int(s[i])<=57) 39 | r = r*10 + int(s[i++])-48; 40 | else break; 41 | long long int c = r*sign; 42 | if(c< (-1)*(pow(2,31))) 43 | return (-1)*(pow(2,31)); 44 | if(c>pow(2,31)-1) 45 | return pow(2,31)-1; 46 | } 47 | 48 | r*= sign ; 49 | return r; 50 | 51 | } 52 | }; 53 | 54 | int main(){ 55 | Solution s ; 56 | int result= s.myAtoi("45"); 57 | cout< 4 | 5 | // since one byte has 8 bits 6 | #define BITS 8 * sizeof(int) 7 | 8 | void showBit(int num); 9 | int setNthBit(int num, int pos, int bit); 10 | 11 | 12 | int main(){ 13 | // getting from user 14 | int num, pos, bit; 15 | printf("\nEnter number: "); 16 | scanf(" %d",&num); 17 | printf("\nEnter nth pos: "); 18 | scanf(" %d",&pos); 19 | printf("\nEnter bit to be changed: "); 20 | scanf(" %d",&bit); 21 | 22 | // output 23 | showBit(num); 24 | num = setNthBit(num, pos, bit); 25 | showBit(num); 26 | return 0; 27 | } 28 | 29 | // change nth bit of number 30 | int setNthBit(int num, int pos, int bit){ 31 | // getting bits before nth pos 32 | int p1 = num >> pos + 1; 33 | p1 <<= pos + 1; 34 | 35 | // getting bits after nth pos 36 | int temp = num - p1; 37 | int temp2 = temp >> pos; 38 | temp2 <<= pos; 39 | int p2 = temp - temp2; 40 | 41 | // setting bit's position 42 | bit <<= pos; 43 | // adding all three parts to get new number 44 | return p1+bit+p2; 45 | } 46 | 47 | // this function will show bits 48 | void showBit(int num){ 49 | int bit = 0; 50 | printf("\n%d => ",num); 51 | for(int i = BITS - 1; i >= 0; i--){ 52 | bit = num >> i; 53 | bit = bit & 1; 54 | printf("%d",bit); 55 | } 56 | printf("\n"); 57 | } -------------------------------------------------------------------------------- /Merge Sort Program in C: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | // Merge Function 4 | void merge(int arr[], int l, int m, int r) 5 | { 6 | int i, j, k; 7 | int n1 = m - l + 1; 8 | int n2 = r - m; 9 | int L[n1], R[n2]; 10 | for (i = 0; i < n1; i++) 11 | L[i] = arr[l + i]; 12 | for (j = 0; j < n2; j++) 13 | R[j] = arr[m + 1+ j]; 14 | i = 0; 15 | j = 0; 16 | k = l; 17 | while (i < n1 && j < n2) 18 | { 19 | if (L[i] <= R[j]) 20 | { 21 | arr[k] = L[i]; 22 | i++; 23 | } 24 | else 25 | { 26 | arr[k] = R[j]; 27 | j++; 28 | } 29 | k++; 30 | } 31 | while (i < n1) 32 | { 33 | arr[k] = L[i]; 34 | i++; 35 | k++; 36 | } 37 | while (j < n2) 38 | { 39 | arr[k] = R[j]; 40 | j++; 41 | k++; 42 | } 43 | } 44 | // Merge Sort Function in C 45 | void mergeSort(int arr[], int l, int r) 46 | { 47 | if (l < r) 48 | { 49 | int m = l+(r-l)/2; 50 | mergeSort(arr, l, m); 51 | mergeSort(arr, m+1, r); 52 | merge(arr, l, m, r); 53 | } 54 | } 55 | // Functions to Print Elements of Array 56 | void printArray(int A[], int size) 57 | { 58 | int i; 59 | for (i=0; i < size; i++) 60 | printf("%d ", A[i]); 61 | printf("n"); 62 | } 63 | // Main Method 64 | int main() 65 | { 66 | int arr[] = {85, 24, 63, 45, 17, 31, 96, 50}; 67 | int arr_size = sizeof(arr)/sizeof(arr[0]); 68 | printf("Given array is n"); 69 | printArray(arr, arr_size); 70 | mergeSort(arr, 0, arr_size - 1); 71 | printf("nSorted array is n"); 72 | printArray(arr, arr_size); 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /Radix_Sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define range 10 4 | 5 | int getmax(int a[],int n) 6 | { 7 | int i; 8 | int mx = a[0]; 9 | 10 | for( i = 1; i < n; i++) 11 | if( a[i] > mx ) 12 | mx = a[i]; 13 | 14 | return mx; 15 | } 16 | 17 | void countsort(int arr[],int n,int place) 18 | { 19 | int output[n]; 20 | int i, freq[range]={0}; 21 | 22 | for( i = 0 ; i < n; i++) 23 | freq[ (arr[i]/place)%range ]++; 24 | 25 | for(i= 1; i < range; i++) 26 | freq[i] += freq[i-1]; 27 | 28 | for( i = n-1; i >= 0; i--) 29 | { 30 | output[ freq[ (arr[i]/place)%range ] - 1 ] = arr[i]; 31 | freq[ (arr[i]/place)%range ]--; 32 | } 33 | 34 | for( i = 0; i < n; i++) 35 | arr[i]=output[i]; 36 | 37 | return ; 38 | } 39 | 40 | void radixsort(int arr[],int n,int maxx) 41 | { 42 | int mul=1; 43 | 44 | while(maxx) 45 | { 46 | countsort( arr, n, mul); 47 | mul *= 10; 48 | maxx /= 10; 49 | } 50 | 51 | return ; 52 | } 53 | 54 | int main() 55 | { 56 | int n,i; 57 | 58 | printf("Enter the size of the array : "); 59 | scanf("%d", &n); 60 | 61 | int a[n]; 62 | 63 | printf("\nEnter the values into the array : "); 64 | for( i = 0; i < n; ++i) 65 | scanf("%d",&a[i]); 66 | 67 | radixsort(a,n,getmax(a,n)); 68 | 69 | printf("\nThe Sorted Array :\n"); 70 | for( i = 0; i < n; i++) 71 | printf("%d ",a[i]); 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /XOR_Encryptor.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argv, char *argc[]) 6 | { 7 | FILE *ptr, *out, *open; 8 | unsigned char ch; 9 | // ptr is the file to read the Seed to be encrypted 10 | // out is the file in which encrypted Seed is written 11 | // open stores the seed using which we will encrypt the input 12 | // Any person who wants to decrypt this will require the seed using which we have encrypted this file 13 | // In this repository a XOR_Decyptor.c is also being given to decrypt the generated input 14 | ptr = fopen(argc[1], "r"); 15 | out = fopen(argc[3], "w"); 16 | open = fopen(argc[2], "w"); 17 | srand(time(0)); 18 | int *C, *A; 19 | C = malloc(sizeof(int) * 100000000); 20 | A = malloc(sizeof(int) * 100000000); 21 | for (int i = 0; i < 127; i++) 22 | { 23 | C[i] = rand() % 2; 24 | fprintf(open, "%d", C[i]); 25 | } 26 | int j = 8; 27 | while (fscanf(ptr, "%c", &ch) != EOF) 28 | { 29 | int temp = ch; 30 | for (int i = j - 1; i >= j - 8; i--) 31 | { 32 | A[i] = temp % 2; 33 | temp /= 2; 34 | } 35 | j += 8; 36 | } 37 | for (int i = 127; i < 127 + j - 8; i++) 38 | { 39 | C[i] = C[i - 1] ^ C[i - 127]; 40 | A[i - 127] = C[i - 1] ^ C[i - 127] ^ A[i - 127]; 41 | fprintf(out, "%d", A[i - 127]); 42 | } 43 | 44 | fprintf(out, "\n"); 45 | fclose(open); 46 | fclose(out); 47 | fclose(ptr); 48 | } 49 | -------------------------------------------------------------------------------- /GaussianElimination.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n; 6 | scanf("%d", &n); 7 | // Array B contains the coefficients of the n 'n degree' equations long with constant term. 8 | // Array A is used to implement the Gaussian Elimination method to solve linear equations. 9 | // Array Y contains final generated output ans 10 | double B[n + 1][n]; 11 | double A[n][n + 1]; 12 | for (int i = 0; i < n + 1; i++) 13 | { 14 | for (int j = 0; j < n; j++) 15 | { 16 | scanf("%lf", &B[i][j]); 17 | } 18 | } 19 | for (int i = 0; i < n; i++) 20 | { 21 | for (int j = 0; j < n; j++) 22 | { 23 | A[i][j] = B[i][j]; 24 | } 25 | } 26 | for (int i = 0; i < n; i++) 27 | { 28 | A[i][n] = B[n][i]; 29 | } 30 | 31 | for (int b = 0; b < n - 1; b++) 32 | { 33 | for (int i = b + 1; i < n; i++) 34 | { 35 | double z = (A[i][b] / A[b][b]); 36 | for (int j = 0; j < n + 1; j++) 37 | { 38 | A[i][j] = A[i][j] - (z * A[b][j]); 39 | } 40 | } 41 | } 42 | double Y[n]; 43 | Y[n - 1] = (A[n - 1][n] / A[n - 1][n - 1]); 44 | for (int i = n - 2; i >= 0; i--) 45 | { 46 | double sum = 0; 47 | for (int j = i + 1; j < n; j++) 48 | { 49 | sum += A[i][j] * Y[j]; 50 | } 51 | Y[i] = (A[i][n] - sum) / A[i][i]; 52 | } 53 | for (int i = 0; i < n; i++) 54 | { 55 | printf("%.7lf\n", Y[i]); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Integer_To_Roman.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Solution { 4 | public: 5 | string intToRoman(int num) { 6 | string arr[] = {"I","II","III","IV","V","VI","VII","VIII","IX","X"}; 7 | string ans = ""; 8 | int m= num/1000; 9 | for(int i=0;i=900) 13 | { 14 | ans += "CM"; 15 | num -= 900; 16 | } 17 | m = num/500; 18 | for(int i=0;i=400) 22 | {ans += "CD"; 23 | num -= 400; 24 | } 25 | m = num /100; 26 | for(int i=0;i=90) 30 | { ans += "XC"; 31 | num -= 90; 32 | } 33 | m = num/50; 34 | for(int i=0;i=40) 38 | { 39 | ans += "XL"; 40 | num -= 40; 41 | } 42 | m = num /10; 43 | for(int i=0;i 2 | void getMatrix(int r,int c,int arr[][10]) 3 | { 4 | printf("Enter matrix elements\n"); 5 | for(int i=0;i 4 | 5 | // Function to swap the the position of two elements 6 | void swap(int *a, int *b) { 7 | int temp = *a; 8 | *a = *b; 9 | *b = temp; 10 | } 11 | 12 | void heapify(int arr[], int n, int i) { 13 | // Find largest among root, left child and right child 14 | int largest = i; 15 | int left = 2 * i + 1; 16 | int right = 2 * i + 2; 17 | 18 | if (left < n && arr[left] > arr[largest]) 19 | largest = left; 20 | 21 | if (right < n && arr[right] > arr[largest]) 22 | largest = right; 23 | 24 | // Swap and continue heapifying if root is not largest 25 | if (largest != i) { 26 | swap(&arr[i], &arr[largest]); 27 | heapify(arr, n, largest); 28 | } 29 | } 30 | 31 | // Main function to do heap sort 32 | void heapSort(int arr[], int n) { 33 | // Build max heap 34 | for (int i = n / 2 - 1; i >= 0; i--) 35 | heapify(arr, n, i); 36 | 37 | // Heap sort 38 | for (int i = n - 1; i >= 0; i--) { 39 | swap(&arr[0], &arr[i]); 40 | 41 | // Heapify root element to get highest element at root again 42 | heapify(arr, i, 0); 43 | } 44 | } 45 | 46 | // Print an array 47 | void printArray(int arr[], int n) { 48 | for (int i = 0; i < n; ++i) 49 | printf("%d ", arr[i]); 50 | printf("\n"); 51 | } 52 | 53 | // Driver code 54 | int main() { 55 | int arr[] = {1, 12, 9, 5, 6, 10}; 56 | int n = sizeof(arr) / sizeof(arr[0]); 57 | 58 | heapSort(arr, n); 59 | 60 | printf("Sorted array is \n"); 61 | printArray(arr, n); 62 | } 63 | -------------------------------------------------------------------------------- /find_in_doublyll.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct node 6 | { 7 | struct node *prev; 8 | int data; 9 | struct node *next; 10 | }; 11 | 12 | typedef struct My_dll my_dll; 13 | struct My_dll 14 | { 15 | struct node *head; // head=root 16 | }; 17 | 18 | // Find the index of any node in the doubly linked list. 19 | 20 | void find(my_dll *list, int x) 21 | { 22 | struct node *head = list->head; 23 | int index = 0; 24 | while (head != NULL) 25 | { 26 | if (head->data == x) 27 | { 28 | printf("%d\n", index); 29 | return; 30 | } 31 | index++; 32 | head = head->next; 33 | } 34 | printf("-1\n"); 35 | } 36 | 37 | void print(my_dll *list) 38 | { 39 | struct node *head = list->head; 40 | 41 | struct node *ptr; 42 | ptr = head; 43 | while (ptr != NULL) 44 | { 45 | printf("%d ", ptr->data); 46 | ptr = ptr->next; 47 | } 48 | printf("\n"); 49 | } 50 | 51 | int main() 52 | { 53 | struct My_dll *list; 54 | list = (struct My_dll *)malloc(sizeof(struct My_dll)); 55 | list->head = NULL; 56 | while (1) 57 | { 58 | char name[100]; 59 | scanf("%s", name); 60 | if (strcmp(name, "print") == 0) 61 | { 62 | print(list); 63 | } 64 | else if (strcmp(name, "find") == 0) 65 | { 66 | int a; 67 | scanf("%d", &a); 68 | find(list, a); 69 | } 70 | else if (strcmp(name, "exit") == 0) 71 | { 72 | return 0; 73 | } 74 | } 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /Reverse_Linkedlist.c: -------------------------------------------------------------------------------- 1 | // Iterative C program to reverse a linked list 2 | #include 3 | #include 4 | 5 | /* Link list node */ 6 | struct Node 7 | { 8 | int data; 9 | struct Node *next; 10 | }; 11 | 12 | /* Function to reverse the linked list */ 13 | static void reverse(struct Node **head_ref) 14 | { 15 | struct Node *prev = NULL; 16 | struct Node *current = *head_ref; 17 | struct Node *next = NULL; 18 | while (current != NULL) 19 | { 20 | // Store next 21 | next = current->next; 22 | 23 | // Reverse current node's pointer 24 | current->next = prev; 25 | 26 | // Move pointers one position ahead. 27 | prev = current; 28 | current = next; 29 | } 30 | *head_ref = prev; 31 | } 32 | 33 | /* Function to push a node */ 34 | void push(struct Node **head_ref, int new_data) 35 | { 36 | struct Node *new_node = (struct Node *)malloc(sizeof(struct Node)); 37 | new_node->data = new_data; 38 | new_node->next = (*head_ref); 39 | (*head_ref) = new_node; 40 | } 41 | 42 | /* Function to print linked list */ 43 | void printList(struct Node *head) 44 | { 45 | struct Node *temp = head; 46 | while (temp != NULL) 47 | { 48 | printf("%d ", temp->data); 49 | temp = temp->next; 50 | } 51 | } 52 | 53 | /* Driver code*/ 54 | int main() 55 | { 56 | /* Start with the empty list */ 57 | struct Node *head = NULL; 58 | 59 | push(&head, 20); 60 | push(&head, 4); 61 | push(&head, 15); 62 | push(&head, 85); 63 | 64 | printf("Given linked list\n"); 65 | printList(head); 66 | reverse(&head); 67 | printf("\nReversed linked list \n"); 68 | printList(head); 69 | getchar(); 70 | } 71 | -------------------------------------------------------------------------------- /mean_median_mode.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int a[50],n,i,tmp,j,temp; 5 | int z,count, mode,mcount; 6 | int tally[n]; 7 | float tot=0 , mean , median = 0, mid =0; 8 | printf("Enter number of elements : "); 9 | scanf("%d",&n); 10 | for(i=0;ia[j+1]) 26 | { 27 | tmp=a[j]; 28 | a[j]=a[j+1]; 29 | a[j+1]=tmp; 30 | } 31 | } 32 | } 33 | 34 | if(n%2 == 0) 35 | { 36 | temp=(n/2)-1; 37 | for(i=0;i mcount) 68 | { 69 | mcount = count; 70 | mode = a[i]; 71 | } 72 | } 73 | printf("\nMode value is : %d", mode); 74 | } -------------------------------------------------------------------------------- /Matrix_Determinant.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int recursive_f(int b, long long int A[b][b]) 4 | { 5 | long int determinant = 0; 6 | int L = 1, r, c; 7 | long long int X[b - 1][b - 1]; 8 | long int z = 1000000007; 9 | if (b == 1) 10 | { 11 | return A[0][0]; 12 | } 13 | else 14 | { 15 | for (int i = 0; i < b; i++) 16 | { 17 | r = 0, c = 0; 18 | for (int j = 1; j < b; j++) 19 | { 20 | for (int k = 0; k < b; k++) 21 | { 22 | if (k != i) 23 | { 24 | X[r][c] = A[j][k]; 25 | if (c <= b - 3) 26 | { 27 | c = c + 1; 28 | } 29 | else 30 | { 31 | r = r + 1; 32 | c = 0; 33 | } 34 | } 35 | } 36 | } 37 | determinant += ((L % z) * (A[0][i] % z) * (recursive_f(b - 1, X) % z)) % z; 38 | determinant = determinant % z; 39 | L = (-1) * L; 40 | } 41 | } 42 | return determinant; 43 | } 44 | 45 | int main() 46 | { 47 | int c; 48 | scanf("%d", &c); 49 | long long int A[c][c]; 50 | for (int i = 0; i < c; i++) 51 | { 52 | for (int j = 0; j < c; j++) 53 | { 54 | scanf("%lld", &A[i][j]); 55 | } 56 | } 57 | long int z = 1000000007; 58 | long long int r = recursive_f(c, A); 59 | if (r >= 0) 60 | { 61 | printf("%lld\n", r); 62 | } 63 | else 64 | { 65 | printf("%lld\n", r + z); 66 | } 67 | 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /fibonacci_and_factotial_with_recursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int fact(); 4 | int fibo(); 5 | int a=0, b=1, s; 6 | void main() 7 | { 8 | while (1) 9 | { 10 | int opt, n, facti, a=0, b=1, s; 11 | printf("\n1. Factorial\n"); 12 | printf("2. Fibonacci\n"); 13 | printf("3. Exit\n"); 14 | printf("Enter Your Choice\n"); 15 | scanf("%d", &opt); 16 | switch (opt) 17 | { 18 | case 1: 19 | printf("Enter the no.: "); 20 | scanf("%d", &n); 21 | facti=fact(n); 22 | printf("%d",facti); 23 | break; 24 | case 2: 25 | printf("Enter the no. terms you want: "); 26 | scanf("%d", &n); 27 | if (n>0) 28 | { 29 | if (n==1) 30 | { 31 | printf("0 "); 32 | } 33 | else if (n==2) 34 | { 35 | printf("0 "); 36 | printf("1 "); 37 | } 38 | else 39 | { 40 | printf("0 "); 41 | printf("1 "); 42 | n=n-2; 43 | fibo(n); 44 | } 45 | } 46 | else 47 | { 48 | printf("Enter a Valid No. of Terms!\n"); 49 | } 50 | break; 51 | case 3: 52 | exit(0); 53 | default: 54 | printf("Invalid Option!\n"); 55 | } 56 | } 57 | } 58 | 59 | 60 | //Factorial 61 | 62 | int fact(int n) 63 | { 64 | if (n==1 || n==0) 65 | { 66 | return 1; 67 | } 68 | else 69 | { 70 | return n*fact(n-1); 71 | } 72 | } 73 | 74 | 75 | //Fibonacci 76 | 77 | int fibo(int n) 78 | { 79 | while (n>0) 80 | { 81 | s=a+b; 82 | printf("%d ", s); 83 | a=b; 84 | b=s; 85 | return fibo(n-1); 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /Selection_Sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void selectionSort(int a[], int n); 5 | void print(int a[], int n); 6 | 7 | int main() 8 | { 9 | int length, i; 10 | printf("Welcome to the Selection Sort Algorithm!!\n"); 11 | printf("You can sort the data elements here, I will help you in that\n"); 12 | printf("So lets start with sorting integer datatypes\n"); 13 | printf("So let me know the how much integer datatypes you want me to sort:\n"); 14 | scanf("%d", &length); 15 | 16 | // Creating an array using calloc i.e. array for the user defined length 17 | int *array = (int *) calloc (length, sizeof(int)); 18 | 19 | // Accepting the data elements for the array 20 | printf("Now you can enter the integers:\n"); 21 | for ( i = 0; i < length; i++) 22 | { 23 | scanf("%d", &array[i]); 24 | } 25 | 26 | // Implemention the Selection Sort (in Ascending Order) 27 | selectionSort(array, length); 28 | 29 | // Printing the Sorted elements 30 | print(array, length); 31 | } 32 | 33 | void selectionSort(int a[], int n) 34 | { 35 | int i, j, minimunIndex, temp; 36 | 37 | printf("Just started to sort Using the Selection Sort Algorithm\n"); 38 | 39 | for ( i = 0; i < n; i++) 40 | { 41 | minimunIndex = i; 42 | 43 | for ( j = i + 1; j < n; j++) 44 | { 45 | if (a[minimunIndex] > a[j]) 46 | minimunIndex = j; 47 | } 48 | temp = a[i]; 49 | a[i] = a[minimunIndex]; 50 | a[minimunIndex] = temp; 51 | } 52 | 53 | printf("Ended Sorting using Selection Sort\n"); 54 | } 55 | 56 | void print(int a[], int n) 57 | { 58 | int i; 59 | printf("So now Printing the Array below:\n"); 60 | 61 | for ( i = 0; i < n; i++) 62 | { 63 | printf("%d ",a[i]); 64 | } 65 | } -------------------------------------------------------------------------------- /get_size_doubly_linkedlist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct node 6 | { 7 | struct node *prev; 8 | int data; 9 | struct node *next; 10 | }; 11 | 12 | typedef struct My_dll my_dll; 13 | struct My_dll 14 | { 15 | struct node *head; // head=root 16 | }; 17 | 18 | void print_reverse(my_dll *list) 19 | { 20 | struct node *head = list->head; 21 | struct node *ptr1 = head; 22 | while (ptr1->next) 23 | ptr1 = ptr1->next; 24 | while (ptr1) 25 | { 26 | printf("%d ", ptr1->data); 27 | ptr1 = ptr1->prev; 28 | } 29 | printf("\n"); 30 | } 31 | 32 | int get_size(my_dll *list) 33 | { 34 | struct node *head = list->head; 35 | int res = 0; 36 | while (head != NULL) 37 | { 38 | res++; 39 | head = head->next; 40 | } 41 | return res; 42 | } 43 | 44 | void print(my_dll *list) 45 | { 46 | struct node *head = list->head; 47 | 48 | struct node *ptr; 49 | ptr = head; 50 | while (ptr != NULL) 51 | { 52 | printf("%d ", ptr->data); 53 | ptr = ptr->next; 54 | } 55 | printf("\n"); 56 | } 57 | 58 | int main() 59 | { 60 | struct My_dll *list; 61 | list = (struct My_dll *)malloc(sizeof(struct My_dll)); 62 | list->head = NULL; 63 | while (1) 64 | { 65 | char name[100]; 66 | scanf("%s", name); 67 | if (strcmp(name, "print") == 0) 68 | { 69 | print(list); 70 | } 71 | else if (strcmp(name, "print_reverse") == 0) 72 | { 73 | print_reverse(list); 74 | } 75 | else if (strcmp(name, "get_size") == 0) 76 | { 77 | printf("%d\n", get_size(list)); 78 | } 79 | else if (strcmp(name, "exit") == 0) 80 | { 81 | return 0; 82 | } 83 | } 84 | return 0; 85 | } 86 | -------------------------------------------------------------------------------- /2darray.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int i, j, k, a, n, temp; 4 | 5 | /* get the order of the matrix from the user */ 6 | printf("Enter the order of the matrix:"); 7 | scanf("%d", &n); 8 | int mat[n][n]; 9 | 10 | /* get the matrix entries from the user */ 11 | printf("Enter your entries for the input mat:\n"); 12 | for (i = 0; i < n; i++) { 13 | for (j = 0; j < n; j++) { 14 | scanf("%d", &mat[i][j]); 15 | } 16 | } 17 | 18 | /* sort the contents of the two dimensional array */ 19 | for (i = 0; i < n; i++) { 20 | for (j = 0; j < n; j++) { 21 | temp = mat[i][j]; 22 | a = j + 1; 23 | for (k = i; k < n; k++) { 24 | while (a < n) { 25 | /* swapping the data */ 26 | if (temp < mat[k][a]) { 27 | temp = mat[k][a]; 28 | mat[k][a] =mat[i][j]; 29 | mat[i][j] = temp; 30 | } 31 | a++; 32 | } 33 | a = 0; // to continue sorting in consecutive rows 34 | } 35 | } 36 | } 37 | 38 | /* print the result */ 39 | printf("\n"); 40 | printf("Sorted Matrix in decending order:\n"); 41 | for (i = 0; i < n; i++) { 42 | for (j = 0; j < n; j++) { 43 | printf("%d ", mat[i][j]); 44 | } 45 | printf("\n"); 46 | } 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /circular_queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct queue{ 6 | int front ,rear, size; 7 | unsigned capacity; 8 | int* arr; 9 | }; 10 | 11 | struct queue * createQueue(unsigned capacity){ 12 | struct queue* q=(struct queue*)malloc(sizeof(struct queue)); 13 | q->capacity = capacity; 14 | q->front = q->size = 0; 15 | q->rear= q->capacity - 1; 16 | q->arr = (int *)malloc(q->capacity*sizeof(int)); 17 | return q; 18 | } 19 | 20 | int isFull(struct queue* q){ 21 | return(q->size == q->capacity); 22 | } 23 | 24 | int isEmpty(struct queue* q){ 25 | return (q->size == 0); 26 | } 27 | 28 | void enqueue(struct queue* q,int data){ 29 | if(isFull(q)) return; 30 | q->rear = (q->rear+1)%q->capacity; //for circular queue 31 | q->arr[q->rear] = data; 32 | q->size = q->size+1; 33 | } 34 | 35 | int dequeue(struct queue* q){ 36 | if(isEmpty(q)) 37 | return INT_MIN; 38 | int item = q->arr[q->front]; 39 | q->front = (q->front+1)%q->capacity; 40 | q->size--; 41 | return item; 42 | } 43 | 44 | int front(struct queue* q){ 45 | if(isEmpty(q)) return INT_MIN; 46 | return q->arr[q->front]; 47 | } 48 | 49 | int rear(struct queue* q){ 50 | if(isEmpty(q)) return INT_MIN; 51 | return q->arr[q->rear]; 52 | } 53 | 54 | int main(){ 55 | printf("Enter capacity of queue: "); 56 | int val; 57 | scanf("%d",&val); 58 | struct queue* new=createQueue(val); 59 | for(int i=0;i 2 | using namespace std; 3 | class Solution { 4 | public: 5 | int romanToInt(string s) { 6 | int r =0; 7 | for(int i=0;iemptying a vessel 12 | * ->filling a vessel 13 | * ->pouring liquid from larger can to the smaller, 14 | * without spilling untill one of the cans is either full or empty. 15 | * 16 | * If it is not possible to obtain Z liters exactly then the output must be -1. 17 | * 18 | * Example 1: 19 | * 20 | * Input: 21 | * 5 22 | * 2 23 | * 3 24 | * Output: 25 | * 2 26 | * 27 | * Explanation: 28 | * Here X=5,Y=2 29 | * Step 1:Pour 5 liters of liquid into 5 liter can 30 | * step 2:Pour 2 liter from 5 liter can into 2 liter can 31 | * Now the 5 liter can will have 3 liter which is Z.Hence 2 steps are required. 32 | * 33 | * Example 2: 34 | * 35 | * Input: 36 | * 2 37 | * 3 38 | * 4 39 | * Output: 40 | * -1 41 | * 42 | * Explanation: 43 | * Z is greater than X and Y. 44 | * Hence it is not possible to have 4 liters in any one of the cans. 45 | * Hence output is -1. 46 | * 47 | */ 48 | 49 | #include 50 | 51 | int main() 52 | { 53 | int x, y, z; 54 | int count = 1, check = 0, res = 0; 55 | 56 | scanf("%d %d %d",&x,&y,&z); 57 | 58 | if( z > x && z > y ) 59 | { 60 | printf("%d",-1); 61 | return 0; 62 | } 63 | 64 | if( x < y ) 65 | { 66 | int temp = x; 67 | x = y; 68 | y = temp; 69 | } 70 | 71 | else{ 72 | 73 | do 74 | { 75 | if( x == z ) 76 | { 77 | check = 1; 78 | break; 79 | } 80 | 81 | x = x-y; 82 | 83 | if( res ) 84 | { 85 | count += 2; 86 | } 87 | else 88 | { 89 | count++; 90 | res = 1; 91 | } 92 | 93 | }while( x >= z ); 94 | } 95 | 96 | 97 | printf("%d", (check) ? count : -1 ); 98 | 99 | return 0; 100 | } 101 | -------------------------------------------------------------------------------- /polynomial_addition.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct poly 5 | { 6 | float coeff; 7 | int exp; 8 | }; 9 | struct poly a[50],b[50],c[50]; 10 | int main() 11 | { 12 | int i; 13 | int deg1,deg2; 14 | int k=0,l=0,m=0; 15 | printf("Enter the highest degree of poly1:"); 16 | scanf("%d",°1); 17 | for(i=0;i<=deg1;i++) 18 | { 19 | printf("\nEnter the coeff of x^%d :",i); 20 | scanf("%f",&a[i].coeff); 21 | 22 | a[k++].exp = i; 23 | } 24 | printf("\nEnter the highest degree of poly2:"); 25 | scanf("%d",°2); 26 | 27 | for(i=0;i<=deg2;i++) 28 | { 29 | printf("\nEnter the coeff of x^%d :",i); 30 | scanf("%f",&b[i].coeff); 31 | 32 | b[l++].exp = i; 33 | } 34 | 35 | printf("\nExpression 1 = %.1f",a[0].coeff); 36 | for(i=1;i<=deg1;i++) 37 | { 38 | printf("+ %.1fx^%d",a[i].coeff,a[i].exp); 39 | } 40 | 41 | printf("\nExpression 2 = %.1f",b[0].coeff); 42 | for(i=1;i<=deg2;i++) 43 | { 44 | printf("+ %.1fx^%d",b[i].coeff,b[i].exp); 45 | } 46 | 47 | if(deg1>deg2) 48 | { 49 | for(i=0;i<=deg2;i++) 50 | { 51 | c[m].coeff = a[i].coeff + b[i].coeff; 52 | c[m].exp = a[i].exp; 53 | m++; 54 | } 55 | 56 | for(i=deg2+1;i<=deg1;i++) 57 | { 58 | c[m].coeff = a[i].coeff; 59 | c[m].exp = a[i].exp; 60 | m++; 61 | } 62 | } 63 | else 64 | { 65 | for(i=0;i<=deg1;i++) 66 | { 67 | c[m].coeff = a[i].coeff + b[i].coeff; 68 | c[m].exp = a[i].exp; 69 | m++; 70 | } 71 | 72 | for(i=deg1+1;i<=deg2;i++) 73 | { 74 | c[m].coeff = b[i].coeff; 75 | c[m].exp = b[i].exp; 76 | m++; 77 | } 78 | } 79 | 80 | printf("\nExpression after additon = %.1f",c[0].coeff); 81 | for(i=1;i 2 | #include // in gcc, use < conio.h > in turboc 3 | #include 4 | 5 | #define USERNAME "user001" 6 | #define PASSWORD "ok@123" 7 | 8 | /************************************************ 9 | THIS CODE IS USED IN GCC LINUX 10 | because getch() is not defined in library file 11 | ************************************************/ 12 | 13 | static struct termios old, new; 14 | 15 | /* Initialize new terminal i/o settings */ 16 | void initTermios(int echo) 17 | { 18 | tcgetattr(0, &old); /* grab old terminal i/o settings */ 19 | new = old; /* make new settings same as old settings */ 20 | new.c_lflag &= ~ICANON; /* disable buffered i/o */ 21 | new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */ 22 | tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */ 23 | } 24 | 25 | /* Restore old terminal i/o settings */ 26 | void resetTermios(void) 27 | { 28 | tcsetattr(0, TCSANOW, &old); 29 | } 30 | 31 | /* Read 1 character - echo defines echo mode */ 32 | char getch_(int echo) 33 | { 34 | char ch; 35 | initTermios(echo); 36 | ch = getchar(); 37 | resetTermios(); 38 | return ch; 39 | } 40 | 41 | /* Read 1 character without echo */ 42 | char getch(void) 43 | { 44 | return getch_(0); 45 | } 46 | 47 | /* Read 1 character with echo */ 48 | char getche(void) 49 | { 50 | return getch_(1); 51 | } 52 | 53 | /* function : getPassword(), 54 | to get password from keyboard */ 55 | 56 | void getPassword(char *pass) 57 | { 58 | int c=0; 59 | char buff[30]={0},ch; 60 | int len=0; 61 | while((ch=getch())!='\n') 62 | { 63 | if(ch==0x7f) // use 0x08 in turboc (WINDOWS) 64 | { 65 | if(len==0) continue; 66 | printf("\b \b"); len--; continue; 67 | } 68 | printf("%c",'*'); 69 | pass[len]=ch; 70 | len++; 71 | } 72 | pass[len]='\0'; 73 | 74 | } 75 | 76 | int main() 77 | { 78 | 79 | char user[30],pass[30]; 80 | printf("Enter User Name :"); 81 | gets(user); 82 | printf("Enter Password :"); 83 | getPassword(pass); 84 | 85 | if(strcmp(user,USERNAME)==0 && strcmp(pass,PASSWORD)==0) 86 | printf("\nLOGIN SUCCESS.\n"); 87 | else 88 | printf("\nLOGIN FAILED.\n"); 89 | return 0; 90 | } 91 | -------------------------------------------------------------------------------- /Tree Traversal.c: -------------------------------------------------------------------------------- 1 | // C program for different tree traversals 2 | #include 3 | #include 4 | 5 | /* A binary tree node has data, pointer to left child 6 | and a pointer to right child */ 7 | struct node { 8 | int data; 9 | struct node* left; 10 | struct node* right; 11 | }; 12 | 13 | /* Helper function that allocates a new node with the 14 | given data and NULL left and right pointers. */ 15 | struct node* newNode(int data) 16 | { 17 | struct node* node 18 | = (struct node*)malloc(sizeof(struct node)); 19 | node->data = data; 20 | node->left = NULL; 21 | node->right = NULL; 22 | 23 | return (node); 24 | } 25 | 26 | /* Given a binary tree, print its nodes according to the 27 | "bottom-up" postorder traversal. */ 28 | void printPostorder(struct node* node) 29 | { 30 | if (node == NULL) 31 | return; 32 | 33 | // first recur on left subtree 34 | printPostorder(node->left); 35 | 36 | // then recur on right subtree 37 | printPostorder(node->right); 38 | 39 | // now deal with the node 40 | printf("%d ", node->data); 41 | } 42 | 43 | /* Given a binary tree, print its nodes in inorder*/ 44 | void printInorder(struct node* node) 45 | { 46 | if (node == NULL) 47 | return; 48 | 49 | /* first recur on left child */ 50 | printInorder(node->left); 51 | 52 | /* then print the data of node */ 53 | printf("%d ", node->data); 54 | 55 | /* now recur on right child */ 56 | printInorder(node->right); 57 | } 58 | 59 | /* Given a binary tree, print its nodes in preorder*/ 60 | void printPreorder(struct node* node) 61 | { 62 | if (node == NULL) 63 | return; 64 | 65 | /* first print data of node */ 66 | printf("%d ", node->data); 67 | 68 | /* then recur on left subtree */ 69 | printPreorder(node->left); 70 | 71 | /* now recur on right subtree */ 72 | printPreorder(node->right); 73 | } 74 | 75 | /* Driver program to test above functions*/ 76 | int main() 77 | { 78 | struct node* root = newNode(1); 79 | root->left = newNode(2); 80 | root->right = newNode(3); 81 | root->left->left = newNode(4); 82 | root->left->right = newNode(5); 83 | 84 | printf("\nPreorder traversal of binary tree is \n"); 85 | printPreorder(root); 86 | 87 | printf("\nInorder traversal of binary tree is \n"); 88 | printInorder(root); 89 | 90 | printf("\nPostorder traversal of binary tree is \n"); 91 | printPostorder(root); 92 | 93 | getchar(); 94 | return 0; 95 | } 96 | -------------------------------------------------------------------------------- /Cyclic_Sort.c: -------------------------------------------------------------------------------- 1 | // Cyclic Sort: 2 | // ------------ 3 | 4 | // Cyclic sort is a sorting technique which performs sorting in O(n) i.e., in liner time complexity. 5 | // Thus making it one of the most efficient sorting alogrithm and one which you're going to use during 6 | // interview exams. 7 | 8 | // Q) Where can we apply cyclic sort? 9 | // Sol) We can apply this sorting technique only when the given array of elements is range within 0 to n 10 | // elements (or) 1 to n-1 elements. 11 | 12 | // Q) What is cyclic sort ? 13 | // Sol) Since the array of elements are range from 0 to n elements ( it can 1 to n-1 elements too ), 14 | // the resultant/sorted array will satisfy the condition: 15 | 16 | // array[ element-1 ] = element 17 | 18 | // So, by using the same principal we can sort the whole array in Linear Time complexity and Constant 19 | // Space complexity. 20 | 21 | // Say what if the condition for an element in "Unsorted Array" fails ? We just swap the element into its 22 | // rightful place using the same principal. 23 | 24 | #include 25 | 26 | // Cyclic Sort logic 27 | void cyclic_sort(int arr[]) 28 | { 29 | int i = 0, ele, temp; 30 | while (arr[i] != '\0') 31 | { 32 | ele = arr[i]; 33 | 34 | // if the condition is satisfied then 35 | // this element is sorted in its place 36 | // So we check for next element 37 | if (arr[ele - 1] == ele) 38 | { 39 | ++i; 40 | continue; 41 | } 42 | 43 | // else we will swap 'ele' into 44 | // its place 45 | temp = arr[ele - 1]; 46 | arr[ele - 1] = arr[i]; 47 | arr[i] = temp; 48 | } 49 | return; 50 | } 51 | 52 | // Display function prototyping 53 | void display_arr(int arr[]); 54 | 55 | // Driver Program 56 | int main(void) 57 | { 58 | int arr[6] = {1, 5, 3, 4, 2}; 59 | 60 | printf("\nUnsorted Array: "); 61 | display_arr(arr); 62 | 63 | printf("\nAfter applying Cyclic Sort: "); 64 | cyclic_sort(arr); 65 | display_arr(arr); 66 | 67 | printf("\n"); 68 | 69 | return 0; 70 | } 71 | 72 | // Display function definition 73 | // Inspired by Arrays.toString() method in Java 74 | void display_arr(int arr[]) 75 | { 76 | int i; 77 | 78 | printf("[ "); 79 | 80 | for (i = 0; arr[i + 1] != '\0'; ++i) 81 | { 82 | printf("%d, ", arr[i]); 83 | } 84 | 85 | printf("%d ]\n", arr[i]); 86 | 87 | return; 88 | } 89 | 90 | // Output : 91 | // ------- 92 | // 93 | // Unsorted Array : [ 1, 5, 3, 4, 2 ] 94 | // 95 | // After applying Cyclic Sort : [ 1, 2, 3, 4, 5 ] -------------------------------------------------------------------------------- /Weekday of given date.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* 4 | * function: validateDate 5 | * arguments: d- day, m- month, y- year 6 | * return type: 0 - invalid, 1 - valid 7 | * */ 8 | int validateDate(int d, int m, int y) 9 | { 10 | //check year validity 11 | if (y >= 1800 && y <= 2999) { 12 | //check month validity 13 | if (m >= 1 && m <= 12) { 14 | //check day validity 15 | if (d >= 1 && d <= 31) { 16 | if ((d >= 1 && d <= 30) && (m == 4 || m == 6 || m == 9 || m == 11)) 17 | return 1; //valid date 18 | else if ((d >= 1 && d <= 30) && (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)) 19 | return 1; //valid date 20 | else if ((d >= 1 && d <= 28) && (m == 2)) 21 | return 1; //valid date 22 | else if (d == 29 && m == 2 && ((y % 400 == 0) || (y % 4 == 0 && y % 4 != 0))) 23 | return 1; //valid date 24 | else 25 | return 0; //invalid day 26 | } 27 | else { 28 | return 0; //day is invalid 29 | } 30 | } 31 | else { 32 | return 0; //month is invalid 33 | } 34 | } 35 | else { 36 | return 0; //year is invalid 37 | } 38 | } 39 | // This function will return week day number from 0 to 6 40 | int wd(int year, int month, int day) 41 | { 42 | int wday = 0; 43 | wday = (day + ((153 * (month + 12 * ((14 - month) / 12) - 3) + 2) / 5) 44 | + (365 * (year + 4800 - ((14 - month) / 12))) 45 | + ((year + 4800 - ((14 - month) / 12)) / 4) 46 | - ((year + 4800 - ((14 - month) / 12)) / 100) 47 | + ((year + 4800 - ((14 - month) / 12)) / 400) 48 | - 32045) 49 | % 7; 50 | return wday; 51 | } 52 | 53 | int main() 54 | { 55 | int day, month, year; 56 | int wDayNo = 0; 57 | char dayNames[][12] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; 58 | 59 | //input date 60 | printf("Input date (DD-MM-YYYY): "); 61 | scanf("%d-%d-%d", &day, &month, &year); 62 | 63 | //check date is correct or not 64 | if (validateDate(day, month, year) == 1) { 65 | printf("Date is correct [%02d/%02d/%02d].\n", day, month, year); 66 | //get weekday number 67 | wDayNo = wd(year, month, day); 68 | //print weekday according to wDayNo 69 | printf("week day is: %s\n", dayNames[wDayNo]); 70 | } 71 | else 72 | printf("Date is in-correct.\n"); 73 | 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /product_of_2_matrices.c: -------------------------------------------------------------------------------- 1 | //Finding the product of 2 matrices 2 | 3 | #include 4 | void main() 5 | { 6 | int m, n, i, j, p, q, k; 7 | 8 | //Entering matrix elements 9 | printf("Enter the no. of rows of first matrix:\n"); 10 | scanf("%d", &m); 11 | printf("Enter the no. of columns of first matrix:\n"); 12 | scanf("%d", &n); 13 | printf("Enter the no. of rows of second matrix:\n"); 14 | scanf("%d", &p); 15 | printf("Enter the no. of columns of second matrix:\n"); 16 | scanf("%d", &q); 17 | 18 | //Number of columns of first matrix equal to number of rows of second matrix implies that matrix multiplication is impossible 19 | if (n != p) 20 | { 21 | printf("Matrix multiplication is not possible"); 22 | } 23 | else 24 | { 25 | int a[m][n], b[p][q], c[m][q]; 26 | for (i = 0; i < m; i++) 27 | { 28 | for (j = 0; j < n; j++) 29 | { 30 | printf("Enter the a[%d][%d] element:\n", i, j); 31 | scanf("%d", &a[i][j]); 32 | } 33 | } 34 | for (i = 0; i < p; i++) 35 | { 36 | for (j = 0; j < q; j++) 37 | { 38 | printf("Enter the b[%d][%d] element:\n", i, j); 39 | scanf("%d", &b[i][j]); 40 | } 41 | } 42 | 43 | //Displaying first matrix 44 | printf("The first matrix is:\n"); 45 | for (i = 0; i < m; i++) 46 | { 47 | for (j = 0; j < n; j++) 48 | { 49 | printf("\t%d", a[i][j]); 50 | } 51 | printf("\n"); 52 | } 53 | 54 | //Displaying second matrix 55 | printf("The second matrix is:\n"); 56 | for (i = 0; i < p; i++) 57 | { 58 | for (j = 0; j < q; j++) 59 | { 60 | printf("\t%d", b[i][j]); 61 | } 62 | printf("\n"); 63 | } 64 | 65 | for (i = 0; i < m; i++) 66 | { 67 | for (j = 0; j < n; j++) 68 | { 69 | c[i][j] = 0; 70 | for (k = 0; k < n; k++) 71 | { 72 | c[i][j] += a[i][k] * b[k][j]; 73 | } 74 | } 75 | } 76 | //Displaying product matrix 77 | printf("The product matrix is:\n"); 78 | for (i = 0; i < m; i++) 79 | { 80 | for (j = 0; j < q; j++) 81 | { 82 | printf("\t%d", c[i][j]); 83 | } 84 | printf("\n"); 85 | } 86 | } 87 | } -------------------------------------------------------------------------------- /Infix_to_postfix.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #define SIZE 100 7 | 8 | char stack[SIZE]; 9 | int top = -1; 10 | 11 | void push(char item) 12 | { 13 | if(top >= SIZE-1) 14 | { 15 | printf("\nStack Overflow."); 16 | } 17 | else 18 | { 19 | top = top+1; 20 | stack[top] = item; 21 | } 22 | } 23 | 24 | 25 | char pop() 26 | { 27 | char item ; 28 | 29 | if(top <0) 30 | { 31 | printf("stack under flow: invalid infix expression"); 32 | getchar(); 33 | exit(1); 34 | } 35 | else 36 | { 37 | item = stack[top]; 38 | top = top-1; 39 | return(item); 40 | } 41 | } 42 | 43 | 44 | int is_operator(char symbol) 45 | { 46 | if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol =='-') 47 | return 1; 48 | return 0; 49 | } 50 | 51 | 52 | int precedence(char symbol) 53 | { 54 | if(symbol == '^') 55 | { 56 | return(3); 57 | } 58 | else if(symbol == '*' || symbol == '/') 59 | { 60 | return(2); 61 | } 62 | else if(symbol == '+' || symbol == '-') 63 | { 64 | return(1); 65 | } 66 | else 67 | { 68 | return(0); 69 | } 70 | } 71 | 72 | void InfixToPostfix(char infix_exp[], char postfix_exp[]) 73 | { 74 | int i, j; 75 | char item; 76 | char x; 77 | 78 | push('('); 79 | strcat(infix_exp,")"); 80 | 81 | i=0; 82 | j=0; 83 | 84 | item=infix_exp[i]; 85 | 86 | while(item != '\0') 87 | { 88 | if(item == '(') 89 | { 90 | push(item); 91 | } 92 | else if( isdigit(item) || isalpha(item)) 93 | { 94 | postfix_exp[j] = item; 95 | j++; 96 | } 97 | else if(is_operator(item) == 1) 98 | { 99 | x=pop(); 100 | while(is_operator(x) == 1 && precedence(x)>= precedence(item)) 101 | { 102 | postfix_exp[j] = x; 103 | j++; 104 | x = pop(); 105 | } 106 | push(x); 107 | push(item); 108 | } 109 | else if(item == ')') 110 | { 111 | x = pop(); 112 | while(x != '(') 113 | { 114 | postfix_exp[j] = x; 115 | j++; 116 | x = pop(); 117 | } 118 | } 119 | else 120 | { 121 | printf("\nInvalid infix Expression.\n"); 122 | getchar(); 123 | exit(1); 124 | } 125 | i++; 126 | item = infix_exp[i]; 127 | } 128 | if(top>0) 129 | { 130 | printf("\nInvalid infix Expression.\n"); 131 | getchar(); 132 | exit(1); 133 | } 134 | postfix_exp[j] = '\0'; 135 | } 136 | 137 | 138 | int main() 139 | { 140 | char infix[SIZE], postfix[SIZE]; 141 | 142 | printf("\nEnter Infix expression : "); 143 | gets(infix); 144 | 145 | InfixToPostfix(infix,postfix); 146 | 147 | printf("\nPostfix Expression: "); 148 | puts(postfix); 149 | 150 | return 0; 151 | } 152 | -------------------------------------------------------------------------------- /doubly linked list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | // constructor 4 | struct node{ 5 | int data; 6 | struct node*next; 7 | struct node*prev;}*head; 8 | 9 | //finction for creating doubly linked list 10 | void createlist(int n) 11 | { 12 | struct node*newnode,*temp; 13 | int data,i=1; 14 | head=(struct node*)malloc(sizeof(struct node)); 15 | if(head==0) 16 | {printf("empty");} 17 | else 18 | {printf("enter data"); 19 | scanf("%d",&head->data); 20 | head->next=0; 21 | head->prev=0; 22 | temp=head; 23 | for(i=2;i<=n;i++) 24 | { 25 | newnode=(struct node*)malloc(sizeof(struct node)); 26 | printf("enter data"); 27 | scanf("%d",&newnode->data); 28 | newnode->next=0; 29 | temp->next=newnode; 30 | newnode->prev=temp; 31 | temp=newnode; 32 | } 33 | } 34 | 35 | } 36 | 37 | // display a list 38 | void displaylist(){struct node*temp; 39 | int count=0; 40 | temp=head; 41 | while(temp!=0) 42 | { 43 | printf("%d\n",temp->data); 44 | temp=temp->next; 45 | count++; 46 | } 47 | printf("count is :%d\n",count); 48 | } 49 | 50 | // insert at beginning 51 | struct node*insertatbeg(){ 52 | int data; 53 | struct node*newnode; 54 | newnode=(struct node*)malloc(sizeof(struct node)); 55 | printf("enter data u want to insert"); 56 | scanf("%d",&newnode->data); 57 | newnode->next=head; 58 | newnode->prev=0; 59 | head=newnode; 60 | return head; 61 | } 62 | 63 | 64 | 65 | // search an element in linked list 66 | void searchelement(int item){ 67 | struct node*temp; 68 | temp=head; 69 | int pos=1; 70 | while(temp!=0) 71 | { 72 | if(temp->data==item) 73 | {printf("position is:%d",pos); 74 | return; 75 | } 76 | pos++; 77 | temp=temp->next; 78 | 79 | } 80 | } 81 | 82 | 83 | // feletion of node at beginning 84 | struct node*deleteatbeg() 85 | {struct node*temp; 86 | temp=head; 87 | head=head->next; 88 | free(temp); 89 | return head; 90 | } 91 | 92 | // deletion at end 93 | 94 | struct node*deleteatend(){ 95 | struct node*temp,*prevnode; 96 | temp=head; 97 | while(temp->next!=0) 98 | { 99 | prevnode=temp; 100 | temp=temp->next; 101 | } 102 | prevnode->next=0; 103 | free(temp) ; 104 | return head; 105 | } 106 | 107 | 108 | int main(){ 109 | 110 | int n,item; 111 | printf("enter no of nodes"); 112 | scanf("%d",&n); 113 | createlist(n); 114 | printf("list is:\n"); 115 | displaylist(); 116 | printf("after insertion"); 117 | head=insertatbeg(); 118 | displaylist(); 119 | 120 | printf("enter element to be searched"); 121 | scanf("%d",&item); 122 | searchelement(item); 123 | 124 | head=deleteatbeg(); 125 | displaylist(); 126 | head=deleteatend(); 127 | displaylist(); 128 | return 0; 129 | } 130 | 131 | -------------------------------------------------------------------------------- /queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 5 4 | 5 | void enqueue(); 6 | void dequeue(); 7 | void display(); 8 | 9 | int queue[MAX]; 10 | int rear = - 1, front = - 1; 11 | 12 | int main(){ 13 | int choice; 14 | 15 | printf("1.Insert element to queue \n"); 16 | printf("2.Delete element from queue \n"); 17 | printf("3.Display all elements of Circular queue \n"); 18 | printf("4.Quit \n"); 19 | 20 | while (1) 21 | { 22 | printf("\nEnter your choice : "); 23 | scanf("%d", &choice); 24 | 25 | switch(choice) 26 | { 27 | case 1: 28 | enqueue(); 29 | break; 30 | case 2: 31 | dequeue(); 32 | break; 33 | case 3: 34 | display(); 35 | break; 36 | case 4: 37 | exit(1); 38 | default: 39 | printf("Invalid choice \n"); 40 | } 41 | } 42 | } 43 | void enqueue() 44 | { 45 | int item; 46 | if((rear == MAX - 1 && front == 0) || (front == rear+1)) 47 | { 48 | printf("Queue Overflow \n"); 49 | } 50 | else 51 | { 52 | if(front == - 1) front = 0; 53 | printf("\nInsert the element in queue : "); 54 | scanf("%d", &item); 55 | 56 | if(rear == MAX -1) rear = 0; 57 | else rear = rear + 1; 58 | 59 | queue[rear] = item; 60 | } 61 | } 62 | void dequeue() 63 | { 64 | if(front == - 1) 65 | { 66 | printf("Queue Underflow \n"); 67 | return; 68 | } 69 | else 70 | { 71 | printf("\nElement deleted from queue is : %d\n", queue[front]); 72 | 73 | if(front == rear){ 74 | //queue[front] = NULL; 75 | front = -1; 76 | rear = -1; 77 | 78 | } 79 | else if (front == MAX -1) 80 | { 81 | //queue[front] = NULL; 82 | front = 0; 83 | } 84 | else 85 | { 86 | //queue[front] = NULL; 87 | front = front + 1; 88 | } 89 | } 90 | } 91 | void display() 92 | { 93 | int i; 94 | if(front == - 1) 95 | { 96 | printf("Queue is empty \n"); 97 | } 98 | else if(front > rear) 99 | { 100 | 101 | printf("\nQueue is : \n"); 102 | for(i = front; i <= MAX-1; i++) 103 | { 104 | printf("%d ", queue[i]); 105 | printf("\n"); 106 | } 107 | for(i = 0; i <= rear; i++) 108 | { 109 | printf("%d ", queue[i]); 110 | printf("\n"); 111 | } 112 | } 113 | else{ 114 | printf("\nQueue is : \n\t"); 115 | for(i = front; i <= MAX-1; i++){ 116 | printf("%d ", queue[i]); 117 | printf("\n"); 118 | } 119 | } 120 | 121 | } 122 | -------------------------------------------------------------------------------- /traversals_in_binarytree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef int DataType; 5 | 6 | typedef struct BT_Node 7 | { 8 | DataType val; 9 | struct BT_Node *left, *right; 10 | } BT_Node; 11 | 12 | typedef struct BT 13 | { 14 | BT_Node *root; 15 | } BT; 16 | 17 | // Initalize a binary tree node and return a pointer to it 18 | BT_Node *BT_Node_init(DataType val) 19 | { 20 | BT_Node *n = malloc(sizeof(BT_Node)); 21 | n->val = val; 22 | n->left = n->right = NULL; 23 | return n; 24 | } 25 | 26 | // Create a binary tree and return it 27 | // arr is n x 3, where for the ith node 28 | // arr[i][0] is parent, arr[i][1] is left/right, arr[i][2] is value 29 | BT *create_BT(int **arr, int n) 30 | { 31 | BT *b = (BT *)malloc(sizeof(BT)); 32 | BT_Node *nodes[n]; 33 | for (int i = 0; i < n; ++i) 34 | { 35 | nodes[i] = BT_Node_init(arr[i][2]); 36 | } 37 | for (int i = 0; i < n; ++i) 38 | { 39 | if (arr[i][0] == -1) 40 | { 41 | b->root = nodes[i]; 42 | } 43 | else 44 | { 45 | if (arr[i][1] == 0) 46 | { 47 | nodes[arr[i][0]]->left = nodes[i]; 48 | } 49 | else 50 | { 51 | nodes[arr[i][0]]->right = nodes[i]; 52 | } 53 | } 54 | } 55 | return b; 56 | } 57 | 58 | void delete_BT(BT_Node *curr) 59 | { 60 | if (curr == NULL) 61 | { 62 | return; 63 | } 64 | delete_BT(curr->left); 65 | delete_BT(curr->right); 66 | free(curr); 67 | } 68 | 69 | void inorder(BT_Node *root) 70 | { 71 | if (root != NULL) 72 | { 73 | inorder(root->left); 74 | printf("%d ", root->val); 75 | inorder(root->right); 76 | } 77 | } 78 | 79 | void preorder(BT_Node *root) 80 | { 81 | if (root != NULL) 82 | { 83 | printf("%d ", root->val); 84 | preorder(root->left); 85 | preorder(root->right); 86 | } 87 | } 88 | 89 | void postorder(BT_Node *root) 90 | { 91 | if (root != NULL) 92 | { 93 | postorder(root->left); 94 | postorder(root->right); 95 | printf("%d ", root->val); 96 | } 97 | } 98 | 99 | // Taking input in specified format and creating a binary tree 100 | int main() 101 | { 102 | int n; 103 | scanf("%d", &n); 104 | int **arr = malloc(n * sizeof(int *)); 105 | for (int i = 0; i < n; ++i) 106 | { 107 | arr[i] = malloc(3 * sizeof(int)); 108 | } 109 | 110 | for (int i = 0; i < n; ++i) 111 | { 112 | int p, c, val; 113 | scanf("%d %d %d", &p, &c, &val); 114 | arr[i][0] = p; 115 | arr[i][1] = c; 116 | arr[i][2] = val; 117 | } 118 | BT *b = create_BT(arr, n); 119 | // inorder(b->root); 120 | printf("\n"); 121 | // preorder(b->root); 122 | printf("\n"); 123 | postorder(b->root); 124 | printf("\n"); 125 | // delete_BT(b->root); 126 | // free(b); 127 | } 128 | -------------------------------------------------------------------------------- /delete_in_doubly_linkedlist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct node 6 | { 7 | struct node *prev; 8 | int data; 9 | struct node *next; 10 | }; 11 | 12 | typedef struct My_dll my_dll; 13 | struct My_dll 14 | { 15 | struct node *head; // head=root 16 | }; 17 | 18 | // Deleting a node from linkedlist 19 | struct My_dll *delete (my_dll *list, int position) 20 | { 21 | struct node *head = list->head; 22 | struct node *temp = head; 23 | struct node *temp2 = NULL; 24 | 25 | if (position == 0) 26 | { 27 | if (head->next == 0) 28 | { 29 | 30 | list->head = 0; 31 | } 32 | else 33 | { 34 | struct node *temp = head; 35 | head = head->next; 36 | list->head = head; 37 | list->head->prev = NULL; 38 | free(temp); 39 | } 40 | return list; 41 | } 42 | 43 | while (position > 0) 44 | { 45 | temp = temp->next; 46 | position--; 47 | } 48 | if (temp->next == NULL) 49 | { 50 | struct node *temp = head; 51 | struct node *temp2; 52 | while (temp->next != NULL) 53 | { 54 | temp = temp->next; 55 | } 56 | temp2 = temp->prev; 57 | temp2->next = NULL; 58 | free(temp); 59 | return list; 60 | } 61 | else 62 | { 63 | temp2 = temp->prev; 64 | temp2->next = temp->next; 65 | temp->next->prev = temp2; 66 | free(temp); 67 | temp = NULL; 68 | } 69 | return list; 70 | } 71 | 72 | // Deleting alternate nodes from the linkedlist 73 | 74 | void prune(my_dll *list) 75 | { 76 | struct node *head = list->head; 77 | int temp = get_size(list) - 1; 78 | if (temp % 2 == 0) 79 | { 80 | temp--; 81 | } 82 | for (int i = temp; i >= 0; i -= 2) 83 | { 84 | list = delete (list, i); 85 | } 86 | } 87 | 88 | void print(my_dll *list) 89 | { 90 | struct node *head = list->head; 91 | 92 | struct node *ptr; 93 | ptr = head; 94 | while (ptr != NULL) 95 | { 96 | printf("%d ", ptr->data); 97 | ptr = ptr->next; 98 | } 99 | printf("\n"); 100 | } 101 | 102 | int main() 103 | { 104 | struct My_dll *list; 105 | list = (struct My_dll *)malloc(sizeof(struct My_dll)); 106 | list->head = NULL; 107 | while (1) 108 | { 109 | char name[100]; 110 | scanf("%s", name); 111 | if (strcmp(name, "delete") == 0) 112 | { 113 | int index; 114 | scanf("%d", &index); 115 | list = delete (list, index); 116 | } 117 | else if (strcmp(name, "print") == 0) 118 | { 119 | print(list); 120 | } 121 | else if (strcmp(name, "prune") == 0) 122 | { 123 | prune(list); 124 | } 125 | else if (strcmp(name, "exit") == 0) 126 | { 127 | return 0; 128 | } 129 | } 130 | return 0; 131 | } 132 | -------------------------------------------------------------------------------- /insert_in_doubly_linkedlist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | struct node 6 | { 7 | struct node *prev; 8 | int data; 9 | struct node *next; 10 | }; 11 | 12 | typedef struct My_dll my_dll; 13 | struct My_dll 14 | { 15 | struct node *head; // head=root 16 | }; 17 | 18 | // Insert at the end of the linked list. 19 | 20 | struct My_dll *insert(my_dll *list, int data) 21 | { 22 | struct node *head = list->head; 23 | struct node *temp = malloc(sizeof(struct node)); 24 | if (head == NULL) 25 | { 26 | head = temp; 27 | temp->data = data; 28 | list->head = head; 29 | return list; 30 | } 31 | temp->prev = NULL; 32 | temp->data = data; 33 | temp->next = NULL; 34 | struct node *tp; 35 | tp = head; 36 | while (tp->next != NULL) 37 | { 38 | tp = tp->next; 39 | } 40 | tp->next = temp; 41 | temp->prev = tp; 42 | return list; 43 | } 44 | 45 | // Insert at any position in the linked list. 46 | 47 | struct My_dll *insert_at(my_dll *list, int data, int position) 48 | { 49 | struct node *head = list->head; 50 | struct node *temp = malloc(sizeof(struct node)); 51 | struct node *newp = NULL; 52 | // struct My_dll*new_node; 53 | newp = (struct node *)malloc(sizeof(struct node)); 54 | temp = head; 55 | // struct My_dll *temp2 = NULL; 56 | if (position == 0) 57 | { 58 | newp->next = head; 59 | head->prev = newp; 60 | head = newp; 61 | newp->data = data; 62 | newp->prev = NULL; 63 | list->head = head; 64 | return list; 65 | } 66 | for (int i = 0; i < position - 1; i++) 67 | temp = temp->next; 68 | newp->data = data; 69 | newp->next = temp->next; 70 | temp->next = newp; 71 | if (newp->next) 72 | newp->next->prev = newp; 73 | newp->prev = temp; 74 | return list; 75 | } 76 | 77 | // Print the linked list. 78 | 79 | void print(my_dll *list) 80 | { 81 | struct node *head = list->head; 82 | 83 | struct node *ptr; 84 | ptr = head; 85 | while (ptr != NULL) 86 | { 87 | printf("%d ", ptr->data); 88 | ptr = ptr->next; 89 | } 90 | printf("\n"); 91 | } 92 | 93 | int main() 94 | { 95 | struct My_dll *list; 96 | list = (struct My_dll *)malloc(sizeof(struct My_dll)); 97 | list->head = NULL; 98 | while (1) 99 | { 100 | char name[100]; 101 | scanf("%s", name); 102 | if (strcmp(name, "insert") == 0) 103 | { 104 | int n; 105 | scanf("%d", &n); 106 | list = insert(list, n); 107 | } 108 | else if (strcmp(name, "insert_at") == 0) 109 | { 110 | int m; 111 | int idx; 112 | scanf("%d %d", &m, &idx); 113 | list = insert_at(list, m, idx); 114 | } 115 | else if (strcmp(name, "print") == 0) 116 | { 117 | print(list); 118 | } 119 | else if (strcmp(name, "exit") == 0) 120 | { 121 | return 0; 122 | } 123 | } 124 | return 0; 125 | } 126 | -------------------------------------------------------------------------------- /Game.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | void main() 5 | { 6 | start: int a[3][3]={5,6,7,8,9,10,11,12,13},i,j,k,x,y,t,cn=0,co=0; 7 | char c; 8 | static int nod=0,nn=0,sc[2]={0,0}; 9 | nod++; 10 | clrscr(); 11 | int driver=DETECT,mode; 12 | initgraph(&driver,&mode,"c:\tc\bgi"); 13 | setbkcolor(RED); 14 | line(100,200,400,200); 15 | line(100,300,400,300); 16 | line(200,100,200,400); 17 | line(300,100,300,400); 18 | gotoxy(20,2); 19 | cout<<"TIC TAC TOE"; 20 | gotoxy(20,3); 21 | cout<<"Unregistered Vesion 1.0"; 22 | gotoxy(10,5);cout<<" BOX NO "; 23 | gotoxy(15,7);cout<<"0"; 24 | gotoxy(30,7);cout<<"1"; 25 | gotoxy(45,7);cout<<"2"; 26 | gotoxy(15,14);cout<<"3"; 27 | gotoxy(30,14);cout<<"4"; 28 | gotoxy(45,14);cout<<"5"; 29 | gotoxy(15,21);cout<<"6"; 30 | gotoxy(30,21);cout<<"7"; 31 | gotoxy(45,21);cout<<"8"; 32 | gotoxy(55,22);cout<<"RESULT"; 33 | gotoxy(55,23);cout<<"*********"; 34 | gotoxy(55,15);cout<<"P1-"< 2 | #include 3 | #include 4 | 5 | typedef long long int element; 6 | 7 | struct heap 8 | { 9 | long long int count; 10 | long long int size; 11 | element *heapparr; 12 | }; 13 | 14 | typedef struct heap *PriorQueue; 15 | 16 | long long int *heap; 17 | long long int size; 18 | long long int count; 19 | long long int init_size = 300005; 20 | 21 | PriorQueue heap_init(PriorQueue h) 22 | { 23 | h = (PriorQueue)malloc(sizeof(struct heap)); 24 | h->heapparr = (element *)malloc(sizeof(long long int) * (init_size)); 25 | h->count = 0; 26 | h->size = init_size; 27 | return h; 28 | } 29 | 30 | void min_heapify(element *data, long long int loc, long long int count) 31 | { 32 | long long int left, largest, right, temp; 33 | left = 2 * loc + 1; 34 | right = left + 1; 35 | largest = loc; 36 | 37 | if (left <= count && data[left] <= data[largest]) 38 | { 39 | largest = left; 40 | } 41 | 42 | if (right <= count && data[right] <= data[largest]) 43 | { 44 | largest = right; 45 | } 46 | 47 | if (largest != loc) 48 | { 49 | temp = data[loc]; 50 | data[loc] = data[largest]; 51 | data[largest] = temp; 52 | min_heapify(data, largest, count); 53 | } 54 | } 55 | 56 | void swap(long long int *a, long long int *b) 57 | { 58 | int temp = *a; 59 | *a = *b; 60 | *b = temp; 61 | } 62 | 63 | void insertmin(element x, PriorQueue h) 64 | { 65 | long long int idx, parent; 66 | idx = h->count++; 67 | for (; idx; idx = parent) 68 | { 69 | parent = (idx - 1) / 2; 70 | if (h->heapparr[parent] <= x) 71 | { 72 | break; 73 | } 74 | h->heapparr[idx] = h->heapparr[parent]; 75 | } 76 | h->heapparr[idx] = x; 77 | } 78 | 79 | element deletemin(PriorQueue h) 80 | { 81 | element removed; 82 | long long int temp = h->heapparr[--h->count]; 83 | if (h->count <= (h->size + 2) && (h->size > init_size)) 84 | { 85 | h->size -= 1; 86 | h->heapparr = realloc(h->heapparr, sizeof(element) * h->size); 87 | // if (!h->heapparr) 88 | // { 89 | // printf("E3!\n"); 90 | // return 0; 91 | // } 92 | } 93 | removed = h->heapparr[0]; 94 | h->heapparr[0] = temp; 95 | min_heapify(h->heapparr, 0, h->count); 96 | return removed; 97 | } 98 | 99 | int main() 100 | { 101 | PriorQueue h = NULL; 102 | h = heap_init(h); 103 | long long int l, d; 104 | scanf("%lld %lld", &l, &d); 105 | long long int height[d]; 106 | for (int i = 0; i < d; i++) 107 | { 108 | scanf("%lld", &height[i]); 109 | insertmin(height[i], h); 110 | } 111 | // printf("%lld\n", h->count); 112 | if (h->count == 1) 113 | { 114 | printf("0\n"); 115 | return 0; 116 | } 117 | long long int res = 0; 118 | while (h->count > 1) 119 | { 120 | long long int first = deletemin(h); 121 | long long int second = deletemin(h); 122 | res += first + second; 123 | insertmin(first + second, h); 124 | } 125 | printf("%lld\n", res); 126 | return 0; 127 | } 128 | -------------------------------------------------------------------------------- /stack.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int top = -1; //Since the stack is not yet created, initialized the top of the stack as -1 since it doesnt exist yet. 5 | 6 | void push(int la[], int x) //Function to add elements on top of the stack 7 | { 8 | 9 | if (top >= x - 1) 10 | { 11 | printf("Stack is overflow\n\n"); 12 | } 13 | else 14 | { 15 | int item; 16 | printf("Enter a value to be pushed : "); 17 | scanf("%d", &item); 18 | top++; //Before adding the item first increment top variable so element is added at the next position. 19 | la[top] = item; //Added the item in the respective place. 20 | } 21 | printf("\n"); 22 | } 23 | 24 | void pop(int la[]) //Function to remove elements from top of the stack. 25 | { 26 | 27 | if (top <= -1) //If the top value is -1, this means that stack is empty and there is nothing to delete. This is called underflow condtion. 28 | { 29 | printf("Stack is underflow\n\n"); 30 | } 31 | else //If the stack is not empty. 32 | { 33 | printf("The popped element is " "%d" "\n\n", la[top]);//Print the element which is deleted. 34 | top--;//Decrement the top value since the previous top item is removed 35 | } 36 | } 37 | 38 | int peek(int la[]) 39 | { 40 | 41 | if (top == -1) 42 | { 43 | printf("Underflow\n\n"); 44 | return 0; 45 | } 46 | else 47 | { 48 | printf("The top element is " 49 | "%d" 50 | "\n\n", 51 | la[top]); 52 | return la[top]; 53 | } 54 | } 55 | 56 | void display(int la[]) 57 | { 58 | 59 | if (top >= 0) 60 | { 61 | printf("Stack : "); 62 | for (int i = top; i >= 0; i--) 63 | { 64 | printf("%d ", la[i]); 65 | } 66 | printf("\n\n"); 67 | } 68 | else 69 | { 70 | printf("The stack is empty.\n\n"); 71 | } 72 | } 73 | 74 | int main() 75 | { 76 | 77 | int stack[100]; 78 | int m; 79 | printf("Enter the no. of elements in the stack (1 - 100) : "); 80 | scanf(" %d", &m); 81 | printf("\n"); 82 | 83 | int a = 0; 84 | while (a < 1) 85 | { 86 | int response; 87 | printf("Stack Operations : Push = 1 Pop = 2 Peek = 3 Display = 4 End = 5\n\n"); 88 | printf("Enter your choice = "); 89 | scanf(" %d", &response); 90 | switch (response) 91 | { 92 | case 1: 93 | { 94 | push(stack, m); 95 | break; 96 | } 97 | 98 | case 2: 99 | { 100 | pop(stack); 101 | break; 102 | } 103 | 104 | case 3: 105 | { 106 | peek(stack); 107 | break; 108 | } 109 | 110 | case 4: 111 | { 112 | display(stack); 113 | break; 114 | } 115 | 116 | case 5: 117 | { 118 | a++; 119 | break; 120 | } 121 | 122 | default: 123 | { 124 | printf("Invalid input. Please try again.\n\n"); 125 | break; 126 | } 127 | } 128 | } 129 | printf("\n"); 130 | 131 | return 0; 132 | } 133 | -------------------------------------------------------------------------------- /klee-algo.c: -------------------------------------------------------------------------------- 1 | // Klee's Algorithm by Roshin N. October 2021. @GetPsyched6 2 | 3 | /* 4 | Klee’s rule finds a union of line segments lying horizontally. 5 | Example: If we have 3 line segments with co-ords (2,6) (5,8) and (9,12) 6 | we can find the total length combined of these segments and the largest continuous segment 7 | by using Klee's Algorithm. Lines (2,6) and (5,8) are continuous hence it's length is 6. 8 | The total length of all three line segments is 9; From 2 to 8 >> [6] (continuous segment) 9 | and From 9 to 12 >> [3] we add these and get 6 + 3 = 9. 10 | You can verify by running the program. Enter 3 line segments, co-ords 2,6 | 5,8 | 9,12 11 | and the results should be 9 for union and 6 for continuous max length. 12 | You can test it out with different inputs too. 13 | */ 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | void sort(int b[20][20], int n) 22 | { 23 | int i, x[2][2]; 24 | for (i = 1; i < 2 * n; i++) 25 | { 26 | int j; 27 | x[0][0] = b[i][0]; 28 | x[0][1] = b[i][1]; 29 | j = i - 1; 30 | while (j >= 0 && b[j][0] > x[0][0]) 31 | { 32 | b[j + 1][0] = b[j][0]; 33 | b[j + 1][1] = b[j][1]; 34 | j = j - 1; 35 | } 36 | b[j + 1][0] = x[0][0]; 37 | b[j + 1][1] = x[0][1]; 38 | } 39 | int c[10][10]; 40 | for (i = 0; i < 2 * n; i++) 41 | { 42 | if (b[i][0] == b[i + 1][0] && (char)b[i][1] == 'T') 43 | { 44 | c[i][0] = b[i][0]; 45 | c[i][1] = b[i][1]; 46 | b[i][0] = b[i + 1][0]; 47 | b[i][1] = b[i + 1][1]; 48 | b[i + 1][0] = c[i][0]; 49 | b[i + 1][1] = c[i][1]; 50 | } 51 | } 52 | } 53 | void main() 54 | { 55 | int result = 0, max_length = 0, counter = 0, n = 0, x, y = 0, unions = 0; 56 | int a[20][20], b[20][20], c[20][20]; 57 | printf("Enter number of line segments: \n"); 58 | scanf(" %d", &n); 59 | for (int i = 0; i < n; i++) 60 | { 61 | printf("Enter segment %d's x and y co-ordinate (input as:x y): \n", i + 1); 62 | scanf(" %d", &a[i][0]); 63 | scanf(" %d", &a[i][1]); 64 | } 65 | for (int i = 0; i < n; i++) 66 | { 67 | if (i == 0) 68 | x = i; 69 | else 70 | x = x + 1; 71 | b[x][0] = a[i][0]; 72 | b[x][1] = 'F'; 73 | b[++x][0] = a[i][1]; 74 | b[x][1] = 'T'; 75 | } 76 | sort(b, n); 77 | for (int i = 0; i < 2 * n; i++) 78 | { 79 | if (counter > 0) 80 | { 81 | result += b[i][0] - b[i - 1][0]; 82 | } 83 | if ((char)b[i][1] == 'F') 84 | { 85 | counter++; 86 | } 87 | else if ((char)b[i][1] == 'T') 88 | { 89 | counter--; 90 | } 91 | if (counter == 0 && y == 0) 92 | { 93 | max_length = result; 94 | result = 0; 95 | y++; 96 | } 97 | } 98 | unions = max_length + result; 99 | printf("Length of union of segments of a line is %d\n", unions); 100 | printf("Maximum Length of continuous segments of line is %d", max_length); 101 | } 102 | -------------------------------------------------------------------------------- /BST_deletion.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | typedef struct BST{ 6 | 7 | int data; 8 | struct BST *lc; 9 | struct BST *rc; 10 | 11 | }node; 12 | 13 | node * newNode(int el){ 14 | 15 | node *new = (node*)calloc(1,sizeof(node)); 16 | 17 | new->data = el; 18 | new->lc = NULL; 19 | new->rc = NULL; 20 | 21 | return new; 22 | } 23 | 24 | void insertNode(node **root, int el){ 25 | 26 | if((*root) == NULL){ 27 | (*root) = newNode(el); 28 | } 29 | 30 | else if(el > (*root)->data){ 31 | insertNode(&((*root)->rc), el); 32 | } 33 | else{ 34 | insertNode(&((*root)->lc), el); 35 | } 36 | 37 | } 38 | 39 | void inorder(node *root){ 40 | 41 | if(root != NULL){ 42 | inorder(root->lc); 43 | printf("%d ",root->data); 44 | inorder(root->rc); 45 | } 46 | 47 | } 48 | 49 | int findMaximum(node *root, int max){ 50 | 51 | if(root == NULL){ 52 | return max; 53 | } 54 | max = root->data; 55 | findMaximum(root->rc, max); 56 | 57 | } 58 | 59 | void search(node **root, int el); 60 | 61 | void deletion(node **root){ 62 | 63 | if((*root)->lc == NULL && (*root)->rc == NULL){ // 0 Children 64 | 65 | (*root) = NULL; 66 | 67 | } 68 | else if(!(*root)->lc || !(*root)->rc){ // 1 Child 69 | 70 | if(!(*root)->lc){ 71 | (*root) = (*root)->rc; 72 | } 73 | else{ 74 | (*root) = (*root)->lc; 75 | } 76 | 77 | } 78 | else{ // 2 Children 79 | 80 | int max = findMaximum((*root)->lc, INT_MIN); 81 | (*root)->data = max; 82 | 83 | search(&((*root)->lc), max); 84 | 85 | } 86 | 87 | } 88 | 89 | void search(node **root, int el){ 90 | 91 | if((*root) == NULL){ 92 | printf("No such element in the BST !!\n"); 93 | return; 94 | } 95 | else if(el > (*root)->data){ 96 | search(&((*root)->rc), el); 97 | } 98 | else if(el < (*root)->data){ 99 | search(&((*root)->lc), el); 100 | } 101 | else{ 102 | deletion(&(*root)); 103 | } 104 | 105 | } 106 | 107 | int main(){ 108 | 109 | node *root = NULL; 110 | 111 | int n, el, i, check = 1; 112 | printf("Number of elements ? "); 113 | scanf("%d",&n); 114 | 115 | printf("Enter %d elements : ",n); 116 | for (i = 0; i < n; i++){ 117 | scanf("%d",&el); 118 | insertNode(&root, el); 119 | } 120 | 121 | char option; 122 | printf("\nChoose option : (a) Delete an element from the BST , (b) Print the BST in inorder , (c) Ends the program\n\n"); 123 | 124 | while(check){ 125 | scanf(" %c",&option); 126 | switch(option){ 127 | 128 | case 'a': 129 | printf("Enter the element you want to delete : "); 130 | scanf("%d",&el); 131 | search(&root, el); 132 | break; 133 | case 'b': 134 | printf("Inorder: "); 135 | inorder(root); 136 | printf("\n"); 137 | break; 138 | case 'c': 139 | printf("\n__Program End__\n\n"); 140 | check = 0; 141 | break; 142 | } 143 | } 144 | 145 | } -------------------------------------------------------------------------------- /Tower_of_Hanoi.c: -------------------------------------------------------------------------------- 1 | // Tower of Hanoi: 2 | // -------------- 3 | 4 | // Tower of Hanoi is one study case we come across while learning Recursion.Tower of Hanoi is a simple game in which you are given 5 | // a task to move 'n' no of disks sized from smaller to bigger from top to bottom from Tower A to Tower C using an additional Tower B. 6 | // This game consists of few rules : 7 | 8 | // - You can only pick up single disk at a time. 9 | // - You should not place a bigger disk on the smaller disk. 10 | // - The end Tower(say Tower C) should contain all the disks in the same order as it was in Tower A at the beginning of the game. 11 | 12 | // To solve this problem, we divide it into small sub problem which has to do the same task repeatively. 13 | // Thus, it can be implemented through a recursive function. 14 | 15 | // Let's see a few cases, 16 | 17 | // Case 1: There only a single Disk on Tower A 18 | // ------ 19 | // Since there is only a single on Tower A, we just place it onto Tower B as it is. 20 | 21 | // Case 2: There are 2 Disks on Tower A. 22 | // ------ 23 | // It's not as simple as Case 1 but it still an easy solution, we just have to place the smallest disk to Tower B, then move the 24 | // Largest Disk to Tower C and then Finally from Tower B to Tower C. Thus, we have moved the Disk from Tower A to Tower C. 25 | 26 | // Case 3: There are 3 Disks on Tower A. 27 | // ------ 28 | // Now. from this case the task gets even complex. So, How do we move all the Disks on Tower A to Tower C by following all the rules ? 29 | // So, to Solve this case, we have to break down the problem into sub-problem like Moving the Top most 2 disks to Tower B and then 30 | // moving the Last/Largest Disk to Tower C & at last the 2 Disks from Tower B to Tower C. 31 | 32 | // So, how do we implement the case 3 we just discussed ? and as the rules say we can move only one disk at a time, then how can we shift 33 | // Top most of the 2 Disks from Tower A to Tower B & Tower B to Tower C ? These might be the question you might having right now. 34 | 35 | // As I have told you that we divide the larger problem into sub-problem, we divide the Moving 3 disks into moving 2 disk + moving 1 disk task, 36 | // Then we further break down the moving 2 disks problem into moving 1 disk + moving 1 disk task. But only thing which changes is the source 37 | // and destination Tower. 38 | 39 | // Try constructing the recursive tree diagram for even better understanding on the recursive calls that are helps us solve this puzzle. 40 | 41 | #include 42 | 43 | // Towers of Hanoi logic 44 | void TOH(int n_disks, char Tower_A, char Tower_B, char Tower_C) 45 | { 46 | if (n_disks > 0) 47 | { 48 | // first we make all n-1 disk are moved to Tower B using Tower C. 49 | TOH(n_disks - 1, Tower_A, Tower_C, Tower_B); 50 | 51 | // Then we move the Largest/Last Disk from Tower A to Tower C. 52 | printf("Move Disk %d: from Tower %c -> Tower %c\n", n_disks, Tower_A, Tower_C); 53 | 54 | // Finally we move all n-1 disk to Tower B using Tower C using Tower A. 55 | TOH(n_disks - 1, Tower_B, Tower_A, Tower_C); 56 | } 57 | 58 | return; 59 | } 60 | 61 | // Driver Program 62 | int main(void) 63 | { 64 | int no_of_disks = 3; 65 | char Tower_A = 'A', Tower_B = 'B', Tower_C = 'C'; 66 | TOH(no_of_disks, Tower_A, Tower_B, Tower_C); 67 | return 0; 68 | } 69 | 70 | // Output: 71 | // ------- 72 | 73 | // Move Disk 1: from Tower A -> Tower C 74 | // Move Disk 2: from Tower A -> Tower B 75 | // Move Disk 1: from Tower C -> Tower B 76 | // Move Disk 3: from Tower A -> Tower C 77 | // Move Disk 1: from Tower B -> Tower A 78 | // Move Disk 2: from Tower B -> Tower C 79 | // Move Disk 1: from Tower A -> Tower C -------------------------------------------------------------------------------- /DynamicArray.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct Array{ 4 | int capacity; 5 | int lastIndex; 6 | int *ptr; 7 | }; 8 | 9 | struct Array* createArray(int cap){ 10 | struct Array *arr; 11 | arr=(struct Array*)malloc(sizeof(struct Array)); 12 | arr->capacity=cap; 13 | arr->lastIndex=-1; 14 | arr->ptr=(int*)malloc(sizeof(int)*cap); 15 | return arr; 16 | } 17 | 18 | void doubleArray(struct Array *arr) 19 | { 20 | int *temp,i; 21 | temp=(int*)malloc(sizeof(int)*(arr->capacity)*2); 22 | for(i=0;i<=arr->lastIndex;i++) 23 | temp[i]=arr->ptr[i]; 24 | free(arr->ptr); 25 | arr->ptr=temp; 26 | arr->capacity*=2; 27 | } 28 | 29 | void halfArray(struct Array *arr) 30 | { 31 | realloc(arr->ptr,2); 32 | } 33 | 34 | void append(struct Array *arr,int data) 35 | { 36 | if(arr->capacity-1==arr->lastIndex) 37 | doubleArray(arr); //printf("overflow"); 38 | arr->lastIndex+=1; 39 | arr->ptr[arr->lastIndex]=data; 40 | 41 | } 42 | void setValue(struct Array *arr,int index,int data) 43 | { 44 | int i; 45 | if(index<0 || index > arr->lastIndex+1) 46 | printf("invalid index"); 47 | else if(arr->capacity-1==arr->lastIndex) 48 | doubleArray(arr); //printf("overflow"); 49 | for(i=arr->lastIndex;i>=index;i--) 50 | arr->ptr[i+1]=arr->ptr[i]; 51 | arr->ptr[index]=data; 52 | arr->lastIndex+=1; 53 | } 54 | 55 | void editValue(struct Array *arr,int index,int data) 56 | { 57 | if(index<0||index>arr->lastIndex) 58 | printf("invalid index"); 59 | else 60 | arr->ptr[index]=data; 61 | } 62 | 63 | int get(struct Array *arr,int index) 64 | { 65 | if(index<0 || index>arr->lastIndex) 66 | printf("invalid index"); 67 | else 68 | return arr->ptr[index]; 69 | } 70 | 71 | int count(struct Array *arr) 72 | { 73 | return arr->lastIndex+1; 74 | } 75 | 76 | void deleteValue(struct Array *arr,int index) 77 | { 78 | int i; 79 | if(arr->lastIndex==-1) 80 | printf("underflow"); 81 | else if(index<0 || index> arr->lastIndex) 82 | printf("invalid index"); 83 | else{ 84 | for(i=index;ilastIndex;i++) 85 | arr->ptr[i]=arr->ptr[i+1]; 86 | arr->lastIndex-=1; 87 | } 88 | } 89 | 90 | int menu() 91 | { 92 | int ch; 93 | printf("\n 1. insert an item "); 94 | printf("\n 2. append an item "); 95 | printf("\n 3. print Array value"); 96 | printf("\n 4. delete value"); 97 | printf("\n 5. count total items present "); 98 | printf("\n 6. edit values"); 99 | printf("\n 7. array capacity "); 100 | printf("\n 8. exit"); 101 | printf("\n\n Enter your choice: "); 102 | scanf("%d",&ch); 103 | return ch; 104 | } 105 | 106 | void main(){ 107 | int index,data,i; 108 | struct Array *arr; 109 | arr=createArray(10); 110 | while(1) 111 | { 112 | switch(menu()) 113 | { 114 | case 1: 115 | printf("enter index and data to insert "); 116 | scanf("%d%d",&index,&data); 117 | setValue(arr,index,data); 118 | break; 119 | case 2: 120 | printf("enter data to append "); 121 | scanf("%d",&data); 122 | append(arr,data); 123 | break; 124 | case 3: 125 | printf("\n "); 126 | for(i=0;icapacity); 144 | break; 145 | case 8: 146 | printf("Thankyou %c % c %c, press any key to exit....",2,2,2); 147 | getch(); 148 | exit(0); 149 | default: 150 | printf("Invalid Choice"); 151 | } 152 | getch(); 153 | system("cls"); 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /DoubleLinkedList.c: -------------------------------------------------------------------------------- 1 | // A complete working C program to 2 | // demonstrate all insertion 3 | // methods 4 | #include 5 | #include 6 | 7 | // A linked list node 8 | struct Node { 9 | int data; 10 | struct Node* next; 11 | struct Node* prev; 12 | }; 13 | 14 | /* Given a reference (pointer to pointer) to the head of a 15 | list and an int, inserts a new node on the front of the 16 | list. */ 17 | void push(struct Node** head_ref, int new_data) 18 | { 19 | /* 1. allocate node */ 20 | struct Node* new_node 21 | = (struct Node*)malloc(sizeof(struct Node)); 22 | 23 | /* 2. put in the data */ 24 | new_node->data = new_data; 25 | 26 | /* 3. Make next of new node as head and previous as NULL 27 | */ 28 | new_node->next = (*head_ref); 29 | new_node->prev = NULL; 30 | 31 | /* 4. change prev of head node to new node */ 32 | if ((*head_ref) != NULL) 33 | (*head_ref)->prev = new_node; 34 | 35 | /* 5. move the head to point to the new node */ 36 | (*head_ref) = new_node; 37 | } 38 | 39 | /* Given a node as prev_node, insert a new node after the 40 | * given node */ 41 | void insertAfter(struct Node* prev_node, int new_data) 42 | { 43 | /*1. check if the given prev_node is NULL */ 44 | if (prev_node == NULL) { 45 | printf("the given previous node cannot be NULL"); 46 | return; 47 | } 48 | 49 | /* 2. allocate new node */ 50 | struct Node* new_node 51 | = (struct Node*)malloc(sizeof(struct Node)); 52 | 53 | /* 3. put in the data */ 54 | new_node->data = new_data; 55 | 56 | /* 4. Make next of new node as next of prev_node */ 57 | new_node->next = prev_node->next; 58 | 59 | /* 5. Make the next of prev_node as new_node */ 60 | prev_node->next = new_node; 61 | 62 | /* 6. Make prev_node as previous of new_node */ 63 | new_node->prev = prev_node; 64 | 65 | /* 7. Change previous of new_node's next node */ 66 | if (new_node->next != NULL) 67 | new_node->next->prev = new_node; 68 | } 69 | 70 | /* Given a reference (pointer to pointer) to the head 71 | of a DLL and an int, appends a new node at the end */ 72 | void append(struct Node** head_ref, int new_data) 73 | { 74 | /* 1. allocate node */ 75 | struct Node* new_node 76 | = (struct Node*)malloc(sizeof(struct Node)); 77 | 78 | struct Node* last = *head_ref; /* used in step 5*/ 79 | 80 | /* 2. put in the data */ 81 | new_node->data = new_data; 82 | 83 | /* 3. This new node is going to be the last node, so 84 | make next of it as NULL*/ 85 | new_node->next = NULL; 86 | 87 | /* 4. If the Linked List is empty, then make the new 88 | node as head */ 89 | if (*head_ref == NULL) { 90 | new_node->prev = NULL; 91 | *head_ref = new_node; 92 | return; 93 | } 94 | 95 | /* 5. Else traverse till the last node */ 96 | while (last->next != NULL) 97 | last = last->next; 98 | 99 | /* 6. Change the next of last node */ 100 | last->next = new_node; 101 | 102 | /* 7. Make last node as previous of new node */ 103 | new_node->prev = last; 104 | 105 | return; 106 | } 107 | 108 | // This function prints contents of linked list starting 109 | // from the given node 110 | void printList(struct Node* node) 111 | { 112 | struct Node* last; 113 | printf("\nTraversal in forward direction \n"); 114 | while (node != NULL) { 115 | printf(" %d ", node->data); 116 | last = node; 117 | node = node->next; 118 | } 119 | 120 | printf("\nTraversal in reverse direction \n"); 121 | while (last != NULL) { 122 | printf(" %d ", last->data); 123 | last = last->prev; 124 | } 125 | } 126 | 127 | // Driver code 128 | int main() 129 | { 130 | /* Start with the empty list */ 131 | struct Node* head = NULL; 132 | 133 | // Insert 6. So linked list becomes 6->NULL 134 | append(&head, 6); 135 | 136 | // Insert 7 at the beginning. So linked list becomes 137 | // 7->6->NULL 138 | push(&head, 7); 139 | 140 | // Insert 1 at the beginning. So linked list becomes 141 | // 1->7->6->NULL 142 | push(&head, 1); 143 | 144 | // Insert 4 at the end. So linked list becomes 145 | // 1->7->6->4->NULL 146 | append(&head, 4); 147 | 148 | // Insert 8, after 7. So linked list becomes 149 | // 1->7->8->6->4->NULL 150 | insertAfter(head->next, 8); 151 | 152 | printf("Created DLL is: "); 153 | printList(head); 154 | 155 | getchar(); 156 | return 0; 157 | } 158 | -------------------------------------------------------------------------------- /DQueueLinkedList.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node{ 5 | struct node *prev; 6 | int item; 7 | struct node *next; 8 | }; 9 | 10 | void insertTail(struct node **h,struct node **t,int data) 11 | { 12 | struct node *temp; 13 | temp=(struct node*)malloc(sizeof(struct node)); 14 | temp->item=data; 15 | temp->next=NULL; 16 | temp->prev=NULL; 17 | if((*h)==NULL&&(*t)==NULL) 18 | { 19 | *h=temp; 20 | *t=temp; 21 | } 22 | else 23 | { 24 | temp->prev=*t; 25 | (*t)->next=temp; 26 | (*t)=temp; 27 | } 28 | } 29 | 30 | void insertHead(struct node **h,struct node **t,int data) 31 | { 32 | struct node *temp; 33 | temp=(struct node*)malloc(sizeof(struct node)); 34 | temp->prev=NULL; 35 | temp->next=NULL; 36 | temp->item=data; 37 | 38 | if((*h)==NULL&&(*t)==NULL) 39 | { 40 | *h=temp; 41 | *t=temp; 42 | } 43 | else 44 | { 45 | temp->next=*h; 46 | (*h)->prev=temp; 47 | *h=temp; 48 | } 49 | } 50 | 51 | void deletionHead(struct node **h,struct node **t) 52 | { 53 | struct node *r; 54 | r=*h; 55 | if((*h)==NULL&&(*t)==NULL) 56 | { 57 | printf("Queue is Empty\n"); 58 | } 59 | else if(*h==*t) 60 | { 61 | free(*h); 62 | free(*t); 63 | *h=NULL; 64 | *t=NULL; 65 | } 66 | else 67 | { 68 | (*h)=(*h)->next; 69 | (*h)->prev=NULL; 70 | free(r); 71 | 72 | } 73 | } 74 | 75 | void deletionTail(struct node **h,struct node **t) 76 | { 77 | struct node *r; 78 | r=*t; 79 | if((*h)==NULL&&(*t)==NULL) 80 | printf("Queue is Empty\n"); 81 | else if(*h==*t) 82 | { 83 | free(*h); 84 | free(*t); 85 | *h=NULL; 86 | *t=NULL; 87 | } 88 | else 89 | { 90 | (*t)=(*t)->prev; 91 | (*t)->next=NULL; 92 | free(r); 93 | } 94 | } 95 | 96 | struct node* search(struct node *h,int data) 97 | { 98 | while(h){ 99 | if(h->item==data) 100 | return h; 101 | h=h->next; 102 | } 103 | return NULL; 104 | } 105 | 106 | void viewFirst(struct node *h,struct node *t) 107 | { 108 | while(h) 109 | { 110 | printf("%d \t",h->item); 111 | h=h->next; 112 | } 113 | } 114 | 115 | void viewLast(struct node *h,struct node *t) 116 | { 117 | while(t) 118 | { 119 | printf("%d \t",t->item); 120 | t=t->prev; 121 | } 122 | } 123 | int menu() 124 | { 125 | int ch; 126 | printf("\n\n 1. insert an item at Head "); 127 | printf("\n 2. insert an item at Tail "); 128 | printf("\n 3. delete node at Tail "); 129 | printf("\n 4. delete node at Head "); 130 | printf("\n 5. view at Head to Tail "); 131 | printf("\n 6. view at Tail to Head "); 132 | printf("\n 7. search node"); 133 | printf("\n 8. exit"); 134 | printf("\n\n Enter your choice: "); 135 | scanf("%d",&ch); 136 | return ch; 137 | } 138 | 139 | 140 | void main() 141 | { 142 | struct node *head=NULL; 143 | struct node *tail=NULL; 144 | struct node *temp; 145 | while(1) 146 | { 147 | switch(menu()) 148 | { int data; 149 | case 1: { 150 | int i,n; 151 | printf("How many element you want to insert = "); 152 | scanf("%d",&n); 153 | printf("\n enter data to be inserted at head \n "); 154 | for(i=1;i<=n;i++){ 155 | scanf("%d",&data); 156 | insertHead(&head,&tail,data); 157 | } 158 | break; 159 | } 160 | 161 | case 2: { 162 | int i,n; 163 | printf("How many element you want to insert = "); 164 | scanf("%d",&n); 165 | printf(" enter data to be inserted at Tail \n "); 166 | for(i=1;i<=n;i++){ 167 | scanf("%d",&data); 168 | insertTail(&head,&tail,data); 169 | } 170 | break;} 171 | 172 | case 3: 173 | deletionTail(&head,&tail); 174 | break; 175 | 176 | case 4: 177 | deletionHead(&head,&tail); 178 | break; 179 | 180 | case 5: 181 | viewFirst(head,tail); 182 | break; 183 | case 6: 184 | viewLast(head,tail); 185 | break; 186 | 187 | case 7: 188 | { 189 | struct node *temp; 190 | printf(" enter data you want to search \n "); 191 | scanf("%d",&data); 192 | temp=search(head,data); 193 | if(temp==NULL) 194 | { 195 | printf(" no such node found %c %c \n",2,2); 196 | } 197 | else 198 | printf(" data %d is found ",temp->item); 199 | break; 200 | } 201 | 202 | case 8: 203 | exit(0); 204 | } 205 | } 206 | } 207 | -------------------------------------------------------------------------------- /DoublyLinkedList.c: -------------------------------------------------------------------------------- 1 | //doubly linked list 2 | 3 | #include 4 | #include 5 | struct node 6 | { 7 | int data; 8 | struct node *next; 9 | struct node *prev; 10 | } *head = NULL; 11 | 12 | int size() 13 | { 14 | int c = 1; 15 | struct node *p = head; 16 | if (head == NULL) 17 | { 18 | printf("Linked List is empty."); 19 | return 0; 20 | } 21 | while (p->next != NULL) 22 | { 23 | c++; 24 | p = p->next; 25 | } 26 | return c; 27 | } 28 | 29 | void display() 30 | { 31 | struct node *p = head; 32 | while (p->next != NULL) 33 | { 34 | printf("%d <-> ", p->data); 35 | p = p->next; 36 | } 37 | 38 | printf("%d", p->data); 39 | } 40 | 41 | void insertHead(int data) 42 | { 43 | struct node *new = (struct node *)malloc(sizeof(struct node)); 44 | struct node *p = head; 45 | new->data = data; 46 | new->prev = NULL; 47 | new->next = head; 48 | head = new; 49 | } 50 | 51 | void insertTail(int data) 52 | { 53 | struct node *new = (struct node *)malloc(sizeof(struct node)); 54 | if (head == NULL) 55 | { 56 | insertHead(data); 57 | return; 58 | } 59 | struct node *p = head; 60 | while (p->next != NULL) 61 | p = p->next; 62 | new->data = data; 63 | p->next = new; 64 | new->prev = p; 65 | new->next = NULL; 66 | } 67 | 68 | void insertPos(int pos, int data) 69 | { 70 | struct node *new = (struct node *)malloc(sizeof(struct node)); 71 | if (pos == 1) 72 | { 73 | insertHead(data); 74 | return; 75 | } 76 | new->data = data; 77 | struct node *p = head; 78 | struct node *q = head; 79 | for (int i = 1; i < pos - 1; i++) 80 | { 81 | p = p->next; 82 | } 83 | for (int i = 1; i < pos; i++) 84 | { 85 | q = q->next; 86 | } 87 | p->next = new; 88 | new->prev = p; 89 | new->next = q; 90 | q->prev = new; 91 | } 92 | 93 | void delHead() 94 | { 95 | struct node *p = head; 96 | struct node *q = head->next; 97 | free(p); 98 | head = q; 99 | q->prev = NULL; 100 | } 101 | 102 | void delTail() 103 | { 104 | struct node *p = head; 105 | struct node *q = head; 106 | while (p->next != NULL) 107 | p = p->next; 108 | for (int i = 1; i < size() - 1; i++) 109 | q = q->next; 110 | q->prev = p->prev; 111 | free(p); 112 | q->next = NULL; 113 | } 114 | 115 | void delPos(int pos) 116 | { 117 | if (pos == 1) 118 | { 119 | delHead(); 120 | return; 121 | } 122 | if (pos == size()) 123 | { 124 | delTail(); 125 | return; 126 | } 127 | struct node *p = head; 128 | struct node *q = head; 129 | for (int i = 1; i < pos - 1; i++) 130 | { 131 | p = p->next; 132 | } 133 | for (int i = 1; i <= pos; i++) 134 | { 135 | q = q->next; 136 | } 137 | free(p->next); 138 | p->next = q; 139 | q->prev = p; 140 | } 141 | 142 | int main() 143 | { 144 | printf("a. Display the elements of the linked list\n"); 145 | printf("b. Insert an element at the beginning of the list\n"); 146 | printf("c. Insert an element at the end of the list\n"); 147 | printf("d. Insert an element at a specified position in the list\n"); 148 | printf("e. Deletion of an element from the beginning of the list\n"); 149 | printf("f. Deletion of an element from the end of the list\n"); 150 | printf("g. Deletion of an element from a specified position in the list\n"); 151 | while (1) 152 | { 153 | int data,pos; 154 | char choice; 155 | scanf(" %c", &choice); 156 | switch (choice) 157 | { 158 | case 'a': 159 | display(); 160 | break; 161 | case 'b': 162 | 163 | scanf("%d", &data); 164 | insertHead(data); 165 | break; 166 | case 'c': 167 | 168 | scanf("%d", &data); 169 | insertTail(data); 170 | break; 171 | case 'd': 172 | 173 | scanf("%d %d", &pos, &data); 174 | insertPos(pos, data); 175 | break; 176 | case 'e': 177 | delHead(); 178 | break; 179 | case 'f': 180 | delTail(); 181 | break; 182 | case 'g': 183 | 184 | scanf("%d", &pos); 185 | delPos(pos); 186 | break; 187 | default: 188 | printf("!!!ERROR!!!"); 189 | return 0; 190 | } 191 | } 192 | 193 | return 0; 194 | } -------------------------------------------------------------------------------- /d_linked_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct node 4 | { 5 | int data; 6 | struct node *next; 7 | struct node *prev; 8 | }*start=NULL; 9 | void add_begin(); 10 | void display(); 11 | void insert_end(); 12 | void insert_pos(); 13 | void count_node(); 14 | void del_first(); 15 | void del_last(); 16 | void del_pos(); 17 | int main() 18 | { 19 | int ch; 20 | printf("Enter 1 to insert at the beginning.\n"); 21 | printf("Enter 2 to insert at the end.\n"); 22 | printf("Enter 3 to insert position wise.\n"); 23 | printf("Enter 4 to count number of node& display the data.\n"); 24 | printf("Enter 5 to Delete at the beginning.\n"); 25 | printf("Enter 6 to delete at the end.\n"); 26 | printf("Enter 7 to delete position wise.\n"); 27 | printf("Enter 8 to display the linked list.\n"); 28 | printf("Enter any other number to exit.\n"); 29 | while(1) 30 | { 31 | printf("Enter your choice: "); 32 | scanf("%d",&ch); 33 | switch(ch) 34 | { 35 | case 1: 36 | add_begin(); 37 | break; 38 | case 2: 39 | insert_end(); 40 | break; 41 | case 3: 42 | insert_pos(); 43 | break; 44 | case 4: 45 | count_node(); 46 | break; 47 | case 5: 48 | del_first(); 49 | break; 50 | case 6: 51 | del_last(); 52 | break; 53 | case 7: 54 | del_pos(); 55 | break; 56 | case 8: 57 | display(); 58 | break; 59 | default: 60 | exit(0); 61 | } 62 | } 63 | return 0; 64 | } 65 | void add_begin() 66 | { 67 | struct node *temp; 68 | temp=(struct node *)malloc(sizeof(struct node)); 69 | printf("Enter the data: "); 70 | scanf("%d",&temp->data); 71 | temp->prev=NULL; 72 | if(start==NULL) 73 | { 74 | temp->next=NULL; 75 | } 76 | else 77 | { 78 | temp->next=start; 79 | start->prev=temp; 80 | } 81 | start=temp; 82 | } 83 | void insert_end() 84 | { 85 | struct node *tmp,*q; 86 | if(start==NULL) 87 | { 88 | printf("No node present.\n"); 89 | return; 90 | } 91 | q=start; 92 | while(q->next!=NULL) 93 | q=q->next; 94 | tmp=(struct node *)malloc(sizeof(struct node)); 95 | printf("Enter the data: "); 96 | scanf("%d",&tmp->data); 97 | tmp->next=q->next; 98 | tmp->prev=q; 99 | q->next=tmp; 100 | } 101 | void insert_pos() 102 | { 103 | struct node *tmp,*q; 104 | if(start==NULL) 105 | { 106 | printf("No node present.\n"); 107 | return; 108 | } 109 | q=start; 110 | int pos; 111 | printf("Enter the position: \n"); 112 | scanf("%d",&pos); 113 | for(int i=1;inext; 115 | tmp=(struct node *)malloc(sizeof(struct node)); 116 | printf("Enter the data: "); 117 | scanf("%d",&tmp->data); 118 | tmp->next=q->next; 119 | tmp->prev=q; 120 | q->next->prev=tmp; 121 | q->next=tmp; 122 | } 123 | void count_node() 124 | { 125 | int a=0; 126 | struct node *f; 127 | f=start; 128 | while(f->next!=NULL) 129 | { 130 | f=f->next; 131 | a++; 132 | } 133 | printf("No. of nodes present is: %d\n",a+1); 134 | } 135 | void del_first() 136 | { 137 | struct node *temp; 138 | temp=start; 139 | start=start->next; 140 | start->prev=NULL; 141 | free(temp); 142 | printf("Node deleted sucessfully...\n"); 143 | } 144 | void del_last() 145 | { 146 | struct node *temp,*p; 147 | temp=start; 148 | p=start; 149 | while(p->next!=NULL) 150 | { 151 | temp=p; 152 | p=p->next; 153 | } 154 | temp->next=NULL; 155 | temp->prev=p; 156 | free(p); 157 | printf("Last node deleted successfully...\n"); 158 | } 159 | void del_pos() 160 | { 161 | struct node *temp,*p; 162 | int x; 163 | printf("Enter the position you want to delete: "); 164 | scanf("%d",&x); 165 | p=start; 166 | x=x-1; 167 | for(int i=1;inext; 169 | temp=p->next; 170 | p->next=temp->next; 171 | if(p->next!=NULL) 172 | p->next->prev=p; 173 | free(temp); 174 | printf("Node deleted successfully...\n"); 175 | } 176 | void display() 177 | { 178 | struct node *p; 179 | if(start==NULL) 180 | { 181 | printf("No node present.\n"); 182 | return; 183 | } 184 | p=start; 185 | while(p!=NULL) 186 | { 187 | printf("%d\n",p->data); 188 | p=p->next; 189 | } 190 | } 191 | -------------------------------------------------------------------------------- /hashtable.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | // structs 8 | struct Item{ 9 | int key; 10 | int frequency; 11 | struct Item* next; 12 | }; 13 | 14 | struct Bucket{ 15 | struct Item * items; 16 | }; 17 | 18 | struct HashTable{ 19 | int a, b, countBucket; 20 | struct Bucket * buckets; 21 | }; 22 | 23 | // function declarations 24 | struct HashTable* init_hash_table(int a, int b, int countBucket); // initialize hashtable with countBucket buckets and hash function as (a + b*key)%countBucket 25 | 26 | struct HashTable* insert(struct HashTable* T, int key); // insert key into T 27 | 28 | bool search(struct HashTable* T, int key); //true if exists in table 29 | 30 | struct HashTable* Delete(struct HashTable* T, int key); // delete key from hastable 31 | 32 | void print_table(struct HashTable* T); // a function to print the hashtable 33 | 34 | // function definitions 35 | 36 | struct HashTable* init_hash_table(int a, int b, int countBucket){ 37 | 38 | struct HashTable* new_ht = (struct HashTable*) malloc(sizeof(struct HashTable)); 39 | 40 | assert(new_ht!=NULL); 41 | 42 | new_ht->a = a; 43 | new_ht->b = b; 44 | new_ht->countBucket = countBucket; 45 | new_ht->buckets = (struct Bucket*) malloc(countBucket * sizeof(struct Bucket)); 46 | assert(new_ht->buckets!=NULL); 47 | 48 | for (int i = 0; ibuckets[i].items = (struct Item*) malloc(sizeof(struct Item)); 50 | assert(new_ht->buckets->items!=NULL); 51 | 52 | new_ht->buckets[i].items->frequency = -1; 53 | new_ht->buckets[i].items->key = -1; 54 | new_ht->buckets[i].items->next = NULL; 55 | } 56 | 57 | return new_ht; 58 | } 59 | 60 | struct HashTable* insert(struct HashTable* T, int key){ 61 | 62 | int bucket_num = (T->a * key + T->b)%T->countBucket; 63 | 64 | struct Item* cur_node = T->buckets[bucket_num].items->next; 65 | 66 | while(cur_node!=NULL){ 67 | 68 | if (cur_node->key == key){ 69 | 70 | cur_node->frequency++; 71 | return T; 72 | 73 | } 74 | 75 | cur_node = cur_node->next; 76 | 77 | } 78 | 79 | struct Item* new_item = (struct Item*) malloc(sizeof(struct Item)); 80 | new_item->key = key; 81 | new_item->frequency = 1; 82 | new_item->next = T->buckets[bucket_num].items->next; 83 | 84 | T->buckets[bucket_num].items->next =new_item; 85 | return T; 86 | } 87 | 88 | bool search(struct HashTable* T, int key){ 89 | 90 | int bucket_num = (T->a * key + T->b)%T->countBucket; 91 | struct Item* cur_node = T->buckets[bucket_num].items->next; 92 | 93 | while(cur_node!=NULL){ 94 | 95 | if (cur_node->key == key && cur_node->frequency>0){ 96 | return 1; 97 | } 98 | 99 | cur_node = cur_node->next; 100 | 101 | } 102 | 103 | return 0; 104 | 105 | } 106 | 107 | struct HashTable* Delete(struct HashTable* T, int key){ 108 | 109 | int bucket_num = (T->a * key + T->b)%T->countBucket; 110 | struct Item* cur_node = T->buckets[bucket_num].items; 111 | 112 | while(cur_node->next!=NULL){ 113 | 114 | if (cur_node->next->key == key){ 115 | 116 | if (cur_node->next->frequency>1){ 117 | 118 | cur_node->next->frequency--; 119 | 120 | }else{ 121 | 122 | struct Item* temp_node = cur_node->next; 123 | cur_node->next = cur_node->next->next; 124 | free(temp_node); 125 | 126 | } 127 | 128 | break; 129 | } 130 | 131 | cur_node = cur_node->next; 132 | } 133 | 134 | return T; 135 | 136 | } 137 | 138 | void print_table(struct HashTable* T){ 139 | 140 | for (int i = 0; icountBucket; i++){ 141 | 142 | struct Item* cur_node = T->buckets[i].items->next; 143 | while(cur_node!=NULL){ 144 | 145 | printf("%d %d ", cur_node->key, cur_node->frequency); 146 | cur_node = cur_node->next; 147 | 148 | } 149 | printf("\n"); 150 | 151 | } 152 | 153 | } 154 | 155 | // driver code 156 | int main(){ 157 | 158 | // creating a hashtable for 1,2,3,4,...20 159 | 160 | int a = 1; 161 | int b = 0; 162 | int countBucket = 10; 163 | 164 | struct HashTable* hashtable=init_hash_table(a,b,countBucket); 165 | 166 | printf("\n\nAfter inserting 1, 2, ... 20\n"); 167 | for (int i = 1; i<=20; i++){ 168 | insert(hashtable, i); 169 | } 170 | 171 | print_table(hashtable); 172 | 173 | // delete all even numbers 174 | for (int i = 2; i<=20; i+=2){ 175 | Delete(hashtable, i); 176 | } 177 | 178 | printf("\n\nAfter deleting 2, 4, ... 20\n"); 179 | print_table(hashtable); 180 | 181 | return 0; 182 | } -------------------------------------------------------------------------------- /roots.c: -------------------------------------------------------------------------------- 1 | #include 2 | int value(double arr[], double m, double *e, int n) 3 | { 4 | int b = n + 1; 5 | int a = 0; 6 | int c = 0; 7 | double sum = 0; 8 | double d = 1; 9 | int i = n; 10 | while (i >= 0) 11 | { 12 | while (c < a) 13 | { 14 | d = d * m; 15 | c++; 16 | } 17 | sum = arr[i] * d + sum; 18 | a++; 19 | i--; 20 | } 21 | *e = sum; 22 | } 23 | int diff(double arr[], double m, double *c, int n) 24 | { 25 | int a = n; 26 | int i = 0; 27 | double arr1[n]; 28 | while (i < n) 29 | { 30 | arr1[i] = arr[i + 1] * (a); 31 | a--; 32 | i++; 33 | } 34 | value(arr1, m, c, n); 35 | } 36 | int main() 37 | { 38 | int n; 39 | double p, q; 40 | scanf("%d", &n); 41 | int b = n + 1; 42 | double arr[b]; 43 | double c, d, x, y; 44 | double h; 45 | int count = 0; 46 | p = -100000; 47 | for (int i = 0; i < b; i++) 48 | { 49 | scanf("%lf", &arr[i]); 50 | } 51 | go: 52 | if (count < n) 53 | { 54 | for (q = p + 1; q < 1000000; q++) 55 | { 56 | value(arr, p, &c, n); 57 | value(arr, q, &d, n); 58 | if (c * d <= 0) 59 | { 60 | p = q - 1; 61 | value(arr, p, &c, n); 62 | if (c == 0 && d == 0) 63 | { 64 | printf("%lf\n", p); 65 | count++; 66 | p = q; 67 | goto go; 68 | } 69 | else if (c == 0) 70 | { 71 | printf("%lf\n", p); 72 | count++; 73 | p = q; 74 | goto go; 75 | } 76 | else if (d == 0) 77 | { 78 | p = q; 79 | goto go; 80 | } 81 | while (c * d < 0) 82 | { 83 | h = (p + q) / 2; 84 | double e; 85 | value(arr, h, &e, n); 86 | if (e < 0.005 && e > -0.005) 87 | { 88 | printf("%lf\n", h); 89 | count++; 90 | p = q; 91 | goto go; 92 | } 93 | else if (e < -0.005) 94 | { 95 | if (c < 0) 96 | { 97 | p = h; 98 | } 99 | else 100 | { 101 | q = h; 102 | } 103 | } 104 | else if (e > 0.005) 105 | { 106 | if (c > 0) 107 | { 108 | p = h; 109 | } 110 | else 111 | { 112 | q = h; 113 | } 114 | } 115 | } 116 | } 117 | else 118 | { 119 | diff(arr, p, &x, n); 120 | diff(arr, q, &y, n); 121 | if (x * y < 0) 122 | { 123 | for (int t = 0; t < 10000000; t++) 124 | { 125 | h = (p + q) / 2; 126 | double e; 127 | value(arr, h, &e, n); 128 | if (e < 0.005 && e > -0.005) 129 | { 130 | printf("%lf\n", h); 131 | count++; 132 | p = q; 133 | goto go; 134 | } 135 | else if (e < -0.005) 136 | { 137 | if (c < 0) 138 | { 139 | p = h; 140 | } 141 | else 142 | { 143 | q = h; 144 | } 145 | } 146 | else if (e > 0.005) 147 | { 148 | if (c > 0) 149 | { 150 | p = h; 151 | } 152 | else 153 | { 154 | q = h; 155 | } 156 | } 157 | } 158 | } 159 | } 160 | } 161 | } 162 | else 163 | { 164 | return 0; 165 | } 166 | } 167 | -------------------------------------------------------------------------------- /sliding_window_maximum_using_deque: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | /* ---------- DEQUE IMPLEMENTATION STARTS ---------- */ 7 | 8 | typedef struct info 9 | { 10 | int val; 11 | } info; 12 | 13 | typedef struct node queuenode; 14 | struct node 15 | { 16 | info data; 17 | struct node *prev; 18 | struct node *next; 19 | }; 20 | 21 | typedef struct Queue 22 | { 23 | int size; 24 | queuenode *front; 25 | queuenode *back; 26 | } Queue; 27 | 28 | queuenode *InitializeNode(info d) 29 | { 30 | queuenode *temp = malloc(sizeof(struct node)); 31 | temp->prev = NULL; 32 | temp->next = NULL; 33 | temp->data = d; 34 | return temp; 35 | } 36 | 37 | Queue *InitializeQueue() 38 | { 39 | Queue *temp = malloc(sizeof(Queue)); 40 | temp->size = 0; 41 | temp->front = NULL; 42 | temp->back = NULL; 43 | return temp; 44 | } 45 | 46 | int Size(Queue *temp) // RETURNS THE SIZE OF THE LIST 47 | { 48 | return (temp->size); 49 | } 50 | 51 | int Front(Queue *temp) // RETURNS THE FRONT ELEMENT OF THE LIST 52 | { 53 | return (temp->front->data.val); 54 | } 55 | 56 | int Back(Queue *temp) // RETURNS THE BACK ELEMENT OF THE LIST 57 | { 58 | return (temp->back->data.val); 59 | } 60 | 61 | int isEmpty(Queue *temp) // CHECKS IF LIST IS EMPTY OR NOT 62 | { 63 | if (temp->size == 0) 64 | { 65 | return 1; 66 | } 67 | else 68 | { 69 | return 0; 70 | } 71 | } 72 | 73 | void enqueue(Queue *temp, info d) // ADDS ELEMENT AT THE END OF THE LIST ~ push.back() 74 | { 75 | queuenode *node = InitializeNode(d); 76 | if (isEmpty(temp)) 77 | { 78 | temp->front = node; 79 | temp->back = node; 80 | } 81 | else 82 | { 83 | temp->back->next = node; 84 | node->prev = temp->back; 85 | temp->back = node; 86 | } 87 | temp->size += 1; 88 | } 89 | 90 | void enqueueFront(Queue *temp, info d) // ADDS ELEMENT AT THE FRONT OF THE LIST ~ push.front() 91 | { 92 | queuenode *node = InitializeNode(d); 93 | if (isEmpty(temp)) 94 | { 95 | temp->front = node; 96 | temp->back = node; 97 | } 98 | else 99 | { 100 | temp->front->prev = node; 101 | node->next = temp->front; 102 | temp->front = node; 103 | } 104 | temp->size += 1; 105 | } 106 | 107 | info dequeue(Queue *temp) // DELETES ELEMENT AT THE FRONT OF THE LIST ~ pop.front() 108 | { 109 | queuenode *toremove, *newhead; 110 | toremove = temp->front; 111 | newhead = temp->front->next; 112 | if (newhead == NULL) 113 | { 114 | temp->back = NULL; 115 | } 116 | temp->front = newhead; 117 | info deleted = toremove->data; 118 | free(toremove); 119 | toremove = NULL; 120 | temp->size -= 1; 121 | return deleted; 122 | } 123 | 124 | info dequeueBack(Queue *temp) // DELETES ELEMENT AT THE END OF THE LIST ~ pop.back() 125 | { 126 | queuenode *toremove, *newback; 127 | toremove = temp->back; 128 | newback = temp->back->prev; 129 | if (newback == NULL) 130 | { 131 | temp->front = NULL; 132 | } 133 | temp->back = newback; 134 | info deleted = toremove->data; 135 | free(toremove); 136 | toremove = NULL; 137 | temp->size -= 1; 138 | return deleted; 139 | } 140 | 141 | void Print(Queue *q) // HELPER FUNCTION USED FOR DEBUGGING AND CHECKING OUTPUT. 142 | { 143 | Queue *temp = InitializeQueue(); 144 | while (!isEmpty(q)) 145 | { 146 | info x = dequeue(q); 147 | printf("%d ", x.val); 148 | enqueue(temp, x); 149 | } 150 | printf("\n"); 151 | while (!isEmpty(temp)) 152 | { 153 | info y = dequeue(temp); 154 | enqueue(q, y); 155 | } 156 | free(temp); 157 | } 158 | 159 | /* ------------- DEQUE IMPLEMENTATION ENDS ------------------ */ 160 | 161 | /* ------------- SOLVER FUNCTION FOR ANSWER ----------------- */ 162 | 163 | void solve(int arr[], int n, int k) 164 | { 165 | Queue *list = InitializeQueue(); 166 | int i; 167 | for (i = 0; i < k; ++i) 168 | { 169 | info temp; 170 | temp.val = i; 171 | temp.val = i; 172 | while ((!isEmpty(list)) && arr[i] <= arr[Back(list)]) 173 | { 174 | dequeueBack(list); 175 | } 176 | enqueue(list, temp); 177 | } 178 | for (; i < n; ++i) 179 | { 180 | info temp2; 181 | temp2.val = i; 182 | printf("%d ", arr[Front(list)]); 183 | while (!isEmpty(list) && Front(list) <= i - k) 184 | dequeue(list); 185 | while (!isEmpty(list) && arr[i] <= arr[Back(list)]) 186 | dequeueBack(list); 187 | enqueue(list, temp2); 188 | } 189 | printf("%d ", arr[Front(list)]); 190 | printf("\n"); 191 | } 192 | 193 | /* --------------------------------------------------------------*/ 194 | 195 | /* ----------- MAIN FUNCTION ------------ */ 196 | 197 | int main() 198 | { 199 | int n, m; 200 | scanf("%d %d", &n, &m); 201 | int A[n]; 202 | for (int i = 0; i < n; i++) 203 | { 204 | scanf("%d", &A[i]); 205 | } 206 | solve(A, n, m); 207 | return 0; 208 | } 209 | 210 | /* ----------------------------------------*/ 211 | --------------------------------------------------------------------------------