├── .github └── FUNDING.yml ├── Addition.c ├── Address of 1-D array.c ├── AllTempScalesConv.c ├── Anagram-Program-in-C.c ├── AreaAndCircumference.c ├── Area_of_Circle.c ├── Area_of_Square.c ├── Area_of_Triangle.c ├── ArmstrongNumber.c ├── Automorphic_number.c ├── BackgroundThreadSorter.c ├── BasicArithmatic.c ├── BasicGame.c ├── BinarySearch.c ├── BitwiseAndOperator.c ├── BitwiseComplementOperator.c ├── BitwiseLeftshiftOperator.c ├── BitwiseOddOrEven.c ├── BubbleSort.c ├── Buffer Overflow for Kids.md ├── CalcUsingSwitchCase.c ├── CelciusToKelvinConv.c ├── Changingbase.c ├── CheckCharacterType.c ├── CommonElementsInTwoArrays.c ├── DailyWageCalc.c ├── DayNameUsingSwitchCase.c ├── DecimalToBaseN.c ├── DecimalToBinary.c ├── DecimalToHexadecimalViceVersa.c ├── DecrementOperator.c ├── Diagonal-Difference.c ├── DiceRoll.c ├── DigitalRoot.c ├── DisplayLinuxEnvirmentVariables.c ├── Division.c ├── DynamicTwoDArrayUsingArrayOfPointer.c ├── DynamicTwoDArrayUsingOnePointer.c ├── EmployeeGrade.c ├── EncryptDecryptXOR.c ├── Even Fibonacci numbers.c ├── Factorial.c ├── FahrenheitToCelciusConv.c ├── FibonacciGeneration.c ├── FindAsciiValue.c ├── FindRemainder.c ├── GotoStatementEvenOrOdd.c ├── Gross_Salary.c ├── HaystackAndNeedle_SubString.c ├── HelloWorld.c ├── IncrementOperator.c ├── Insertionsort.c ├── LICENSE ├── Largest.c ├── LeapYearTernaryOperator.c ├── Linked List Creation.c ├── LowercaseToUppercase.c ├── MagicNumbers.c ├── Mergesort.c ├── MirrorNumber.c ├── NestedIfLeapYear.c ├── NestedifGreatestInteger.c ├── NumberPattern.c ├── Palindrome.c ├── PalindromeNumber.c ├── PascalTriangle.c ├── Pattern1.c ├── Pattern_Combos.c ├── PerfectNumber.c ├── Pointers.c ├── Polynomial_linklist.c ├── Position_of_First'n'Second_max_elements_without_sorting_array.c ├── Prime.c ├── PrimeByEratosthenes.c ├── Quicksort.c ├── README.md ├── Recursion.c ├── RecursiveFactorial.c ├── RelationalOperators.c ├── ReverseNumber.c ├── ReverseNumber2.c ├── RussianPeasantMultiplication.c ├── Screenshot.PNG ├── SegmentationFaultorBusErrorDemo.c ├── SelectionSort.c ├── Separate.c ├── SimpleArithmeticAverage.c ├── SimpleEMICalculator.c ├── SimpleInterestCalculator.c ├── SimpleMultiplicationTable.c ├── Simple_Interest.c ├── SizeofOperator.c ├── SquareRoot.c ├── Stack - Linked List.c ├── Star_Pattern.c ├── StringLength.c ├── Structure.c ├── StudentMarksPercentage.c ├── Subtraction.c ├── SumUsingThreads.c ├── SwapByRefandCopy.c ├── SwapIntegers.c ├── SwapValueUsingThirdVariable.c ├── SwapValueWithoutUsingThirdVariable.c ├── Swap_1.c ├── Swap_2.c ├── Temperature.c ├── TemperatureSwitch.c ├── TernaryOperator.c ├── Trif.c ├── UppercaseToLowercase.c ├── VowelorConsonant.c ├── alphabetTriangle.c ├── combine_calculator.c ├── counting_sort.c ├── dynamicMemoryAllocation.c ├── endian.c ├── gcd.c ├── heap sort.c ├── increment_number.c ├── isInputLeapYear.c ├── large_factorial.c ├── last-digit-fibonacci.c ├── lcm.c ├── nCrCalculatorLargeNumbers.c ├── oddandeven.c ├── priority_queue.c ├── swappingwithoutthirdvariable.c └── transposeOfMatrix.c /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | custom: ["https://www.paypal.com/paypalme/ompp00"] 2 | -------------------------------------------------------------------------------- /Addition.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int a, b, c; 6 | printf("Enter two Numbers : "); 7 | scanf("%d %d", &a, &b); 8 | c = a + b; 9 | printf("The Sum is %d\n", c); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /Address of 1-D array.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int a[100], i, n, *add; 6 | printf("enter the size :"); 7 | scanf("%d", &n); 8 | printf("enter the no :"); 9 | for(i = 0; i < n; i++) 10 | { 11 | scanf("%d", &a[i]); 12 | } 13 | for(i = 0; i < n; i++) 14 | { 15 | add = a + i; 16 | printf("add is %p value is %d\n", (void *)add, *add); 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /AllTempScalesConv.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | // Function that performs the conversion 6 | double convertTemp(double initValue, int initScale, int finalScale) 7 | { 8 | double finalValue; 9 | switch(initScale) 10 | { 11 | // Celsius 12 | case 1: 13 | // Celsius to Kelvin 14 | if(finalScale == 1) 15 | { 16 | finalValue = initValue + 273.15; 17 | } 18 | // Celsius to Fahrenheit 19 | else if(finalScale == 2) 20 | { 21 | finalValue = (initValue * 9 / 5) + 32; 22 | } 23 | else 24 | exit(1); 25 | break; 26 | case 2: 27 | // Kelvin to Celsius 28 | if(finalScale == 1) 29 | { 30 | finalValue = initValue - 273.15; 31 | } 32 | // Kelvin to Fahrenheit 33 | else if(finalScale == 2) 34 | { 35 | finalValue = ((initValue - 273.15) * 9 / 5) + 32; 36 | } 37 | else 38 | exit(1); 39 | break; 40 | case 3: 41 | // Fahrenheit to Celsius 42 | if(finalScale == 1) 43 | { 44 | finalValue = (initValue - 32) * 5 / 9; 45 | } 46 | // Fahrenheit to Kelvin 47 | else if(finalScale == 2) 48 | { 49 | finalValue = ((initValue - 32) * 5 / 9) + 273.15; 50 | } 51 | else 52 | exit(1); 53 | break; 54 | } 55 | return finalValue; 56 | } 57 | 58 | int main(void) 59 | { 60 | int option; 61 | double initialValue, finalValue; 62 | while(1) 63 | { 64 | // main menu 65 | printf("\n0 - Exit\n"); 66 | printf("1 - Convert from Celsius to Kelvin\n"); 67 | printf("2 - Convert from Celsius to Fahrenheit\n"); 68 | printf("3 - Convert from Kelvin to Fahrenheit\n"); 69 | printf("4 - Convert from Kelvin to Celsius\n"); 70 | printf("5 - Convert from Fahrenheit to Celsius\n"); 71 | printf("6 - Convert from Fahrenheit to Kelvin\n"); 72 | 73 | printf("Select a number: "); 74 | scanf("%d", &option); 75 | assert(option >= 0); 76 | assert(option <= 6); 77 | 78 | if(!option) 79 | { 80 | printf("Ending program\n"); 81 | return 0; 82 | } 83 | 84 | printf("Please enter the initial value: "); 85 | scanf("%lf", &initialValue); 86 | 87 | switch(option) 88 | { 89 | case 1: 90 | finalValue = convertTemp(initialValue, 1, 1); 91 | printf("Valor em Kelvin: %.2lf", finalValue); 92 | break; 93 | case 2: 94 | finalValue = convertTemp(initialValue, 1, 2); 95 | printf("Valor em Fahrenheit: %.2lf", finalValue); 96 | break; 97 | case 3: 98 | finalValue = convertTemp(initialValue, 2, 1); 99 | printf("Valor em Celsius: %.2lf", finalValue); 100 | break; 101 | case 4: 102 | finalValue = convertTemp(initialValue, 2, 2); 103 | printf("Valor em Fahrenheit: %.2lf", finalValue); 104 | break; 105 | case 5: 106 | finalValue = convertTemp(initialValue, 3, 1); 107 | printf("Valor em Celsius: %.2lf", finalValue); 108 | break; 109 | case 6: 110 | finalValue = convertTemp(initialValue, 3, 1); 111 | printf("Valor em Kelvin: %.2lf", finalValue); 112 | break; 113 | } 114 | printf("\n"); 115 | } 116 | 117 | return 0; 118 | } 119 | -------------------------------------------------------------------------------- /Anagram-Program-in-C.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int checkAnagram(char *str1, char *str2); 6 | 7 | int main() 8 | { 9 | char str1[100], str2[100]; 10 | 11 | printf("Function : whether two given strings are anagram :"); 12 | printf("\nExample : pears and spare, stone and tones :"); 13 | 14 | printf(" Input the first String : "); 15 | fgets(str1, sizeof str1, stdin); 16 | printf(" Input the second String : "); 17 | fgets(str2, sizeof str2, stdin); 18 | 19 | if(checkAnagram(str1, str2) == 1) 20 | { 21 | str1[strlen(str1) - 1] = '\0'; 22 | str2[strlen(str2) - 1] = '\0'; 23 | printf(" %s and %s are Anagram.\n\n", str1, str2); 24 | } 25 | else 26 | { 27 | str1[strlen(str1) - 1] = '\0'; 28 | str2[strlen(str2) - 1] = '\0'; 29 | printf(" %s and %s are not Anagram.\n\n", str1, str2); 30 | } 31 | return 0; 32 | } 33 | 34 | // Function to check whether two passed strings are anagram or not 35 | 36 | int checkAnagram(char *str1, char *str2) 37 | { 38 | int str1ChrCtr[256] = {0}, str2ChrCtr[256] = {0}; 39 | int ctr; 40 | 41 | /* check the length of equality of Two Strings */ 42 | 43 | if(strlen(str1) != strlen(str2)) 44 | { 45 | return 0; 46 | } 47 | 48 | // count frequency of characters in str1 49 | 50 | for(ctr = 0; str1[ctr] != '\0'; ctr++) 51 | { 52 | str1ChrCtr[(int)str1[ctr]]++; 53 | } 54 | 55 | // count frequency of characters in str2 56 | 57 | for(ctr = 0; str2[ctr] != '\0'; ctr++) 58 | { 59 | str2ChrCtr[(int)str2[ctr]]++; 60 | } 61 | 62 | // compare character counts of both strings 63 | 64 | for(ctr = 0; ctr < 256; ctr++) 65 | { 66 | if(str1ChrCtr[ctr] != str2ChrCtr[ctr]) 67 | return 0; 68 | } 69 | return 1; 70 | } 71 | -------------------------------------------------------------------------------- /AreaAndCircumference.c: -------------------------------------------------------------------------------- 1 | // To calculate Area and Circumference of a circle 2 | 3 | #include 4 | 5 | #define PI 3.14 6 | int main(void) 7 | { 8 | float r, a, c; 9 | printf("enter radius :\n"); 10 | scanf("%f", &r); 11 | a = PI * r * r; 12 | c = 2 * PI * r; 13 | printf("Area = %f\n circumference = %f\n", a, c); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Area_of_Circle.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define PI 3.14 4 | 5 | int main(void) 6 | { 7 | float a, r; 8 | printf("Enter the Radius : "); 9 | scanf("%f", &r); 10 | a = PI * r * r; 11 | printf("Area is %f\n", a); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Area_of_Square.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | float a, s; 6 | printf("Enter the Side length : "); 7 | scanf("%f", &s); 8 | a = s * s; 9 | printf("Area is %f\n", a); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /Area_of_Triangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | float a, b, h; 6 | printf("Enter the base : "); 7 | scanf("%f", &b); 8 | printf("Enter the height : "); 9 | scanf("%f", &h); 10 | a = 0.5 * b * h; 11 | printf("Area is %f\n", a); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /ArmstrongNumber.c: -------------------------------------------------------------------------------- 1 | // To check whether the given number is Armstrong number or not 2 | // Armstrong number: An n -digit number equal to the sum of the nth powers 3 | // of its digits. 4 | // Example: (1^3) + (5^3) + (3^3)= 153 5 | 6 | #include 7 | #include 8 | 9 | int main(void) 10 | { 11 | int number, sum = 0, rem = 0, nthPower = 0, digits = 0, temp; 12 | printf("Enter a number:\n"); 13 | scanf("%d", &number); 14 | temp = number; 15 | // to calculate the number of digits in the number 16 | while(number != 0) 17 | { 18 | number = number / 10; 19 | digits++; 20 | } 21 | number = temp; 22 | // to get the nth power of each digit and add it to the sum 23 | while(number != 0) 24 | { 25 | rem = number % 10; 26 | nthPower = pow(rem, digits); 27 | sum = sum + nthPower; 28 | number = number / 10; 29 | } 30 | // to check if obtained sum is equal to the original number 31 | if(sum == temp) 32 | printf("The given number is an Armstrong number"); 33 | else 34 | printf("The given number is not an Armstrong number"); 35 | printf("\n"); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Automorphic_number.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(void) 6 | { 7 | int num, sqr, temp, last; 8 | int n = 0; 9 | printf("Enter a number \n"); 10 | scanf("%d", &num); 11 | sqr = num * num; // calculating square of num 12 | temp = num; 13 | // Counting number of digits 14 | while(temp > 0) 15 | { 16 | n++; 17 | temp = temp / 10; 18 | } 19 | // Extracting last n digits 20 | int den = floor(pow(10, n)); 21 | last = sqr % den; 22 | if(last == num) 23 | printf("Automorphic number \n"); 24 | else 25 | printf("Not Automorphic \n"); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /BackgroundThreadSorter.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is an example of using a background thread to sort a fixed size array 3 | * asynchronously. The array itself can be modified by adding new elements. The array 4 | * acts like a circular buffer when we cross the size of the array i.e the index goes like this: 5 | * 0,1,2,3,4,0,1,2,3,4,.....(For an array of size 5). 6 | * This program demonstrates usage of pthreads, mutexes, qsort(in built C sorting function). 7 | * The array used is an array of structures. 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #define RECT_ARRAY_SIZE 5 16 | 17 | /* 18 | * Rectangle structure has length and breadth. 19 | */ 20 | struct rect 21 | { 22 | int length; 23 | int breadth; 24 | }; 25 | 26 | /* 27 | * Array of rectangle structs to be sorted. 28 | */ 29 | struct rect r[RECT_ARRAY_SIZE]; 30 | /* 31 | * mutex lock to serialize the array of rectangle structs 32 | */ 33 | pthread_mutex_t lock; 34 | 35 | /* 36 | * The comparator to compare the 2 rectangle structures. 37 | * Comparison is based on area. 38 | */ 39 | int area_comp(const void *r1, const void *r2) 40 | { 41 | int area1, area2; 42 | 43 | struct rect r1_t = *(const struct rect *)r1; 44 | struct rect r2_t = *(const struct rect *)r2; 45 | 46 | area1 = r1_t.length * r1_t.breadth; 47 | area2 = r2_t.length * r2_t.breadth; 48 | 49 | return area1 > area2 ? 1 : 0; 50 | } 51 | 52 | /* 53 | * Function of the background pthread. 54 | * This function sorts the array of rectangle structs 55 | * every 10 seconds and prints out the output. 56 | */ 57 | void *sorter(void *p) 58 | { 59 | for(;;) 60 | { 61 | pthread_mutex_lock(&lock); 62 | qsort(r, RECT_ARRAY_SIZE, sizeof(struct rect), area_comp); 63 | printf("The rect array is\n"); 64 | for(int i = 0; i < RECT_ARRAY_SIZE; i++) 65 | { 66 | printf("%d %d\n", r[i].length, r[i].breadth); 67 | } 68 | pthread_mutex_unlock(&lock); 69 | sleep(10); 70 | printf("\n"); 71 | } 72 | } 73 | 74 | int main(void) 75 | { 76 | /* 77 | * Our background thread to sort the array 78 | * of rectangle structs 79 | */ 80 | pthread_t thread; 81 | int index = 0; 82 | 83 | /* 84 | * First initialise the rectangle struct to some values 85 | */ 86 | r[0].length = 10; 87 | r[0].breadth = 2; 88 | 89 | r[1].length = 34; 90 | r[1].breadth = 23; 91 | 92 | r[2].length = 32; 93 | r[2].breadth = 3; 94 | 95 | r[3].length = 2; 96 | r[3].breadth = 45; 97 | 98 | r[4].length = 2; 99 | r[4].breadth = 12; 100 | 101 | /* 102 | * Init the mutexes and the pthread 103 | */ 104 | pthread_mutex_init(&lock, NULL); 105 | pthread_create(&thread, NULL, &sorter, (void *)NULL); 106 | 107 | /* 108 | * The main thread allows the user to input data i.e length 109 | * and breadth of the users rectangle. The length and breadth inputted 110 | * are stored in a structure which is placed into the rectangle struct array. 111 | * The rectangle struct array is of fixed size. The size is defined by 112 | * RECT_ARRAY_SIZE. The array acts like a circular buffer. 113 | */ 114 | for(;;) 115 | { 116 | struct rect temp_rect; 117 | int length, breadth; 118 | 119 | scanf("%d %d", &length, &breadth); 120 | 121 | temp_rect.length = length; 122 | temp_rect.breadth = breadth; 123 | 124 | pthread_mutex_lock(&lock); 125 | r[(index++) % RECT_ARRAY_SIZE] = temp_rect; 126 | pthread_mutex_unlock(&lock); 127 | } 128 | 129 | return 0; 130 | } 131 | -------------------------------------------------------------------------------- /BasicArithmatic.c: -------------------------------------------------------------------------------- 1 | // A simple arithmetic operation on two integers 2 | 3 | #include 4 | int main(void) 5 | { 6 | int number1, number2, addition, subtraction, multiplication, division, modulo; 7 | printf("Enter two numbers :\n"); 8 | scanf("%d%d", &number1, &number2); 9 | addition = number1 + number2; 10 | subtraction = number1 - number2; 11 | multiplication = number1 * number2; 12 | division = number1 / number2; 13 | modulo = number1 % number2; 14 | printf("Addition of number 1 and number 2 : %d\n", addition); 15 | printf("Subtraction of number 1 and number 2 : %d\n", subtraction); 16 | printf("Multiplication of number 1 and number 2 : %d\n", multiplication); 17 | printf("Division of number 1 and number 2 : %d\n", division); 18 | printf("Modulo of number 1 and number 2 : %d\n", modulo); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /BasicGame.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #define MAX 10 8 | #define A "P" 9 | #define B "Q" 10 | 11 | int getRandom(int low, int high); 12 | int getValidInteger(int low, int high); 13 | unsigned int playerRoll(int low, int high); 14 | 15 | void seed(void) 16 | { 17 | srand(time(NULL)); 18 | } 19 | 20 | void space(void) //(unsigned int size) // create space 21 | { 22 | printf(" "); 23 | } 24 | 25 | char getDisplayType(unsigned int index, unsigned int playerPosition, 26 | char playerName) // check the index and return character 27 | { 28 | 29 | if(playerName != '#') 30 | { 31 | if(index == playerPosition) 32 | return playerName; 33 | 34 | if(index == 0) 35 | return ('C'); 36 | 37 | if(index % 3 == 0) 38 | { 39 | if(index % 5 == 0) 40 | if(index % 7 == 0) 41 | return ('G'); 42 | else 43 | return ('L'); 44 | else 45 | return ('W'); 46 | } 47 | 48 | if(index % 5 == 0) 49 | { 50 | if(index % 7 == 0) 51 | return ('G'); 52 | else 53 | return ('L'); 54 | } 55 | 56 | if(index % 7 == 0) 57 | { 58 | return ('G'); 59 | } 60 | else 61 | return (' '); 62 | } 63 | 64 | if(playerName == '#') 65 | { 66 | 67 | if(index == 0) 68 | return ('C'); 69 | 70 | if(index % 3 == 0) 71 | { 72 | if(index % 5 == 0) 73 | { 74 | if(index % 7 == 0) 75 | { 76 | return ('G'); 77 | } 78 | else 79 | return ('L'); 80 | } 81 | else 82 | return ('W'); 83 | } 84 | 85 | if(index % 5 == 0) 86 | { 87 | if(index % 7 == 0) 88 | { 89 | return ('G'); 90 | } 91 | else 92 | return ('L'); 93 | } 94 | if(index % 7 == 0) 95 | { 96 | return ('G'); 97 | } 98 | else 99 | return (' '); 100 | } 101 | return '@'; 102 | } 103 | 104 | void firstLine(unsigned int size) // create the upper line of the square for the first and last line 105 | { 106 | unsigned int i; 107 | 108 | for(i = 0; i < size; i++) 109 | { 110 | printf(" ___ "); 111 | } 112 | printf("\n"); 113 | } 114 | 115 | void secondLine(unsigned int size, unsigned int playerPosition, 116 | char playerName) // create the inside and return the type for the first line 117 | { 118 | unsigned int x, j; 119 | for(j = 0; j < size; j++) 120 | { 121 | x = j; 122 | printf("| %c |", getDisplayType(x, playerPosition, playerName)); 123 | } 124 | printf("\n"); 125 | } 126 | 127 | void secondLine2nd(unsigned int size, unsigned int i, unsigned int playerPosition, char playerName) 128 | { // create the inside and return the type for the last line 129 | int x, y, z; 130 | y = 3 * (size - 1); 131 | printf("| %c |", getDisplayType(y, playerPosition, playerName)); 132 | for(i = 0; i < size - 2; i++) 133 | { 134 | z = 2 * (size - 1) + ((size - 2) - i); 135 | printf("| %c |", getDisplayType(z, playerPosition, playerName)); 136 | } 137 | x = size + i; 138 | printf("| %c |", getDisplayType(x, playerPosition, playerName)); 139 | 140 | printf("\n"); 141 | } 142 | 143 | void thirdLine(unsigned int size) // create lower line for the first and last line 144 | { 145 | unsigned int i; 146 | 147 | for(i = 0; i < size; i++) 148 | { 149 | printf("|___|"); 150 | } 151 | printf("\n"); 152 | } 153 | 154 | void upperrow(unsigned int size) // create the upper line for the square of the row 155 | { 156 | unsigned int i; 157 | printf(" ___"); 158 | for(i = 0; i < size - 2; i++) 159 | { 160 | space(); 161 | } 162 | printf(" ___"); 163 | printf("\n"); 164 | } 165 | 166 | void lowerrow(unsigned int size) // create the lower line for the square of the row 167 | { 168 | unsigned int i; 169 | printf("|___|"); 170 | for(i = 0; i < size - 2; i++) 171 | { 172 | space(); 173 | } 174 | 175 | printf("|___|"); 176 | printf("\n"); 177 | } 178 | 179 | void middlerow(unsigned int size, unsigned int i, unsigned int playerPosition, 180 | char playerName) // create the inside and return the type for the square of the row 181 | { 182 | unsigned int q, b, p; 183 | b = i; 184 | q = 3 * (size - 1) + ((size - 1) - i); 185 | printf("| %c |", getDisplayType(q, playerPosition, playerName)); 186 | for(b = 0; b < size - 2; b++) 187 | { 188 | space(); 189 | } 190 | p = size - 1 + i; 191 | printf("| %c |", getDisplayType(p, playerPosition, playerName)); 192 | printf("\n"); 193 | } 194 | 195 | char getValidCharacter(void) // make sure the character input is right// 196 | { 197 | char choice; 198 | do 199 | { 200 | scanf("%c", &choice); 201 | getchar(); 202 | if((choice != 'p' && choice != 'q' && choice != 'r' && choice != 's')) 203 | { 204 | printf("Invalid input, please try again: "); 205 | } 206 | else 207 | break; 208 | } while(1); //(choice != 'p' && choice != 'q' && choice != 'r' && choice != 's'); 209 | return choice; 210 | } 211 | 212 | int getValidInteger(int low, int high) // validate the input number// 213 | { 214 | int a; 215 | int choice; 216 | do 217 | { 218 | a = scanf("%d", &choice); 219 | if(choice < low || choice > high) 220 | { 221 | printf("Invalid input, please try again: "); 222 | } 223 | else 224 | break; 225 | 226 | } while(1); //(choice < low || choice > high); 227 | return choice; 228 | } 229 | 230 | int getRandom(int low, int high) // get a rando number 231 | { 232 | int a; 233 | a = rand() % (high - low) + low; 234 | 235 | return a; 236 | } 237 | 238 | unsigned int playerRoll(int low, int high) // prompt and output the roll 239 | { 240 | int a = 1, b = 0, c, d, e, choice; 241 | do 242 | { 243 | printf("\nyour turn, how many dice will you roll : "); 244 | scanf("%d", &choice); 245 | if(choice == 1) 246 | { 247 | b = getRandom(low, high); 248 | // printf("b: \n"); 249 | // scanf("%d", &b); 250 | printf("You rolled %d\n", b); 251 | printf("Advancing %d space\n", b); 252 | a = 0; 253 | return b; 254 | } 255 | else if(choice == 2) 256 | { 257 | c = getRandom(low, high); 258 | // printf("c: "); 259 | // scanf("%d", &c); 260 | d = getRandom(low, high); 261 | // printf("d: "); 262 | // scanf("%d", &d); 263 | b = c + d; 264 | printf("You rolled %d %d\n ", c, d); 265 | printf("Advancing %d space\n", b); 266 | a = 0; 267 | return b; 268 | } 269 | else if(choice == 3) 270 | { 271 | c = getRandom(low, high); 272 | d = getRandom(low, high); 273 | e = getRandom(low, high); 274 | b = c + d + e; 275 | printf("You rolled %d %d %d\n ", c, d, e); 276 | printf("Advancing %d space\n", b); 277 | a = 0; 278 | return b; 279 | } 280 | else 281 | printf("Try again,"); 282 | } while(a == 1); // m 283 | return b; 284 | } 285 | 286 | void winPrize(int playerPrizes[], unsigned int *prizeCount) // do the winprize function 287 | { 288 | // int i; 289 | unsigned int prize; 290 | prize = getRandom(10, 100); 291 | printf("%d\n", prize); 292 | if(*prizeCount < MAX) 293 | { 294 | playerPrizes[*prizeCount] = prize; 295 | printf("you won a prize of %d\n", prize); 296 | *prizeCount = *prizeCount + 1; 297 | } 298 | else 299 | printf("Your inventory is full \n"); 300 | } 301 | 302 | void winGrandPrize(int playerPrizes[], unsigned int *prizeCount) // do the win grand prize function 303 | { 304 | // int i; 305 | unsigned int prize; 306 | prize = getRandom(100, 200); 307 | printf("%d\n", prize); 308 | if(*prizeCount < MAX) 309 | { 310 | playerPrizes[*prizeCount] = prize; 311 | printf("you won a grand prize of %d\n", prize); 312 | *prizeCount = *prizeCount + 1; 313 | } 314 | else 315 | printf("Your inventory is full "); 316 | } 317 | 318 | void loseItem(int playerPrizes[], unsigned int *prizeCount) // do the loseitem fuction 319 | { 320 | int i, j, k, /*r, */ ran = 2; 321 | 322 | if(*prizeCount == 0) 323 | { 324 | printf("Nothing happened,Move On\n"); 325 | } 326 | else 327 | { 328 | 329 | ran = getRandom(0, *prizeCount); 330 | playerPrizes[ran] = 0; 331 | *prizeCount = *prizeCount - 1; 332 | printf("you lost the prize"); 333 | for(i = ran - 1; i < MAX; i++) // arange the array in order 334 | for(j = i; j < MAX; j++) 335 | if(playerPrizes[i] == 0) 336 | { 337 | k = playerPrizes[i]; 338 | playerPrizes[i] = playerPrizes[j]; 339 | playerPrizes[j] = k; 340 | } 341 | } 342 | } 343 | 344 | void initPlayer(int *playerScore, int playerPrizes[], unsigned int *prizeCount, char *playerName, 345 | unsigned int *playerPosition) // do the initplayer function, set everything to 0 346 | { 347 | // int i; 348 | playerPrizes[MAX] = 0; 349 | 350 | *playerScore = 0; 351 | printf("playerPrizes: %d\n", playerPrizes[MAX]); 352 | *prizeCount = 0; 353 | *playerPosition = 0; 354 | printf("Enter Player ID: "); 355 | scanf("%c", playerName); 356 | } 357 | 358 | void displayBoard(unsigned int size, unsigned int playerPosition, char playerName) // display the boardgame 359 | { 360 | 361 | int k, size1, loop; 362 | float loop2, playerPosition1, size2; 363 | // printf("player name in display board: %c\n", playerName); 364 | // printf("%d\n", r); 365 | // printf("%d", playerPosition); 366 | playerPosition1 = (float)playerPosition; 367 | size1 = (4 * (size - 1)); 368 | 369 | size2 = (float)size1; 370 | // printf("size2: %.2f\n", size2); 371 | // printf("playerPo1: %.2f\n", playerPosition1); 372 | // playerPosition2 = float size; 373 | // printf("playerPos: %d\n", playerPosition); 374 | loop2 = playerPosition1 / size2; 375 | 376 | loop = trunc(loop2); 377 | 378 | k = playerPosition - (4 * (size - 1)) * loop; 379 | 380 | playerPosition = k; 381 | 382 | { 383 | unsigned int i = 0; 384 | if(size == 1) 385 | { 386 | printf(" ___ \n"); 387 | printf(" | ? | \n"); 388 | printf(" |___|"); 389 | printf("\n"); 390 | } 391 | else 392 | { 393 | 394 | for(i = 0; i < size - 1; i++) 395 | { 396 | 397 | if(i == 0) 398 | { 399 | firstLine(size); 400 | secondLine(size, /*i, */ playerPosition, playerName); 401 | thirdLine(size); 402 | } 403 | } 404 | for(i = 1; i < size - 1; i++) 405 | { 406 | upperrow(size); 407 | middlerow(size, i, playerPosition, playerName); 408 | lowerrow(size); 409 | } 410 | for(i = size - 2; i < size - 1; i++) 411 | { 412 | firstLine(size); 413 | secondLine2nd(size, i, playerPosition, playerName); 414 | thirdLine(size); 415 | } 416 | } 417 | } 418 | } 419 | 420 | int checkout(int *playerScore, int playerPrizes[], unsigned int *prizeCount) // do the checkout 421 | { 422 | unsigned int i; 423 | for(i = 0; i < *prizeCount; i++) 424 | *playerScore += playerPrizes[i]; 425 | *prizeCount = 0; 426 | printf("You check out for $%d score is now: $%d \n", *playerScore, *playerScore); 427 | if(*playerScore >= 200) 428 | { 429 | return 1; 430 | } 431 | else 432 | { 433 | return 0; 434 | } 435 | } 436 | 437 | void playGame(unsigned int size, int *playerScore, int playerPrizes[], unsigned int *prizeCount, char *playerName, 438 | unsigned int *playerPosition) // play the game 439 | { 440 | printf("playerName in playgame %c\n", *playerName); 441 | // printf("%d\n",*prizeCount); 442 | // printf("%d\n", *playerScore); 443 | unsigned int i, l = 1; 444 | while(l) 445 | { 446 | displayBoard(size, *playerPosition, *playerName); 447 | 448 | printf("Score: %d inventory (%d items): ", *playerScore, *prizeCount); 449 | for(i = 0; i < *prizeCount; i++) 450 | { 451 | printf("%d, ", playerPrizes[i]); 452 | } 453 | 454 | *playerPosition = *playerPosition + playerRoll(1, 6); 455 | if(*playerPosition >= 4 * (size - 1)) 456 | *playerPosition = *playerPosition - 4 * (size - 1); 457 | // printf("player position in display %d\n", *playerPosition); 458 | // printf("display type in play game: %c\n", getDisplayType(*playerPosition, *playerPosition, '#')); 459 | // displayBoard(boardSize, playerPosition, playerName); 460 | if(getDisplayType(*playerPosition, *playerPosition, '#') == 'G') 461 | { 462 | winGrandPrize(playerPrizes, prizeCount); 463 | } 464 | else if(getDisplayType(*playerPosition, *playerPosition, '#') == 'W') 465 | { 466 | 467 | winPrize(playerPrizes, prizeCount); 468 | } 469 | else if(getDisplayType(*playerPosition, *playerPosition, '#') == 'L') 470 | { 471 | 472 | loseItem(playerPrizes, prizeCount); 473 | } 474 | else if(getDisplayType(*playerPosition, *playerPosition, '#') == 'C') 475 | { 476 | 477 | if(checkout(playerScore, playerPrizes, prizeCount) == 1) 478 | { 479 | printf("You Win\n"); 480 | l = 0; 481 | } 482 | } 483 | else 484 | printf("nothing happens, go again.\n"); 485 | } 486 | } 487 | 488 | int main(void) 489 | { 490 | int l = 1; 491 | char choice; 492 | // char c = '#'; 493 | int playerScore; 494 | int playerPrizes[MAX]; 495 | unsigned int prizeCount; 496 | char playerName; 497 | unsigned int size; 498 | unsigned int playerPosition; 499 | 500 | printf("Welcome to CHECKOUT\n"); 501 | seed(); 502 | while(l) 503 | { 504 | printf("Main Menu\n"); 505 | printf("p-(p)lay q-(q)uit r-inst(r)uctions s-HI(s)core: \n"); 506 | choice = getValidCharacter(); //('P', 'Q'); 507 | if(choice == 'p') 508 | { 509 | printf("Number of players is 1\n"); 510 | initPlayer(&playerScore, playerPrizes, &prizeCount, &playerName, &playerPosition); 511 | 512 | printf("Enter board size: "); 513 | scanf("%d", &size); 514 | 515 | playGame(size, &playerScore, playerPrizes, &prizeCount, &playerName, &playerPosition); 516 | 517 | getchar(); 518 | } 519 | if(choice == 's') 520 | { 521 | printf("--\n"); 522 | printf(" \\ "); 523 | printf("_______\n"); 524 | printf(" \\++++++|\n"); 525 | printf(" \\=====|\n"); 526 | printf(" 0--- 0\n"); 527 | printf("HI SCORE: %d Player Name: %c \n", playerScore, playerName); 528 | } 529 | if(choice == 'q') 530 | { 531 | printf("dont go, I will miss you :("); 532 | l = 0; 533 | getchar(); 534 | } 535 | } 536 | return 0; 537 | } 538 | -------------------------------------------------------------------------------- /BinarySearch.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* If x in into array return 0, else 1 */ 4 | /* 5 | * A function with 4 params: 6 | * int[] = array of elements 7 | * int = wanted number 8 | * int = start index 9 | * int = end index 10 | * 11 | * returns 0 or 1 12 | */ 13 | int binarySearch(int[], int, int, int); 14 | 15 | int main(void) 16 | { 17 | int arr[] = {5, 15, 24, 32, 56, 89}; 18 | /* check length of array */ 19 | int size_of_array = sizeof(arr) / sizeof(int); 20 | /* Check if 24 is into arr */ 21 | printf("%d\n", binarySearch(arr, 24, 0, size_of_array - 1)); 22 | /* Check if 118 is into arr */ 23 | printf("%d\n", binarySearch(arr, 118, 0, size_of_array - 1)); 24 | return 0; 25 | } 26 | 27 | int binarySearch(int array[], int number, int start, int end) 28 | { 29 | /* if start index is get end index, check if that element is equals wanter nmber */ 30 | if(start >= end) 31 | { 32 | return array[start] == number ? 0 : 1; 33 | } 34 | 35 | int tmp = (int)end / 2; 36 | /* divide array length in half */ 37 | /* if number is greater than element in half, do search by start to tmp 38 | * else search by tmp to end 39 | */ 40 | if(number == array[tmp]) 41 | { 42 | return 0; 43 | } 44 | else if(number > array[tmp]) 45 | { 46 | return binarySearch(array, number, start, tmp); 47 | } 48 | else 49 | { 50 | return binarySearch(array, number, tmp, end); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /BitwiseAndOperator.c: -------------------------------------------------------------------------------- 1 | // Bitwise and operator 2 | 3 | #include 4 | int main(void) 5 | { 6 | unsigned a = 14, b = 7, c; 7 | c = a & b; 8 | printf("Value of c = %u\n", c); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /BitwiseComplementOperator.c: -------------------------------------------------------------------------------- 1 | // Bitwise complement operator 2 | 3 | #include 4 | int main(void) 5 | { 6 | unsigned a = 14, b; 7 | b = ~a; 8 | printf("Value of c = %u\n", b); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /BitwiseLeftshiftOperator.c: -------------------------------------------------------------------------------- 1 | // Bitwise left shift operator 2 | 3 | #include 4 | int main(void) 5 | { 6 | unsigned a = 7, b = 2, c; 7 | c = a << b; 8 | printf("Value of c = %u\n", c); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /BitwiseOddOrEven.c: -------------------------------------------------------------------------------- 1 | // Finding given number is Odd or Even using Bitwise AND(&) Operator 2 | // Checking the Least Significent Bit, if 0 print Even, otherwise print Odd 3 | 4 | #include 5 | 6 | int main(void) 7 | { 8 | unsigned num, mask = 0x1; 9 | 10 | printf("Enter a number: "); 11 | scanf("%u", &num); 12 | 13 | num &= mask; 14 | 15 | if(0 == num) 16 | printf("Even Number\n"); 17 | else 18 | printf("Odd Number\n"); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /BubbleSort.c: -------------------------------------------------------------------------------- 1 | // Bubble sort code 2 | 3 | #include 4 | 5 | int main(void) 6 | { 7 | int array[100], n, c, d, swap; 8 | 9 | printf("Enter number of elements\n"); 10 | scanf("%d", &n); 11 | 12 | printf("Enter %d integers\n", n); 13 | 14 | for(c = 0; c < n; c++) 15 | scanf("%d", &array[c]); 16 | 17 | for(c = 0; c < n - 1; c++) 18 | { 19 | for(d = 0; d < n - c - 1; d++) 20 | { 21 | if(array[d] > array[d + 1]) 22 | { /* For decreasing order use < */ 23 | swap = array[d]; 24 | array[d] = array[d + 1]; 25 | array[d + 1] = swap; 26 | } 27 | } 28 | } 29 | 30 | printf("Sorted list in ascending order:\n"); 31 | 32 | for(c = 0; c < n; c++) 33 | printf("%d\n", array[c]); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Buffer Overflow for Kids.md: -------------------------------------------------------------------------------- 1 | # Buffer Overflow in C 2 | 3 | # What is a buffer? 4 | A buffer, in terms of a program in execution, can be thought of as a region of computer’s main memory that has certain boundaries in context with the program variable that references this memory. 5 | ### Example Code : 6 | **char buff[10];** 7 | 8 | The above line will declare an array of 10 characters in C. Here buff[0] is the left boundary and buff[9] is the right boundary of the buffer. 9 | 10 | # What is Buffer Overflow? 11 | ![Example of Buffer Overflow](https://developer.apple.com/library/archive/documentation/Security/Conceptual/SecureCodingGuide/Art/stringcopy.gif) 12 | 13 | Now that we have seen the idea of a buffer, we can talk about what is meant by buffer overflow. 14 | A buffer is said to be overflown when the data (meant to be written into memory buffer) gets written past the left or the right boundary of the buffer. This way the data gets written to a portion of memory which does not belong to the program variable that references the buffer. 15 | 16 | **Note** : Though in the above case we have taken the example of a character array as a buffer, we can have buffer overflow with different types of data structures as well such as stack, linked lists, etc if we violate their boundary conditions. 17 | 18 | ### Example Code : 19 | **char buff[10];** 20 | **buff[10] = 'a';** 21 | 22 | The effect of the above code will be that most likely during runtime, your code will throw a Segmentation Fault or Stack Smashing Error. 23 | This is due to buffer overflow. 24 | 25 | **Similarly,** 26 | 27 | **char buff[10];** 28 | **buff[-1] = 'a';** 29 | 30 | This code will also throw the same kind of array index violation errors. 31 | 32 | In general accessing a memory location that has not been assigned for your program can lead to buffer overflow. 33 | Hence, de-referencing dangling pointers, filling array or string with more elements than it is defined to, etc can all lead to a buffer overflow and hence segfault. 34 | 35 | **More Examples :** 36 | 1. ***char a[10] = "This string is longer than the buffer can hold";*** 37 | 38 | 2. ***int \*a = (int \*)malloc(10 \* sizeof(int)) ;*** 39 | ***a[20] = 100 ;*** 40 | 41 | ![Example of Buffer Overflow](http://www.cs.fsu.edu/~baker/opsys/notes/graphics/bufferoverflow2.gif) 42 | ## Why does it matter to us? 43 | 44 | Buffer overflows are one of the most common and also one of the most notorious errors in programming as they can lead to overwriting of memory and also unexpected program crashes. 45 | 46 | The 'Segmentation Fault' error is the most common type of error associated with buffer overflow. 47 | 48 | There can also be cases where buffer overflow does not give errors immediately. But what happens in these cases is that the program will secretly overwrite neighboring memory allocated to other programs/same program and eventually will lead to a crash. 49 | 50 | Example of a segfault crash :
51 | 52 | ![enter image description here](https://qph.fs.quoracdn.net/main-qimg-669ba1b3d021390ed98c70ed29e71d62.webp) 53 | 54 | **Special Note for C Programmers :** 55 | C Programmers need to be extra careful when writing code to take care of buffer overflow because many C functions do have the feature of boundary checking and hence the slightest mistake can lead to a segfault easily. 56 | Also since it allows pointer and low level memory manipulation, it is the programmers duty to check for buffer overflow. 57 | 58 | **Thank You !** 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /CalcUsingSwitchCase.c: -------------------------------------------------------------------------------- 1 | // Very tiny and stupid Calculator using switch case 2 | // Answer will be 0 in case 4 if quotients value is in points. 3 | // For instance, 5/3 will be shown as 0. This is because we have declared 4 | // c as integer and not float. 5 | 6 | #include 7 | int main(void) 8 | { 9 | int a, b, c; 10 | char ch; 11 | printf("enter two numbers\n"); 12 | scanf("%d%d", &a, &b); 13 | while((c = getchar()) != '\n' && c != EOF) 14 | ; 15 | 16 | // fflush(stdin); 17 | // flush the input buffer. You might use this function if you think 18 | // that there might be 19 | // some data in input buffer which can create problems for you while 20 | // taking user inputs from stdin. 21 | printf("enter your choice\n"); 22 | printf("1. enter + for addition\n"); 23 | printf("2. enter - for subtraction\n"); 24 | printf("3. enter * for multiplication\n"); 25 | printf("4. Enter / for division\n"); 26 | printf("5. enter %% for modulo division\n"); 27 | scanf("%c", &ch); 28 | switch(ch) 29 | { 30 | case '+': 31 | c = a + b; 32 | break; 33 | case '-': 34 | c = a - b; 35 | break; 36 | case '*': 37 | c = a * b; 38 | break; 39 | case '/': 40 | c = a / b; 41 | break; 42 | case '%': 43 | c = a % b; // Modulus division only works with 44 | // integers. 45 | break; 46 | default: 47 | printf("wrong choice\n"); 48 | } 49 | printf("calculated value=%d\n", c); 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /CelciusToKelvinConv.c: -------------------------------------------------------------------------------- 1 | // Celsius to Kelvin converter 2 | 3 | #include 4 | 5 | int main(void) 6 | { 7 | double c, k; 8 | printf("Enter the desired temperature in Celsius:\n"); 9 | scanf("%lf", &c); 10 | k = c + 273.15; 11 | printf("Converted value in Kelvin: %f\n", k); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Changingbase.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include //used because of predefined power function 3 | #include 4 | 5 | int main(void) 6 | { 7 | long long int n, x, y, b, j = -1, c, d, e, a = 0, k = -1, f = 0; 8 | 9 | printf("This program converts the no. from one base to another\n"); 10 | 11 | printf("enter the no. you want to convert\n"); 12 | scanf("%lld", &n); 13 | 14 | printf("enter the base of no.\n"); 15 | scanf("%lld", &x); 16 | assert(x >= 0); 17 | assert(x <= 10); 18 | 19 | printf("enter the base to which you want to convert\n"); 20 | scanf("%lld", &y); 21 | assert(y >= 0); 22 | assert(y <= 10); 23 | 24 | while(n > 0) 25 | { 26 | b = n % 10; 27 | n = n / 10; 28 | j++; 29 | d = b * pow(x, j); 30 | a = d + a; // converted the no. to base 10 31 | } 32 | 33 | while(a > 0) 34 | { 35 | c = a % y; 36 | a = a / y; 37 | k++; 38 | e = c * pow(10, k); 39 | f = e + f; // converted the no. to base user wanted 40 | } 41 | 42 | printf("Converted number is %lld\n", f); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /CheckCharacterType.c: -------------------------------------------------------------------------------- 1 | // Program to check type of input character 2 | 3 | #include 4 | #include 5 | int main(void) 6 | { 7 | char ch; 8 | printf("Enter a character\n"); 9 | scanf("%c", &ch); 10 | if(isalpha(ch)) 11 | printf("Entered character is an alphabet"); 12 | 13 | else if(isdigit(ch)) 14 | printf("Entered character is a digit"); 15 | 16 | else 17 | printf("Entered character is a special symbol"); 18 | printf("\n"); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /CommonElementsInTwoArrays.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | bool already_found(int *fp, int maxi, int value) 5 | { 6 | int i; 7 | for(i = 0; i < maxi; i++) 8 | if(fp[i] == value) 9 | return true; 10 | return false; 11 | } 12 | 13 | int main(void) 14 | { 15 | int i, j, k, n; 16 | printf("enter the number of elements in an arrays\n"); 17 | scanf("%d", &n); 18 | int a[n], b[n]; 19 | k = 0; 20 | int found[n]; 21 | printf("enter the elements of 1st array\n"); 22 | for(i = 0; i < n; i++) 23 | { 24 | scanf("%d", &a[i]); 25 | } 26 | printf("enter the elements of 2nd array\n"); 27 | for(j = 0; j < n; j++) 28 | { 29 | scanf("%d", &b[j]); 30 | } 31 | printf("The common numbers are: \n"); 32 | for(i = 0; i < n; i++) 33 | { 34 | for(j = 0; j < n; j++) 35 | { 36 | if(a[i] == b[j] && !already_found(found, k, a[i])) 37 | { 38 | printf("%d\n", b[j]); 39 | found[k] = b[j]; 40 | k++; 41 | } 42 | } 43 | } 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /DailyWageCalc.c: -------------------------------------------------------------------------------- 1 | // Program to find the daily wages of a worker according to the following 2 | // conditions using ladder else if statement 3 | // Given below is the Pay according to hour 4 | // --------------------------------------------------------------------- 5 | // Duty in hours Amount in rupees 6 | // ---------------------------------------- 7 | // Within first 8 hours 100 Rupees 8 | // Next 4 hours 20 Rupees/hour 9 | // Next 4 hours 40 Rupees/hour 10 | // Next 4 hours 60 Rupees/hour 11 | // Next 4 hours 80 Rupees/hour 12 | //----------------------------------------------------------------------- 13 | 14 | #include 15 | int main(void) 16 | { 17 | int hour; 18 | int amount = 0; 19 | printf("Enter number of duty hours\n"); 20 | scanf("%d", &hour); 21 | if(hour >= 1 && hour <= 8) 22 | amount = 100; 23 | 24 | else if(hour >= 9 && hour <= 12) 25 | amount = 100 + (hour - 8) * 20; 26 | 27 | else if(hour >= 13 && hour <= 16) 28 | amount = 180 + (hour - 12) * 40; 29 | 30 | else if(hour >= 17 && hour <= 20) 31 | amount = 340 + (hour - 16) * 60; 32 | 33 | else if(hour >= 21 && hour <= 24) 34 | amount = 580 + (hour - 20) * 80; 35 | printf("Amount incurred by worker : %d\n", amount); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /DayNameUsingSwitchCase.c: -------------------------------------------------------------------------------- 1 | // Program to display the day name using switch case 2 | #include 3 | int main(void) 4 | { 5 | int day; 6 | printf("Enter the day number\n"); 7 | scanf("%d", &day); 8 | switch(day) 9 | { 10 | case 1: 11 | printf("Day is Monday"); 12 | break; 13 | case 2: 14 | printf("Day is Tuesday"); 15 | break; 16 | case 3: 17 | printf("Day is Wednesday"); 18 | break; 19 | case 4: 20 | printf("Day is Thrusday"); 21 | break; 22 | case 5: 23 | printf("Day is Friday"); 24 | break; 25 | case 6: 26 | printf("Day is Saturday"); 27 | break; 28 | case 7: 29 | printf("Day is Sunday"); 30 | break; 31 | default: 32 | printf("Wrong choice"); 33 | } 34 | printf("\n"); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /DecimalToBaseN.c: -------------------------------------------------------------------------------- 1 | // Convert base10(decimal) values any base 2 | 3 | #include 4 | #include 5 | 6 | const char letters[] = {'A', 'B', 'C', 'D', 'E', 'F'}; 7 | 8 | void printDigit(int, int); 9 | 10 | int main(void) 11 | { 12 | 13 | int input, base; 14 | 15 | // Getting user input 16 | printf("Enter number: "); 17 | scanf("%d", &input); 18 | printf("Enter base: "); 19 | scanf("%d", &base); 20 | assert(base >= 2); 21 | assert(base <= 16); 22 | 23 | printf("%d to base %d : ", input, base); 24 | 25 | printDigit(input, base); 26 | 27 | printf("\n"); 28 | return 0; 29 | } 30 | 31 | // This is a reccursive function that prints digit by digit 32 | void printDigit(int num, int base) 33 | { 34 | 35 | if(num > 0) 36 | { 37 | // getting current digit to be printed 38 | int digit = num % base; 39 | num = num - digit; 40 | num = num / base; 41 | // Call to function to print next digit 42 | printDigit(num, base); 43 | // If the current digit is more than 9 (eg: 10=A, 11=B) 44 | if(digit > 9) 45 | { 46 | printf("%c", letters[digit]); 47 | } 48 | // Else print the digit directly 49 | else 50 | { 51 | printf("%d", digit); 52 | } 53 | } 54 | else 55 | { 56 | return; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /DecimalToBinary.c: -------------------------------------------------------------------------------- 1 | // Convert base10(decimal) values to base2(binary) 2 | 3 | #include 4 | 5 | void PrintBits(int power, int num) 6 | { 7 | // firstOne is a flag that stops "0" being printed before a "1" is printed 8 | int firstOne = 0; 9 | 10 | // Conversion loop 11 | while(power > 0) 12 | { 13 | if(num >= power) 14 | { 15 | firstOne = 1; 16 | printf("1"); 17 | num %= power; 18 | } 19 | else if(firstOne == 1) 20 | { 21 | printf("0"); 22 | } 23 | power /= 2; 24 | } 25 | printf("\n"); 26 | } 27 | 28 | int main(void) 29 | { 30 | 31 | int num = 0; 32 | int power = 1; 33 | 34 | // Getting input 35 | printf("Please enter a numeric value: "); 36 | scanf("%d", &num); 37 | printf("%d represented in binary is: ", num); 38 | 39 | // Determine highest order of magnitude needed for the base2 conversion 40 | while(power < num) 41 | { 42 | power *= 2; 43 | } 44 | 45 | // Edge case in event of 0 being entered 46 | if(num == 0) 47 | { 48 | printf("0\n"); 49 | } 50 | else 51 | { 52 | PrintBits(power, num); 53 | } 54 | return (0); 55 | } 56 | -------------------------------------------------------------------------------- /DecimalToHexadecimalViceVersa.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void decimal_hex(int n, char hex[]); 7 | int hex_decimal(char hex[]); 8 | char *str_rev(char *str); 9 | 10 | int main(void) 11 | { 12 | /*char str[100]="6458897"; 13 | char *ts=str; 14 | printf("%s\n",ts); 15 | ts=str_rev(ts); 16 | printf("%s\n",ts); */ 17 | 18 | char hex[20]; 19 | int n, c; 20 | printf("***----Program to Convert Decimal and Hexadeciaml Vice Versa----***\n\n"); 21 | printf("Choose Your Choice: \n"); 22 | printf("1.Decimal to Hexadecimal:\n"); 23 | printf("2.Hexadecimal to Decimal:\n\n"); 24 | scanf("%d", &c); 25 | if(c == 1) 26 | { 27 | printf("Enter decimal number: "); 28 | scanf("%d", &n); 29 | decimal_hex(n, hex); 30 | printf("Hexadecimal number: %s", hex); 31 | } 32 | if(c == 2) 33 | { 34 | printf("Enter hexadecimal number: "); 35 | scanf("%s", hex); 36 | printf("Decimal number: %d", hex_decimal(hex)); 37 | } 38 | printf("\n"); 39 | return 0; 40 | } 41 | 42 | void decimal_hex(int n, char hex[]) 43 | { 44 | int i = 0, rem; 45 | while(n != 0) 46 | { 47 | rem = n % 16; 48 | switch(rem) 49 | { 50 | case 10: 51 | hex[i] = 'A'; 52 | break; 53 | case 11: 54 | hex[i] = 'B'; 55 | break; 56 | case 12: 57 | hex[i] = 'C'; 58 | break; 59 | case 13: 60 | hex[i] = 'D'; 61 | break; 62 | case 14: 63 | hex[i] = 'E'; 64 | break; 65 | case 15: 66 | hex[i] = 'F'; 67 | break; 68 | default: 69 | hex[i] = rem + '0'; 70 | break; 71 | } 72 | ++i; 73 | n /= 16; 74 | } 75 | hex[i] = '\0'; 76 | str_rev(hex); 77 | } 78 | 79 | int hex_decimal(char hex[]) 80 | { 81 | int i, length, sum = 0; 82 | for(length = 0; hex[length] != '\0'; ++length) 83 | ; 84 | for(i = 0; hex[i] != '\0'; ++i, --length) 85 | { 86 | if(hex[i] >= '0' && hex[i] <= '9') 87 | sum += (hex[i] - '0') * pow(16, length - 1); 88 | if(hex[i] >= 'A' && hex[i] <= 'F') 89 | sum += (hex[i] - 55) * pow(16, length - 1); 90 | if(hex[i] >= 'a' && hex[i] <= 'f') 91 | sum += (hex[i] - 87) * pow(16, length - 1); 92 | } 93 | return sum; 94 | } 95 | 96 | char *str_rev(char *str) 97 | { 98 | int i; 99 | const size_t len = strlen(str); 100 | char *copy_str = calloc(len + 1, sizeof(char)); 101 | 102 | printf("len is %zu\n", len); 103 | 104 | for(i = 0; str[i] != '\0'; i++) 105 | { 106 | copy_str[i] = str[len - i - 1]; 107 | printf("%c\n", copy_str[i]); 108 | } 109 | 110 | printf("i is %d\n", i); 111 | str[len] = '\0'; 112 | copy_str[len] = '\0'; // i len 0 113 | printf("cs is %s\n", copy_str); 114 | strcpy(str, copy_str); 115 | printf("str is %s\n", str); 116 | free(copy_str); 117 | return str; 118 | } 119 | -------------------------------------------------------------------------------- /DecrementOperator.c: -------------------------------------------------------------------------------- 1 | // Decrement operator 2 | 3 | #include 4 | int main(void) 5 | { 6 | int a = 5, b, c; 7 | b = a--; 8 | c = a--; 9 | printf("Value of b = %d\n Value of c = %d\n", b, c); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /Diagonal-Difference.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | int n, i, j, sum1 = 0, sum2 = 0; // n denotes the number of rows and columns in the matrix arr. 7 | 8 | printf("Enter n:"); 9 | scanf("%d", &n); 10 | int arr[n][n]; 11 | 12 | printf("Enter elements of matrix:\n"); 13 | for(i = 0; i < n; i++) 14 | { 15 | for(j = 0; j < n; j++) 16 | { 17 | scanf("%d", &arr[i][j]); 18 | // Taking diagonal sum of the matrix arr from the both side 19 | if(arr[i][j] >= -100 && arr[i][j] <= 100) 20 | { 21 | if(i == j) 22 | sum1 += arr[i][j]; 23 | if(j == (n - 1 - i)) 24 | sum2 += arr[i][j]; 25 | } 26 | } 27 | } 28 | // This code part belongs to the absolute difference between the sums of the matrix's along two diagonals 29 | if((sum1 - sum2) < 0) 30 | { 31 | printf("Output is %d\n", (-((sum1) - (sum2)))); 32 | } 33 | else 34 | { 35 | printf("Output is %d\n", ((sum1) - (sum2))); 36 | } 37 | return 0; 38 | } 39 | 40 | /* 41 | Sample Input 42 | 43 | 3 44 | 11 2 4 45 | 4 5 6 46 | 10 8 -12 47 | Sample Output 48 | 49 | 15 50 | 51 | Explanation 52 | 53 | The primary diagonal is: 54 | 55 | 11 56 | 5 57 | -12 58 | Sum across the primary diagonal: 11 + 5 - 12 = 4 59 | 60 | The secondary diagonal is: 61 | 62 | 4 63 | 5 64 | 10 65 | Sum across the secondary diagonal: 4 + 5 + 10 = 19 66 | Difference: |4 - 19| = 15 67 | */ 68 | -------------------------------------------------------------------------------- /DiceRoll.c: -------------------------------------------------------------------------------- 1 | // Simulate a diceroll with an adjustable number of sides using the rand() function. 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | int Diceroll(int DiceSides) 8 | { 9 | srand(time(NULL)); 10 | int n = (DiceSides - 1) + 1; // n in this case is the range of the random int 11 | int DiceRoll = 1 + rand() % n; 12 | 13 | return DiceRoll; 14 | } 15 | 16 | int main(int argc, char argv) 17 | { 18 | int DiceSides; 19 | printf("Enter the number of sides your die has. \n"); 20 | scanf("%d", &DiceSides); 21 | 22 | int Dice = Diceroll(DiceSides); 23 | printf("you rolled a %d \n", Dice); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /DigitalRoot.c: -------------------------------------------------------------------------------- 1 | // Program to find the digital root of a number in C 2 | // The digital root of a number is the single digit that you get by adding 3 | // all of the digits of the original number together. If the result of that 4 | // is multiple digits, you add those digits together, repeating the process 5 | // until you get a single digit. That digit is the digital root of the original number. 6 | 7 | #include 8 | 9 | int main(void) 10 | { 11 | unsigned int number, temp, droot = 0; 12 | printf("Enter a positive number: "); 13 | scanf("%u", &number); 14 | temp = number; 15 | while(temp != 0) 16 | { 17 | int digit = temp % 10; 18 | droot += digit; 19 | temp /= 10; 20 | if(temp == 0 && droot > 9) 21 | { 22 | temp = droot; 23 | droot = 0; 24 | } 25 | } 26 | printf("The digital root of %u is %u\n", number, droot); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /DisplayLinuxEnvirmentVariables.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char **argv, char **environ) 4 | { 5 | int i = -1; 6 | 7 | while(environ[++i]) 8 | printf("%s\n", environ[i]); 9 | } 10 | -------------------------------------------------------------------------------- /Division.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | int a, b, c; 7 | printf("Enter two Numbers : "); 8 | scanf("%d %d", &a, &b); 9 | assert(b != 0); 10 | c = a / b; 11 | printf("Division is %d\n", c); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /DynamicTwoDArrayUsingArrayOfPointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | int r = 3, c = 4, i, j, count; 7 | 8 | int *arr[r]; 9 | for(i = 0; i < r; i++) 10 | arr[i] = malloc(c * sizeof(int)); 11 | 12 | // Note that arr[i][j] is same as *(*(arr+i)+j) 13 | count = 0; 14 | for(i = 0; i < r; i++) 15 | for(j = 0; j < c; j++) 16 | arr[i][j] = ++count; // Or *(*(arr+i)+j) = ++count 17 | 18 | for(i = 0; i < r; i++) 19 | for(j = 0; j < c; j++) 20 | printf("%d ", arr[i][j]); 21 | 22 | printf("\n"); 23 | /* Code for further processing and free the 24 | dynamically allocated memory */ 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /DynamicTwoDArrayUsingOnePointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | int r = 3, c = 4; 7 | int *arr = malloc(r * c * sizeof(int)); 8 | 9 | int i, j, count = 0; 10 | for(i = 0; i < r; i++) 11 | for(j = 0; j < c; j++) 12 | *(arr + i * c + j) = ++count; 13 | 14 | for(i = 0; i < r; i++) 15 | for(j = 0; j < c; j++) 16 | printf("%d ", *(arr + i * c + j)); 17 | 18 | printf("\n"); 19 | /* Code for further processing and free the 20 | dynamically allocated memory */ 21 | 22 | // free(arr); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /EmployeeGrade.c: -------------------------------------------------------------------------------- 1 | // Program to find employee grade 2 | // Given TA : 5% , DA : 7.5% , HRA : 10% 3 | 4 | #define TA 0.05 5 | #define DA 0.075 6 | #define HRA 0.1 7 | 8 | #include 9 | int main(void) 10 | { 11 | float BaseSalary, ta, da, hra, GrossSalary; 12 | printf("Enter basic salary of employee\n"); 13 | scanf("%f", &BaseSalary); 14 | ta = TA * BaseSalary; 15 | da = DA * BaseSalary; 16 | hra = HRA * BaseSalary; 17 | GrossSalary = BaseSalary + ta + da + hra; 18 | if(GrossSalary >= 100000) 19 | printf("A grade employee"); 20 | 21 | else if(GrossSalary >= 75000 && GrossSalary < 100000) 22 | printf("B grade employee"); 23 | 24 | else if(GrossSalary >= 50000 && GrossSalary < 75000) 25 | printf("C grade employee"); 26 | 27 | else if(GrossSalary >= 20000 && GrossSalary < 50000) 28 | printf("D grade employee"); 29 | 30 | else 31 | printf("E grade employee"); 32 | printf("\n"); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /EncryptDecryptXOR.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | char XORkey[12] = {'F', 'P', 'k', 'k', 'Y', 'P', 'l', 'p', 'V', 'P', 'L', 'z'}; 4 | void encryptDecrypt(); 5 | int main() 6 | { 7 | char sampleString[] = " This contains highly sensitive message\n" 8 | " coordinates : 23.445, 34.443\n" 9 | " All further messages MUST be send via\n" 10 | " XOR encryption only - Long Live Revolution!!\n"; 11 | printf("\nEncrypted String :\n"); 12 | encryptDecrypt(sampleString); 13 | printf("\nDecyrpted String :\n"); 14 | encryptDecrypt(sampleString); 15 | return 0; 16 | } 17 | void encryptDecrypt(char inputString[]) 18 | { 19 | int i = 0; 20 | int len = strlen(inputString); 21 | for(i = 0; i < len; i++) 22 | { 23 | inputString[i] = inputString[i] ^ XORkey[i % (sizeof(XORkey) / sizeof(char))]; 24 | printf("%c", inputString[i]); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Even Fibonacci numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void) 3 | { 4 | int i; // loop counter 5 | int n; // max value 6 | int t1 = 1; // First term 7 | int t2 = 1; // Second term 8 | int nextTerm; // Next term 9 | int sum = 0; // Summation of total even Fibonacci number 10 | int count = 0; // counter to count the number of even Fibonacci numbers 11 | printf("Enter the max value up to which you want to print the values: "); 12 | scanf("%d", &n); 13 | printf("The listed even Fibonacci number are: "); 14 | printf("\nSlno.\t\tFibonacci number"); 15 | for(i = 1; i <= n; i++) 16 | { 17 | if(t1 >= 1 && t1 <= n) 18 | { 19 | nextTerm = t1 + t2; 20 | t1 = t2; 21 | t2 = nextTerm; 22 | 23 | if(t1 % 2 == 0 && t1 <= n) 24 | { 25 | sum = sum + t1; 26 | count++; 27 | printf("\n %d\t\t\t%d", count, t1); 28 | } 29 | } 30 | } 31 | printf("\n\nThe required sum of the total even Fibonacci number is: %u\n\n\n", sum); 32 | } 33 | -------------------------------------------------------------------------------- /Factorial.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | int n, i; 7 | uintmax_t factorial = 1; // max width unsigned since C99 8 | 9 | // printf("size is %zu\n",sizeof(uintmax_t)); 10 | printf("Enter a number: "); 11 | scanf("%d", &n); 12 | 13 | // Show error if number is less than 0 14 | if(n < 0) 15 | printf("Error! Factorial of a negative number doesn't exist."); 16 | 17 | else 18 | { 19 | for(i = 1; i <= n; ++i) 20 | { 21 | factorial *= i; // factorial = factorial*i; 22 | } 23 | printf("Factorial of %d = %ju\n", n, factorial); 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /FahrenheitToCelciusConv.c: -------------------------------------------------------------------------------- 1 | // Fahrenheit to celcius temp converter 2 | 3 | #include 4 | int main(void) 5 | { 6 | double c, f; 7 | printf("Enter temp in fahrenheit :\n"); 8 | scanf("%lf", &f); 9 | c = (f - 32.0) * 5.0 / 9.0; 10 | printf("Temp in celcius is : %f\n", c); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /FibonacciGeneration.c: -------------------------------------------------------------------------------- 1 | // Fibonacci Series using Recursion 2 | #include 3 | 4 | // fib function with argument n to generate nth fibonacci Number 5 | unsigned fib(unsigned n) 6 | { 7 | // Base case defined 8 | if(n <= 1) 9 | { 10 | return n; 11 | } 12 | 13 | // Recursive Calls to fib function 14 | return fib(n - 1) + fib(n - 2); 15 | } 16 | int main(void) 17 | { 18 | // Sample input 19 | unsigned n; 20 | printf("Enter n:"); 21 | scanf("%u", &n); 22 | // Printing the nth fibonacci Number 23 | printf("Fibonacci is:%u\n", fib(n)); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /FindAsciiValue.c: -------------------------------------------------------------------------------- 1 | // If ASCII in use , program to find ASCII value 2 | 3 | #include 4 | int main(void) 5 | { 6 | char c; 7 | printf("Enter a character: "); 8 | 9 | // Reads character input from the user 10 | scanf("%c", &c); 11 | 12 | // %d displays the integer value of a character 13 | // %c displays the actual character 14 | printf("ASCII value of %c is %d\n", c, c); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /FindRemainder.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int t, A, B; 5 | int rem = 0; 6 | scanf("%d", &t); 7 | while(t--) 8 | { 9 | scanf("%d%d", &A, &B); 10 | rem = A % B; 11 | printf("%d\n", rem); 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /GotoStatementEvenOrOdd.c: -------------------------------------------------------------------------------- 1 | // Program to input an integer number and check whether it is 2 | // even or odd and positive or negative using goto statements 3 | // usage of goto is not encouraged in C 4 | 5 | #include 6 | int main(void) 7 | { 8 | int n; 9 | printf("enter a number\n"); 10 | scanf("%d", &n); 11 | if(n % 2 == 0 && n > 0) 12 | goto a; 13 | 14 | else if(n % 2 != 0 && n < 0) 15 | goto b; 16 | 17 | else if(n % 2 == 0 && n < 0) 18 | goto c; 19 | 20 | else if(n % 2 != 0 && n > 0) 21 | goto d; 22 | 23 | else 24 | goto e; 25 | a: 26 | printf("number is even and positive\n"); 27 | goto stop; 28 | b: 29 | printf("number is odd and negative\n"); 30 | goto stop; 31 | c: 32 | printf("number is even and negative\n"); 33 | goto stop; 34 | d: 35 | printf("number is odd and positive\n"); 36 | goto stop; 37 | e: 38 | printf("special number\n"); 39 | goto stop; 40 | stop: 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Gross_Salary.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | float agp, b, da, gs; 6 | printf("Enter the Basic Salary : \n"); 7 | scanf("%f", &b); 8 | printf("Enter the AGP : \n"); 9 | scanf("%f", &agp); 10 | printf("Enter the DA in Percentage : \n"); 11 | scanf("%f", &da); 12 | gs = (b + agp) * (1.0 + (da / 100.0)); // assuming formula is correct 13 | printf("Gross Salary is %f\n", gs); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /HaystackAndNeedle_SubString.c: -------------------------------------------------------------------------------- 1 | // Function that takes a string "haystack" and a string "needle" 2 | // and checks if needle appears in haystack 3 | 4 | #include 5 | #include 6 | 7 | int SubString(char *haystack, char *needle) // or use strstr in 8 | { 9 | int bigIndex = 0; 10 | int littleIndex = 0; 11 | 12 | // Iterate through Haystack until a match is found 13 | // then iterate through both strings until the end of needle 14 | // or a difference is found 15 | while(haystack[bigIndex] != '\0') 16 | { 17 | while(haystack[bigIndex + littleIndex] == needle[littleIndex]) 18 | { 19 | if(needle[littleIndex + 1] == '\0') 20 | return (1); 21 | littleIndex++; 22 | } 23 | littleIndex = 0; 24 | bigIndex++; 25 | } 26 | return (0); 27 | } 28 | 29 | int main(void) 30 | { 31 | char needle[100]; 32 | char haystack[100]; 33 | 34 | // Getting input 35 | printf("Please enter your Haystack string (< 100 characters): "); 36 | scanf("%s", haystack); 37 | printf("Please enter your Needle string (< 100 characters): "); 38 | scanf("%s", needle); 39 | 40 | if(SubString(haystack, needle)) 41 | printf("Needle was found in Haystack!\n"); 42 | else 43 | printf("Needle not found in Haystack\n"); 44 | return (0); 45 | } 46 | -------------------------------------------------------------------------------- /HelloWorld.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | 6 | printf("Hello, World!\n"); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /IncrementOperator.c: -------------------------------------------------------------------------------- 1 | // Increment operator 2 | 3 | #include 4 | int main(void) 5 | { 6 | int a = 5, b, c; 7 | b = a++; 8 | c = ++a; 9 | printf("Value of b = %d\nValue of c = %d\n", b, c); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /Insertionsort.c: -------------------------------------------------------------------------------- 1 | // Insertion sort ascending order 2 | 3 | #include 4 | int main(void) 5 | { 6 | int n, array[1000], c, d, t; 7 | printf("Enter number of elements\n"); 8 | scanf("%d", &n); 9 | printf("Enter %d integers\n", n); 10 | for(c = 0; c < n; c++) 11 | scanf("%d", &array[c]); 12 | for(c = 1; c <= n - 1; c++) 13 | { 14 | d = c; 15 | while(d > 0 && array[d - 1] > array[d]) 16 | { 17 | t = array[d]; 18 | array[d] = array[d - 1]; 19 | array[d - 1] = t; 20 | d--; 21 | } 22 | } 23 | printf("Sorted list in ascending order:\n"); 24 | for(c = 0; c <= n - 1; c++) 25 | { 26 | printf("%d\n", array[c]); 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 gourav thakur 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 | -------------------------------------------------------------------------------- /Largest.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | int size, i, max; 7 | int *array; 8 | printf("Enter number of numbers: "); 9 | scanf("%d", &size); 10 | // creating an array of entered size 11 | array = malloc(size * sizeof(int)); 12 | printf("Enter %d numbers: \n", size); 13 | // accepting each number and adding them in the array 14 | for(i = 0; i < size; i++) 15 | { 16 | scanf("%d", &array[i]); 17 | } 18 | max = array[0]; 19 | // traversing the array and comparing each element to integer max 20 | for(i = 1; i < size; i++) 21 | { 22 | if(array[i] > max) 23 | { 24 | max = array[i]; 25 | } 26 | } 27 | // printing the integer max 28 | printf("The largest number is %d\n", max); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /LeapYearTernaryOperator.c: -------------------------------------------------------------------------------- 1 | // Program to check for leap year using ternary operator 2 | 3 | #include 4 | int main(void) 5 | { 6 | int y; 7 | printf("enter year for checking leap year or not\n"); 8 | scanf("%d", &y); 9 | 10 | // Using ternary operator 11 | (((y % 100 != 0) && (y % 4 == 0)) || (y % 400 == 0)) ? printf("%d is leap year\n", y) 12 | : printf("%d is not a leap year\n", y); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Linked List Creation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int n; 4 | struct node 5 | { 6 | int data; 7 | struct node *info; 8 | } * start; 9 | void create(int n) 10 | { 11 | int i; 12 | struct node *temp, *p, *newnode; 13 | p = malloc(sizeof(struct node)); 14 | start = p; 15 | printf("enter the data:\n"); 16 | scanf("%d", &p->data); 17 | p->info = NULL; 18 | temp = start; 19 | for(i = 2; i <= n; i++) 20 | { 21 | newnode = malloc(sizeof(struct node)); 22 | printf("enter the data:\n"); 23 | scanf("%d", &newnode->data); 24 | newnode->info = NULL; 25 | temp->info = newnode; 26 | temp = temp->info; 27 | } 28 | } 29 | void display(void) 30 | { 31 | struct node *temp; 32 | temp = start; 33 | while(temp != NULL) 34 | { 35 | printf("%d ", temp->data); 36 | temp = temp->info; 37 | } 38 | printf("\n"); 39 | } 40 | int main(void) 41 | { 42 | printf("enter the no of node(s):"); 43 | scanf("%d", &n); 44 | create(n); 45 | printf("\n"); 46 | display(); 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /LowercaseToUppercase.c: -------------------------------------------------------------------------------- 1 | // Lowercase character to Uppercase conversion 2 | 3 | #include 4 | #include 5 | int main(void) 6 | { 7 | char ch; 8 | int no; 9 | printf("Enter a lowercase character :\n"); 10 | scanf("%c", &ch); 11 | 12 | // no = ch-32; // not portable 13 | no = toupper(ch); 14 | printf("Letter in capital : %c\n", no); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /MagicNumbers.c: -------------------------------------------------------------------------------- 1 | // C program to check if a number is magic number or not 2 | // Magic Number:A number is said to be a magic number 3 | // if the product of the sum and the reverse number of the sum is the given number then the number is the magic number. 4 | // Ex:1729 is a magic number 5 | // Code: 6 | 7 | #include 8 | /* sum of digits of a number */ 9 | int sumOfDigits(int num) 10 | { 11 | int sum = 0; 12 | while(num > 0) 13 | { 14 | sum = sum + (num % 10); 15 | num = num / 10; 16 | } 17 | return sum; 18 | } 19 | 20 | /* returns reverse of a given number */ 21 | int reverse(int num) 22 | { 23 | int rev = 0; 24 | while(num > 0) 25 | { 26 | rev = (rev * 10) + (num % 10); 27 | num = num / 10; 28 | } 29 | return rev; 30 | } 31 | 32 | int main() 33 | { 34 | int num, sum, rev; 35 | 36 | printf("Enter the value for a number:"); 37 | scanf("%d", &num); 38 | 39 | /* find sum of digits by calling function */ 40 | sum = sumOfDigits(num); 41 | 42 | // if the value is single digit, then 43 | // the value and its reverse are same 44 | if(sum < 10) 45 | { 46 | if((sum * sum) == num) 47 | { 48 | printf("%d is a magic number\n", num); 49 | } 50 | else 51 | { 52 | printf("%d is not a magic number\n", num); 53 | } 54 | return 0; 55 | } 56 | 57 | /* reverse of the given number */ 58 | rev = reverse(sum); // calling reverse function 59 | 60 | /* printing the outputs */ 61 | if((sum * rev) == num) 62 | { 63 | printf("%d is a magic number\n", num); 64 | } 65 | else 66 | { 67 | printf("%d is not a magic number\n", num); 68 | } 69 | 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /Mergesort.c: -------------------------------------------------------------------------------- 1 | // Merge sort Algorithm 2 | 3 | #include 4 | void mergesort(int a[], int i, int j); 5 | void merge(int a[], int i1, int j1, int i2, int j2); 6 | int main(void) 7 | { 8 | int a[30], n, i; 9 | printf("Enter no of elements:"); 10 | scanf("%d", &n); 11 | printf("Enter array elements:"); 12 | for(i = 0; i < n; i++) 13 | scanf("%d", &a[i]); 14 | mergesort(a, 0, n - 1); 15 | printf("\nSorted array is :"); 16 | for(i = 0; i < n; i++) 17 | printf("%d ", a[i]); 18 | printf("\n"); 19 | return 0; 20 | } 21 | void mergesort(int a[], int i, int j) 22 | { 23 | int mid; 24 | if(i < j) 25 | { 26 | mid = (i + j) / 2; 27 | mergesort(a, i, mid); // left recursion 28 | mergesort(a, mid + 1, j); // right recursion 29 | merge(a, i, mid, mid + 1, j); // merging of two sorted sub-arrays 30 | } 31 | } 32 | void merge(int a[], int i1, int j1, int i2, int j2) 33 | { 34 | int temp[50]; // array used for merging 35 | int i, j, k; 36 | i = i1; // beginning of the first list 37 | j = i2; // beginning of the second list 38 | k = 0; 39 | while(i <= j1 && j <= j2) // while elements in both lists 40 | { 41 | if(a[i] < a[j]) 42 | temp[k++] = a[i++]; 43 | 44 | else 45 | temp[k++] = a[j++]; 46 | } 47 | while(i <= j1) // copy remaining elements of the first list 48 | temp[k++] = a[i++]; 49 | while(j <= j2) // copy remaining elements of the second list 50 | temp[k++] = a[j++]; 51 | 52 | // Transfer elements from temp[] back to a[] 53 | for(i = i1, j = 0; i <= j2; i++, j++) 54 | a[i] = temp[j]; 55 | } 56 | -------------------------------------------------------------------------------- /MirrorNumber.c: -------------------------------------------------------------------------------- 1 | // Program to find if a number is mirror number or not 2 | 3 | #include 4 | #include 5 | #include 6 | int reverse(int num) 7 | { 8 | int n_digits; 9 | int rev = 0; 10 | int snum = num; 11 | for(n_digits = 0; num != 0; n_digits++) 12 | num /= 10; 13 | 14 | // printf("n_digits is %d\n",n_digits); 15 | num = snum; 16 | for(; n_digits > 0; n_digits--) 17 | { 18 | int rem = num % 10; 19 | rev = rem * pow(10, n_digits) + rev; 20 | num /= 10; 21 | 22 | // printf("@ %d\n",rev); 23 | } 24 | rev /= 10; 25 | return rev; 26 | } 27 | int main(void) 28 | { 29 | int num; //, snum; 30 | // int n_digits; 31 | int rev; 32 | printf("Enter a number\n"); 33 | scanf("%d", &num); 34 | 35 | // snum=num; 36 | rev = reverse(num); 37 | printf("rev is %d\n", rev); 38 | if(rev == num) // snum 39 | printf("Number is mirror\n"); 40 | 41 | else 42 | printf("Not a mirror number\n"); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /NestedIfLeapYear.c: -------------------------------------------------------------------------------- 1 | // Leap year check using nested if statements 2 | 3 | #include 4 | #include 5 | int main(void) 6 | { 7 | int y; 8 | printf("Enter a year:"); 9 | scanf("%d", &y); 10 | if(y % 4 == 0) 11 | { 12 | if(y % 100 == 0) // In case of century 13 | { 14 | if(y % 400 == 0) 15 | { 16 | printf("\n%d is a leap year", y); 17 | return 0; 18 | } 19 | 20 | else 21 | { 22 | printf("\n%d is not a leap year", y); 23 | return 0; 24 | } 25 | } 26 | 27 | else 28 | printf("\n%d is a leap year", y); 29 | } 30 | 31 | else 32 | printf("\n%d is not a leap year", y); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /NestedifGreatestInteger.c: -------------------------------------------------------------------------------- 1 | // Greatest number using nested if 2 | 3 | #include 4 | int main(void) 5 | { 6 | int a, b, c; 7 | printf("enter three integer number\n"); 8 | scanf("%d%d%d", &a, &b, &c); 9 | if(a > b) 10 | if(a > c) 11 | printf("a is greatest"); 12 | 13 | else 14 | printf("c is greatest"); 15 | 16 | else if(b > c) 17 | printf("b is greatest"); 18 | 19 | else 20 | printf("c is greatest"); 21 | printf("\n"); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /NumberPattern.c: -------------------------------------------------------------------------------- 1 | /*Prints the pattern 2 | 1 3 | 2 3 4 | 4 5 6 5 | 7 8 9 10 6 | . 7 | */ 8 | 9 | #include 10 | 11 | int main(void) 12 | { 13 | int i, j, rows, num = 1; 14 | 15 | printf("Enter number of rows: "); 16 | scanf("%d", &rows); 17 | 18 | for(i = 1; i <= rows; i++) 19 | { 20 | for(j = 1; j <= i; j++) 21 | { 22 | printf("%3d ", num); 23 | num++; 24 | } 25 | printf("\n"); 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Palindrome.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | const long long int max = 1e6 + 5; 5 | 6 | int main(void) 7 | { 8 | // Defining a string to take input of the number 9 | char number[max]; 10 | 11 | long long int len, i, count = 0; 12 | 13 | // Taking the inputin form of a string 14 | printf("Enter a Number: "); 15 | scanf("%s", number); 16 | 17 | // Finding the number of digits by calculating the lenght of the string 18 | len = strlen(number); 19 | 20 | // Checking if the number is a palindrome or not 21 | for(i = 0; i < len / 2; i++) 22 | { 23 | if(number[i] == number[len - i - 1]) 24 | count++; 25 | } 26 | if(count == len / 2) 27 | printf("The entered number %s is a palindrome\n", number); 28 | else 29 | printf("The entered number %s is not a palindrome\n", number); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /PalindromeNumber.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | // A no. is said to be palindrome if the number when reversed equals to the same no. eg: 121* 6 | 7 | int n, reversedInteger = 0, remainder, originalInteger; 8 | printf("Enter an integer: "); 9 | scanf("%d", &n); 10 | originalInteger = n; 11 | 12 | // reversed integer is stored in variable 13 | 14 | while(n != 0) 15 | { 16 | remainder = n % 10; 17 | reversedInteger = reversedInteger * 10 + remainder; 18 | n /= 10; 19 | } 20 | 21 | // palindrome if orignalInteger and reversedInteger are equal 22 | 23 | if(originalInteger == reversedInteger) 24 | printf("%d is a palindrome.", originalInteger); 25 | else 26 | printf("%d is not a palindrome.", originalInteger); 27 | printf("\n"); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /PascalTriangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | long long int r, k, j, i, coeff; 5 | // input nuber of rows 6 | scanf("%lld", &r); 7 | 8 | for(i = 0; i < r; i++) 9 | { 10 | printf("\n"); 11 | 12 | // for maintaing the space in initial par 13 | for(j = 0; j < r - 1 - i; j++) 14 | { 15 | printf(" "); 16 | } 17 | 18 | // algorithm 19 | for(k = 0; k <= i; k++) 20 | { 21 | if(i == 0 || k == 0) 22 | coeff = 1; 23 | else 24 | coeff = coeff * (i - k + 1) / k; 25 | printf("%4lld", coeff); 26 | } 27 | printf("\n"); 28 | } 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Pattern1.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void) 3 | { 4 | 5 | /** Program to print the following pattern: 6 | 7 | * 8 | ** 9 | *** 10 | **** 11 | ***** 12 | **/ 13 | int i, j, k; // Declaration of variable 14 | for(i = 1; i <= 5; i++) 15 | { 16 | for(j = 1; j <= i; j++) 17 | { 18 | printf("*"); 19 | } 20 | printf("\n"); 21 | } 22 | 23 | /** Program to print the following pattern: 24 | 25 | 1 26 | 123 27 | 12345 28 | 1234567 29 | 123456789 30 | 1234567 31 | 12345 32 | 123 33 | 1 34 | 35 | **/ 36 | for(i = 1; i <= 5; i++) 37 | { 38 | for(j = i; j < 5; j++) 39 | { 40 | printf(" "); 41 | } 42 | for(k = 1; k < (i * 2); k++) 43 | { 44 | printf("%d", k); 45 | } 46 | printf("\n"); 47 | } 48 | 49 | for(i = 4; i >= 1; i--) 50 | { 51 | for(j = 5; j > i; j--) 52 | { 53 | printf(" "); 54 | } 55 | for(k = 1; k < (i * 2); k++) 56 | { 57 | printf("%d", k); 58 | } 59 | printf("\n"); 60 | } 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /Pattern_Combos.c: -------------------------------------------------------------------------------- 1 | // Run these Pattern Program for the required output...tally it with it's respective algorithm. 2 | 3 | #include 4 | 5 | int main(void) 6 | { 7 | int i, j, k; 8 | int t = 0, temp = 1; 9 | // Problem:1------------------------------------1 10 | printf("Problem 1\n"); 11 | for(i = 0; i < 5; i++) 12 | { 13 | for(j = 0; j < 5; j++) 14 | { 15 | printf("*"); 16 | } 17 | printf("\n"); 18 | } 19 | printf("\n\n"); 20 | // Problem:2-------------------------------------2 21 | printf("Problem 2\n"); 22 | for(i = 1; i <= 5; i++) 23 | { 24 | for(j = 5; j >= i; j--) 25 | { 26 | printf(" "); 27 | } 28 | for(k = 1; k <= i; k++) 29 | { 30 | printf("*"); 31 | } 32 | printf("\n"); 33 | } 34 | printf("\n\n"); 35 | // Problem:3--------------------------------------3 36 | printf("Problem 3\n"); 37 | for(i = 0; i < 5; i++) 38 | { 39 | for(j = 0; j <= i; j++) 40 | { 41 | printf("*"); 42 | } 43 | printf("\n"); 44 | } 45 | printf("\n\n"); 46 | // Problem:4-------------------------------------4 47 | printf("Problem 4\n"); 48 | for(i = 5; i >= 1; i--) 49 | { 50 | for(k = temp; k >= 0; k--) 51 | { 52 | printf(" "); 53 | } 54 | for(j = i; j >= 1; j--) 55 | { 56 | printf("*"); 57 | } 58 | temp = temp + 1; 59 | printf("\n"); 60 | } 61 | printf("\n\n"); 62 | // Problem:5--------------------------------------5 63 | printf("Problem 5\n"); 64 | for(i = 5; i >= 1; i--) 65 | { 66 | for(j = 1; j <= i; j++) 67 | { 68 | printf("*"); 69 | } 70 | printf("\n"); 71 | } 72 | printf("\n\n"); 73 | // Problem:6--------------------------------------6 74 | printf("Problem 6\n"); 75 | for(i = 1; i <= 5; i++) 76 | { 77 | printf(" "); 78 | for(k = t; k < 5; k++) 79 | { 80 | printf(" "); 81 | } 82 | for(j = 0; j < i; j++) 83 | { 84 | printf(" * "); 85 | t = t + 1; 86 | } 87 | printf("\n"); 88 | } 89 | return 0; 90 | } 91 | -------------------------------------------------------------------------------- /PerfectNumber.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int n, i, sum = 0; 6 | printf("enter n\n"); 7 | scanf("%d", &n); 8 | for(i = 1; i < n; i++) 9 | { 10 | if(n % i == 0) 11 | sum = sum + i; 12 | } 13 | if(n == sum) 14 | printf("Entered no. is a perfect no.\n"); 15 | else 16 | printf("Entered no. is not a perfect no.\n"); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Pointers.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Handling of pointers in a C program 3 | * A pointer in the C language is an object that stores the address of another 4 | * object. 5 | * A pointer in C is used to allocate memory dynamically i.e. at run time. 6 | */ 7 | 8 | #include 9 | 10 | int main(void) 11 | { 12 | int *pc; /* the '*' is used to make pointer */ 13 | int c; 14 | 15 | c = 22; 16 | printf("Address of c: %p\n", (void *)&c); 17 | printf("Value of c: %d\n\n", c); 18 | 19 | pc = &c; 20 | printf("Address of c: %p\n", (void *)pc); 21 | printf("Content of c: %d\n\n", *pc); 22 | 23 | c = 11; 24 | printf("Address of c: %p\n", (void *)pc); 25 | printf("Content of c: %d\n\n", *pc); 26 | 27 | *pc = 2; 28 | printf("Address of c: %p\n", (void *)&c); 29 | printf("Value of c: %d\n\n", c); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Polynomial_linklist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | struct Node 5 | { // node structure for polynomial 6 | int coeff; 7 | int exp; // exponent 8 | struct Node *next; // pointing to next node 9 | } *poly = NULL; // type pointer polynomial 10 | void create(void) 11 | { // creating polyno mial 12 | struct Node *t, *last = NULL; // temporary pointer, last pointer 13 | int num, i; 14 | printf("Enter number of terms"); 15 | scanf("%d", &num); 16 | printf("Enter each term with coeff and exp\n"); 17 | for(i = 0; i < num; i++) 18 | { // loop 19 | t = (struct Node *)malloc(sizeof(struct Node)); // create new node 20 | scanf("%d%d", &t->coeff, &t->exp); // reading 2 data 21 | t->next = NULL; // linking each node into linklist 22 | if(poly == NULL) 23 | { // first node check 24 | poly = last = t; 25 | } 26 | else 27 | { 28 | last->next = t; 29 | last = t; 30 | } 31 | } 32 | } 33 | 34 | void Display(struct Node *p) 35 | { 36 | while(p) 37 | { 38 | printf("%dx%d +", p->coeff, p->exp); // printing node 39 | p = p->next; // shifting node 40 | } 41 | printf("\n"); 42 | } 43 | 44 | long Eval(struct Node *p, int x) 45 | { // evalution 46 | long val = 0; 47 | while(p) 48 | { // scanning through polynomial 49 | val += p->coeff * pow(x, p->exp); 50 | p = p->next; 51 | } 52 | return val; 53 | } 54 | 55 | // TODO insert 56 | // TODO delete 57 | 58 | int main(void) 59 | { 60 | create(); 61 | Display(poly); 62 | printf("%ld\n", Eval(poly, 1)); 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /Position_of_First'n'Second_max_elements_without_sorting_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void) 3 | { 4 | int size; 5 | int i; 6 | int arr[100]; 7 | printf("Enter the size of the array:"); 8 | scanf("%d", &size); 9 | for(i = 0; i < size; i++) 10 | { 11 | printf("Enter an element:"); 12 | scanf("%d", &arr[i]); 13 | } 14 | printf("\n\n"); 15 | printf("The required entered array are:\n"); 16 | printf("Slno. Array elements\n"); 17 | for(i = 0; i < size; i++) 18 | { 19 | printf("%d\t\t%d\n", i + 1, arr[i]); 20 | } 21 | int fmax = arr[0]; 22 | int smax = arr[0]; 23 | int spos = 1; 24 | int fpos = 1; 25 | for(i = 0; i < size; i++) 26 | { 27 | if(arr[i] > fmax) 28 | { 29 | smax = fmax; 30 | fmax = arr[i]; 31 | spos = fpos; 32 | fpos = i + 1; 33 | } 34 | else if(arr[i] > smax && arr[i] != fmax) 35 | { 36 | smax = arr[i]; 37 | spos = i + 1; 38 | } 39 | } 40 | 41 | printf("First max element :%d at position:%d\n", fmax, fpos); 42 | printf("Second max element:%d at position:%d\n", smax, spos); 43 | } 44 | -------------------------------------------------------------------------------- /Prime.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define LIMIT 100 /*size of integers array */ 5 | 6 | int main(void) 7 | { 8 | unsigned long long int i, j; 9 | int *primes; // pointer created for prime 10 | // int z = 1; 11 | 12 | primes = malloc(sizeof(int) * LIMIT); // allocating 100 spaces in memory with the starting address to prime pointer 13 | 14 | for(i = 2; i < LIMIT; i++) // initializing the 100 memory spaces 15 | primes[i] = 1; 16 | 17 | for(i = 2; i < LIMIT; 18 | i++) // Filling the array with prime numbers as per the method proposed by seive of eratosthenes 19 | if(primes[i]) 20 | for(j = i; i * j < LIMIT; j++) 21 | primes[i * j] = 0; 22 | 23 | printf("\nPrime numbers in range 1 to %u are: \n", LIMIT); 24 | 25 | for(i = 2; i < LIMIT; i++) // Printing the prime numbers in the range from 1 to 100 26 | if(primes[i]) 27 | printf("%llu\n", i); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /PrimeByEratosthenes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define LIMIT 100 /*size of integers array */ 5 | 6 | int main(void) 7 | { 8 | unsigned long long int i, j; 9 | int *primes; // pointer created for prime 10 | // int z = 1; 11 | 12 | primes = malloc(sizeof(int) * LIMIT); // allocating 100 spaces in memory with the starting address to prime pointer 13 | 14 | for(i = 2; i < LIMIT; i++) // initializing the 100 memory spaces 15 | primes[i] = 1; 16 | 17 | for(i = 2; i < LIMIT; 18 | i++) // Filling the array with prime numbers as per the method proposed by seive of eratosthenes 19 | if(primes[i]) 20 | for(j = i; i * j < LIMIT; j++) 21 | primes[i * j] = 0; 22 | 23 | printf("\nPrime numbers in range 1 to %u are: \n", LIMIT); 24 | 25 | for(i = 2; i < LIMIT; i++) // Printing the prime numbers in the range from 1 to 100 26 | if(primes[i]) 27 | printf("%llu\n", i); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Quicksort.c: -------------------------------------------------------------------------------- 1 | // Quick sort Algorithm 2 | 3 | #include 4 | void quick_sort(int[], int, int); 5 | int partition(int[], int, int); 6 | int main(void) 7 | { 8 | int a[50], n, i; 9 | printf("How many elements?"); 10 | scanf("%d", &n); 11 | printf("\nEnter array elements:"); 12 | for(i = 0; i < n; i++) 13 | scanf("%d", &a[i]); 14 | quick_sort(a, 0, n - 1); 15 | printf("\nArray after sorting:"); 16 | for(i = 0; i < n; i++) 17 | printf("%d ", a[i]); 18 | printf("\n"); 19 | return 0; 20 | } 21 | void quick_sort(int a[], int l, int u) 22 | { 23 | int j; 24 | if(l < u) 25 | { 26 | j = partition(a, l, u); 27 | quick_sort(a, l, j - 1); 28 | quick_sort(a, j + 1, u); 29 | } 30 | } 31 | int partition(int a[], int l, int u) 32 | { 33 | int v, i, j, temp; 34 | v = a[l]; 35 | i = l; 36 | j = u + 1; 37 | 38 | do 39 | { 40 | 41 | do 42 | i++; 43 | while(a[i] < v && i <= u); 44 | 45 | do 46 | j--; 47 | while(v < a[j]); 48 | if(i < j) 49 | { 50 | temp = a[i]; 51 | a[i] = a[j]; 52 | a[j] = temp; 53 | } 54 | } while(i < j); 55 | a[l] = a[j]; 56 | a[j] = v; 57 | return (j); 58 | } 59 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Beginners C Program Examples 3 | 4 | Compared to [upstream repository](https://github.com/gouravthakur39/beginners-C-program-examples) , many source files are revised. Please help me to improve the codes if you want. 5 | 6 | ## Reference site 7 | One of the C reference sites that I trust is [this one](https://en.cppreference.com/w/c). 8 | 9 | 10 | ## Book 11 | There are several recommended books on C. First one is called [K&R](https://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628/). 12 | 13 | ## Linux compile 14 | ### gcc 15 | ``` 16 | gcc -Wall -Wextra -pedantic -lm -std=c11 file_name.c 17 | ``` 18 | ### executing in the same directory 19 | ``` 20 | ./a.out 21 | ``` 22 | ## Windows compile 23 | Free and capable compilers are [LLVM Clang](https://clang.llvm.org/get_started.html) and [mingw](https://www.mingw-w64.org/downloads/). 24 | 25 | ## IDE 26 | There are many IDEs available. [Code::Blocks](https://www.codeblocks.org/) is free , open source , multi platform. You may search for more and choose what suits you. 27 | 28 | *** 29 | ## Donate 30 | [![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/paypalme/ompp00) 31 | 32 | -------------------------------------------------------------------------------- /Recursion.c: -------------------------------------------------------------------------------- 1 | // Sum of natural numbers using recursion 2 | 3 | #include 4 | #include 5 | int sum(int n); 6 | int main(void) 7 | { 8 | int number, result; 9 | printf("Enter a positive integer: "); 10 | scanf("%d", &number); 11 | assert(number >= 0); 12 | result = sum(number); 13 | printf("sum = %d\n", result); 14 | return 0; 15 | } 16 | int sum(int num) 17 | { 18 | if(num != 0) 19 | return num + sum(num - 1); // sum() function calls itself 20 | else 21 | return num; 22 | } 23 | -------------------------------------------------------------------------------- /RecursiveFactorial.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | unsigned factorial(unsigned num) 4 | { 5 | if(num <= 1) 6 | return 1; 7 | else 8 | return num * factorial(num - 1); 9 | } 10 | 11 | int main(void) 12 | { 13 | unsigned number; 14 | 15 | printf("Type a positive number: "); 16 | scanf("%u", &number); 17 | 18 | printf("%u! is equal to %u\n", number, factorial(number)); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /RelationalOperators.c: -------------------------------------------------------------------------------- 1 | // Relational operators in c language 2 | 3 | #include 4 | int main(void) 5 | { 6 | int a, b; 7 | printf("Enter two numbers a and b respectively\n"); 8 | scanf("%d%d", &a, &b); 9 | 10 | // Greater than operator 11 | if(a > b) 12 | printf("a is greater than b\n"); 13 | 14 | else 15 | printf("a is smaller than b\n"); 16 | 17 | // Greater than equal to 18 | if(a >= b) 19 | printf("a is greater than equal to b\n"); 20 | 21 | else 22 | printf("a is not greater than equal to b\n"); 23 | 24 | // Less than 25 | if(a < b) 26 | printf("a is less than b\n"); 27 | 28 | else 29 | printf("a is greater than b\n"); 30 | 31 | // Lesser than equal to 32 | if(a <= b) 33 | printf("a is less than equal to b\n"); 34 | 35 | else 36 | printf("a is greater than equal to b\n"); 37 | 38 | // equal to 39 | if(a == b) 40 | printf("a is equal to b\n"); 41 | 42 | else 43 | printf("a is not equal to b\n"); 44 | 45 | // not equal to 46 | if(a != b) 47 | printf("a is not equal to b\n"); 48 | 49 | else 50 | printf("a is equal to b\n"); 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /ReverseNumber.c: -------------------------------------------------------------------------------- 1 | // program to find reverse of a number 2 | 3 | #include 4 | int main(void) 5 | { 6 | int n, reverse = 0; 7 | printf("Enter a number to reverse\n"); 8 | scanf("%d", &n); 9 | while(n != 0) 10 | { 11 | reverse = reverse * 10; 12 | reverse = reverse + n % 10; 13 | n = n / 10; 14 | } 15 | printf("Reverse of entered number is %d\n", reverse); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /ReverseNumber2.c: -------------------------------------------------------------------------------- 1 | // Reverse of a number - 2nd program 2 | 3 | #include 4 | int main(void) 5 | { 6 | int num, reverse = 0, remainder; 7 | printf("Enter a number\n"); 8 | scanf("%d", &num); 9 | while(num != 0) 10 | { 11 | remainder = num % 10; 12 | reverse = reverse * 10 + remainder; 13 | num = num / 10; 14 | } 15 | printf("Reverse of the number is : %d\n", reverse); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /RussianPeasantMultiplication.c: -------------------------------------------------------------------------------- 1 | /*Russian peasant multiplication is an interesting way to multiply 2 | numbers that uses a process of halving and doubling. Like standard 3 | multiplication and division*/ 4 | 5 | #include 6 | 7 | int main(void) 8 | { 9 | 10 | long long int a, b, k, l, sum = 0, i = 0; 11 | 12 | // input variables 13 | printf("ENTER TWO NUMBERS:\n"); 14 | scanf("%lld %lld", &a, &b); 15 | k = a; 16 | l = b; 17 | 18 | // working of algorithm 19 | while(a > 0) 20 | { 21 | if(a % 2 == 1) 22 | { 23 | sum = sum + b; 24 | } 25 | a = a >> 1; 26 | b = b << 1; 27 | i++; 28 | } 29 | 30 | // output 31 | printf("PRODUCT OF %lld AND %lld IS %lld \n", k, l, sum); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Screenshot.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lazy-dude/beginners-C-program-examples/5762e7b415710a6544f0ef2a6b0885fbebcc673a/Screenshot.PNG -------------------------------------------------------------------------------- /SegmentationFaultorBusErrorDemo.c: -------------------------------------------------------------------------------- 1 | // This program will show a segmentation or bus error because we tried to change string literals 2 | // A segmentation fault (aka segfault) is a common condition that causes programs to crash; 3 | // they are often associated with a file named core . Segfaults are caused by a program trying to 4 | // read or write an illegal memory location. 5 | 6 | #include 7 | int main() 8 | { 9 | char *cards = "JQK"; 10 | char a_card = cards[2]; 11 | cards[2] = cards[1]; 12 | cards[1] = cards[0]; 13 | cards[0] = cards[2]; 14 | cards[2] = cards[1]; 15 | cards[1] = a_card; 16 | puts(cards); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /SelectionSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // function for swapping two numbers 4 | void swap(int *xp, int *yp) 5 | { 6 | int temp = *xp; 7 | *xp = *yp; 8 | *yp = temp; 9 | } 10 | 11 | void selectionSort(int arr[], int n) 12 | { 13 | int i, j, min_idx; 14 | 15 | // One by one move boundary of unsorted subarray 16 | for(i = 0; i < n - 1; i++) 17 | { 18 | // Find the minimum element in unsorted array 19 | min_idx = i; 20 | for(j = i + 1; j < n; j++) 21 | if(arr[j] < arr[min_idx]) 22 | min_idx = j; 23 | 24 | // Swap the found minimum element with the first element 25 | // so that the minimum element of unsorted subarray 26 | // comes to first position 27 | swap(&arr[min_idx], &arr[i]); 28 | } 29 | } 30 | 31 | int main(void) 32 | { 33 | 34 | int n, i; // n is size of array 35 | printf("Enter the number of elements "); 36 | scanf("%d", &n); 37 | int a[n]; // array of n integers to be sorted 38 | printf("Enter the elements "); 39 | for(i = 0; i < n; i++) 40 | scanf("%d", &a[i]); 41 | 42 | selectionSort(a, n); // calling the function to sort the array 43 | // printing sorted array 44 | for(i = 0; i < n; i++) 45 | printf("%d ", a[i]); 46 | printf("\n"); 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /Separate.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void) 3 | { 4 | float a, c; 5 | int b; 6 | printf("Enter the Number : "); 7 | scanf("%f", &a); 8 | b = a / 1; 9 | c = a - b; 10 | printf("Integer Part is : %d\n", b); 11 | printf("Decimal Part is : %f\n", c); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /SimpleArithmeticAverage.c: -------------------------------------------------------------------------------- 1 | // A simple code that calculates a student's arithmetic average over 3 grades. 2 | 3 | #include 4 | 5 | int main(void) 6 | { 7 | 8 | float grade1, grade2, grade3, average; 9 | 10 | // Grades input 11 | printf("Type de first grade: "); 12 | scanf("%f", &grade1); 13 | 14 | printf("Type the second grade : "); 15 | scanf("%f", &grade2); 16 | 17 | printf("Type the third grade: "); 18 | scanf("%f", &grade3); 19 | 20 | // Processing 21 | average = (grade1 + grade2 + grade3) / 3; 22 | 23 | // Output 24 | printf("Student Average = %.1f\n", average); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /SimpleEMICalculator.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | double principal, rate, time, emi; 7 | 8 | printf("Enter principal amount: "); 9 | scanf("%lf", &principal); 10 | 11 | printf("Enter rate of interest: "); 12 | scanf("%lf", &rate); 13 | 14 | printf("Enter time in years: "); 15 | scanf("%lf", &time); 16 | 17 | rate = rate / (12 * 100); /*one month interest */ 18 | time = time * 12; /*one month period */ 19 | 20 | emi = (principal * rate * pow(1 + rate, time)) / (pow(1 + rate, time) - 1); 21 | 22 | printf("Monthly EMI is= %f\n", emi); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /SimpleInterestCalculator.c: -------------------------------------------------------------------------------- 1 | // Program to calculate simple interest 2 | 3 | #include 4 | int main() 5 | { 6 | float PrincipleAmount, Rate, Time, SimpleInterest; 7 | printf("Enter Principal Amount, Rate of interest and Time Respectively\n"); 8 | scanf("%f%f%f", &PrincipleAmount, &Rate, &Time); 9 | SimpleInterest = (PrincipleAmount * Rate * Time) / 100; 10 | printf("Simple Interest is :%f", SimpleInterest); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /SimpleMultiplicationTable.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | 7 | int num, i = 1; 8 | 9 | printf("Enter a number to calculate the multiplication table up to 10:\n"); 10 | 11 | scanf("%d", &num); 12 | assert(num >= 0); 13 | assert(num <= 9); 14 | 15 | for(i = 1; i <= 10; i++) 16 | { 17 | printf("%d x %d = %d\n", i, num, num * i); 18 | } 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Simple_Interest.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | float p, r, t, s; 6 | printf("Enter the principal : "); 7 | scanf("%f", &p); 8 | printf("Enter the rate : "); 9 | scanf("%f", &r); 10 | printf("Enter the time : "); 11 | scanf("%f", &t); 12 | s = (p * r * t) / 100; 13 | printf("SI is %f\n", s); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /SizeofOperator.c: -------------------------------------------------------------------------------- 1 | // sizeof() operator in c is used to calculate the size of a variable 2 | // used inside the program and return the size in integer in the form 3 | // of memory bytes 4 | 5 | #include 6 | int main(void) 7 | { 8 | int a = 5; 9 | char ch = 'X'; // Capital X 10 | printf("Size of a : %ld Bytes\n", sizeof(a)); 11 | printf("Size of X : %ld Bytes\n", sizeof(ch)); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /SquareRoot.c: -------------------------------------------------------------------------------- 1 | /* Program to find the square root of a number 2 | * - the current implementation uses the Newton-Raphson method 3 | * - mathematical explanation can be found online, and requires basic calculus knowledge 4 | */ 5 | 6 | #include // for 'fabs' - returns unsigned absolute value 7 | #include 8 | 9 | const double MAX_ERROR = 1e-7; // equivalent to 10^-7 -> accurate upto 7 decimal places 10 | // can be set according to need or even taken in as input 11 | 12 | double squareRoot(int x) 13 | { 14 | double r = 1; // initial guess for the root 15 | while(fabs(r * r - x) > MAX_ERROR) 16 | { 17 | r = (r + x / r) / 2; 18 | // value of 'r' moves closer and closer to the actual root value 19 | } 20 | 21 | return r; 22 | } 23 | 24 | int main(void) 25 | { 26 | // the number for which we expect to compute the root 27 | int num; 28 | printf("Enter a number: "); 29 | scanf("%d", &num); 30 | 31 | printf("It's square root is: %lf \n", squareRoot(num)); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Stack - Linked List.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | //#define SIZE 11 4 | 5 | typedef struct node 6 | { 7 | char data; 8 | struct node *link; 9 | } ctype, *LinkStack; 10 | 11 | void push(LinkStack *L, char elem); 12 | void pop(LinkStack *L); 13 | char top(LinkStack L); 14 | void display(LinkStack L); 15 | 16 | int main(void) 17 | { 18 | LinkStack A = NULL; 19 | size_t i; 20 | char word[] = {'P', 'R', 'O', 'G', 'R', 'A', 'M', 'M', 'I', 'N', 'G'}; 21 | size_t size = (sizeof(word) / sizeof(char)); 22 | char first; 23 | 24 | for(i = 0; i < size; i++) 25 | { 26 | push(&A, word[i]); /* INSERTING THE CHARACTER IN LINK LIST */ 27 | } 28 | 29 | printf("Example word:\n"); 30 | display(A); 31 | 32 | printf("\n\nAfter popping/deleting the first/top most element:\n"); 33 | pop(&A); 34 | display(A); 35 | 36 | first = top(A); 37 | printf("\n\nTop most element in the stack: %c\n", first); 38 | 39 | return 0; 40 | } 41 | 42 | /* PUSHING/INSERTING EACH CHARACTER TO THE STACK USING LINKLIST 43 | 44 | NOTE: 45 | Since this is a stack, I used the First in Last out order and we dont need to traverse, 46 | thats why the element is interchanged/reversed. 47 | */ 48 | 49 | void push(LinkStack *L, char elem) 50 | { 51 | LinkStack temp; 52 | temp = (LinkStack)malloc(sizeof(ctype)); 53 | 54 | if(temp != NULL) 55 | { 56 | temp->data = elem; 57 | temp->link = *L; 58 | *L = temp; 59 | } 60 | } 61 | 62 | /* DELETING/REMOVING THE TOP MOST ELEMENT IN THE STACK*/ 63 | void pop(LinkStack *L) 64 | { 65 | LinkStack temp; 66 | 67 | temp = *L; 68 | *L = temp->link; 69 | free(temp); 70 | } 71 | 72 | /* RETURNING THE TOP MOST ELEMENT IN THE STACK*/ 73 | char top(LinkStack L) 74 | { 75 | char elem; 76 | elem = L->data; 77 | 78 | return elem; 79 | } 80 | 81 | /* FOR DISPLAYING THE ELEMENTS */ 82 | void display(LinkStack L) 83 | { 84 | LinkStack trav; 85 | 86 | for(trav = L; trav != NULL; trav = trav->link) 87 | { 88 | printf("%c", trav->data); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /Star_Pattern.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | 6 | int n, i, j; 7 | 8 | printf("Enter number of rows : "); 9 | scanf("%d", &n); 10 | 11 | for(i = 1; i <= n; i++) 12 | { 13 | for(j = 1; j <= i; j++) 14 | printf("* "); 15 | printf("\n"); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /StringLength.c: -------------------------------------------------------------------------------- 1 | // Implementation of the strlen function (for standard C strings) 2 | 3 | #include 4 | 5 | int stringLength(char *word) 6 | { 7 | int count = 0; 8 | 9 | // Increment our counter until NULL TERMINATOR is found 10 | while(word[count] != '\0') 11 | { 12 | count++; 13 | } 14 | return (count); 15 | } 16 | 17 | int main(void) 18 | { 19 | char word[100]; 20 | 21 | // Gets single word as input 22 | printf("Please enter a word to get its length (< 100 characters): "); 23 | scanf("%s", word); 24 | 25 | // Prints output of stringLength() 26 | printf("\"%s\" has a length of %d\n", word, stringLength(word)); 27 | 28 | return (0); 29 | } 30 | -------------------------------------------------------------------------------- /Structure.c: -------------------------------------------------------------------------------- 1 | #include 2 | typedef struct 3 | { 4 | char name[50]; 5 | int roll; 6 | float marks; 7 | } student_t; 8 | int main(void) 9 | { 10 | student_t s; 11 | printf("Enter information:\n"); 12 | printf("Enter name: "); 13 | scanf("%s", s.name); 14 | printf("Enter roll number: "); 15 | scanf("%d", &s.roll); 16 | printf("Enter marks: "); 17 | scanf("%f", &s.marks); 18 | printf("Displaying Information:\n"); 19 | printf("Name: "); 20 | puts(s.name); 21 | printf("Roll number: %d\n", s.roll); 22 | printf("Marks: %.1f\n", s.marks); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /StudentMarksPercentage.c: -------------------------------------------------------------------------------- 1 | // Evaluate total, average and percentage of a student 2 | 3 | #include 4 | int main(void) 5 | { 6 | int a, b, c, d, e; 7 | double total, average, percentage; 8 | printf("Enter marks of 5 subjects : \n"); 9 | scanf("%d%d%d%d%d", &a, &b, &c, &d, &e); 10 | total = a + b + c + d + e; 11 | average = total / 5; 12 | percentage = (total / 500) * 100; 13 | printf("Total marks = %f\n", total); 14 | printf("Average marks = %f\n", average); 15 | printf("Net percentage = %f\n", percentage); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Subtraction.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int a, b, c; 6 | printf("Enter two Numbers : "); 7 | scanf("%d %d", &a, &b); 8 | c = a - b; 9 | printf("Subtraction is %d\n", c); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /SumUsingThreads.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | pthread_t tids[3]; // creating three threads using pthread library 5 | int numbers[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // decleration of numbers array with 10 elements 6 | int num[2] = {0, 0}; // decleration of num array with 2 elements 7 | 8 | // dividing firt five numbers into one array 9 | void *first5(void *arg) 10 | { 11 | int ans = 0; 12 | for(int i = 0; i < 5; i++) 13 | { 14 | ans += numbers[i]; 15 | } 16 | printf("%d\n", ans); 17 | num[0] = ans; 18 | } 19 | 20 | diving second five numbers into another array void *second5(void *arg) 21 | { 22 | int ans = 0; 23 | for(int i = 5; i < 10; i++) 24 | { 25 | ans += numbers[i]; 26 | } 27 | printf("%d\n", ans); 28 | num[1] = ans; 29 | } 30 | 31 | // funtion to calculate sum 32 | void *sum(void *arg) 33 | { 34 | pthread_join(tids[0], NULL); 35 | pthread_join(tids[1], NULL); 36 | int ans = num[0] + num[1]; 37 | printf("%d\n", ans); 38 | } 39 | 40 | int main(void) 41 | { 42 | 43 | pthread_create(&tids[0], NULL, first5, (void *)NULL); // creating one thread to calculate sum of first five numbers 44 | pthread_create(&tids[1], NULL, second5, 45 | (void *)NULL); // creating second thread to calculate sum of last five numbers 46 | pthread_create(&tids[2], NULL, sum, (void *)NULL); // creating another thread to to take sum of the above 47 | pthread_join(tids[2], NULL); // thread 3 will wait until thread 1 and 2 finish executing 48 | printf("Main Ended\n"); 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /SwapByRefandCopy.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Swap_ref creates a temporary variable temp and dereffrences the address that was sent to it(this is done to get the 4 | // actual value of the int in the memory space). the dereffrencing allows us to change the actual values. 5 | void swap_ref(int *a, int *b) 6 | { 7 | int temp = *a; 8 | 9 | *a = *b; 10 | *b = temp; 11 | } 12 | 13 | // Swap however does not use addresses and reffrences and only alters the ints within it's own scope 14 | void swap(int a, int b) 15 | { 16 | int temp = a; 17 | a = b; 18 | b = temp; 19 | } 20 | 21 | int main(void) 22 | { 23 | // Declare 2 int variables that are going to be swapped by reference 24 | int a = 1; 25 | int b = 2; 26 | 27 | printf("%d\n", a); 28 | printf("%d\n", b); 29 | 30 | // Pass the Address of the Values to the Swap function 31 | swap_ref(&a, &b); 32 | 33 | printf("%d\n", a); 34 | printf("%d\n", b); 35 | 36 | int c = 5; 37 | int d = 10; 38 | printf("%d\n", c); 39 | printf("%d\n", d); 40 | 41 | swap(c, d); 42 | 43 | printf("%d\n", c); 44 | printf("%d\n", d); 45 | } 46 | -------------------------------------------------------------------------------- /SwapIntegers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int num1 = 5, num2 = 10; 6 | 7 | printf("The numbers are: %d and %d\n", num1, num2); 8 | 9 | num1 = num1 + num2; // num1=15 and num2=10 10 | num2 = num1 - num2; // num1=15 and num2=5 11 | num1 = num1 - num2; // num1=10 and num2=5 12 | 13 | printf("The numbers have been swapped with new positions: %d and %d\n", num1, num2); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /SwapValueUsingThirdVariable.c: -------------------------------------------------------------------------------- 1 | // Swap two integers using third variable 2 | 3 | #include 4 | int main(void) 5 | { 6 | int a, b, c; 7 | printf("Enter two no :\n"); 8 | scanf("%d%d", &a, &b); 9 | c = a; 10 | a = b; 11 | b = c; 12 | printf("After swapping value of a = %d b = %d\n", a, b); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /SwapValueWithoutUsingThirdVariable.c: -------------------------------------------------------------------------------- 1 | // Swap two integers without using third variable , using bitwise ^ 2 | 3 | #include 4 | int main(void) 5 | { 6 | int a, b; 7 | printf("Enter two no :\n"); 8 | scanf("%d%d", &a, &b); 9 | a = a ^ b; 10 | b = a ^ b; 11 | a = a ^ b; 12 | printf("After swapping value of a and b : %d,%d\n", a, b); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Swap_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int a, b, t; 6 | printf("Enter two Numbers : "); 7 | scanf("%d %d", &a, &b); 8 | t = b; 9 | b = a; 10 | a = t; 11 | printf("Swap is %d %d\n", a, b); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Swap_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int a, b; 6 | printf("Enter two Numbers : "); 7 | scanf("%d %d", &a, &b); 8 | a = a + b; 9 | b = a - b; 10 | a = a - b; 11 | printf("Swap is %d %d\n", a, b); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Temperature.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | float c, f; 6 | printf("Enter the Temperature in Celcius : "); 7 | scanf("%f", &c); 8 | f = c * (9.0 / 5.0) + 32.0; 9 | printf("Temperature in Fahernheit is %f\n", f); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /TemperatureSwitch.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | // The resultant temperatures can be in decimals as well, so we use double 6 | double c, f, result = 0; 7 | // We use an integer type data to run the switch statement 8 | int choice; 9 | printf("Select your choice: \n"); 10 | printf("1. Celcius to Fahrenheit\n"); 11 | printf("2. Fahrenheit to Celcius\n"); 12 | 13 | printf("Enter your choice: "); 14 | scanf("%d", &choice); 15 | 16 | // We compute the temperatures for both the cases here respectively 17 | switch(choice) 18 | { 19 | case 1: 20 | printf("Enter the temperature in Celcius: "); 21 | scanf("%lf", &c); 22 | result = (9.0 / 5.0) * c + 32.0; 23 | break; 24 | case 2: 25 | printf("Enter the temperature in Fahrenheit: "); 26 | scanf("%lf", &f); 27 | result = (5.0 / 9.0) * (f - 32.0); 28 | break; 29 | 30 | // This case gets activated when the user inputs anything othrer than 1 or 2 31 | default: 32 | printf("Invalid case!\n"); 33 | } 34 | 35 | // Printing out the result according to the computation 36 | printf("The resultant temperature is: %lf\n", result); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /TernaryOperator.c: -------------------------------------------------------------------------------- 1 | // Largest number among 3 numbers using ternary operator 2 | 3 | #include 4 | int main(void) 5 | { 6 | double a, b, c, large; 7 | printf("Enter any 3 numbers\n"); 8 | scanf("%lf%lf%lf", &a, &b, &c); 9 | large = a > b ? (a > c ? a : c) : (b > c ? b : c); 10 | printf("The largest no is :%f\n", large); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /Trif.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | double a, b; 7 | printf("Enter the degree : "); 8 | scanf("%lf", &a); 9 | a = (a * 3.14) / 180; 10 | b = sin(a); 11 | printf("Sine is %lf", b); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /UppercaseToLowercase.c: -------------------------------------------------------------------------------- 1 | // Uppercase character to lowercase character 2 | 3 | #include 4 | #include 5 | #include 6 | int main(void) 7 | { 8 | char a, u; 9 | printf("Enter Uppercase letter :\n"); 10 | scanf("%c", &a); 11 | assert(isupper(a)); 12 | u = tolower(a); 13 | printf("Lowercase is : %c\n", u); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /VowelorConsonant.c: -------------------------------------------------------------------------------- 1 | // Program to input a character and check whether it is vowel or consonant using switch case 2 | #include 3 | #include 4 | #include 5 | int main(void) 6 | { 7 | char ch; 8 | printf("Enter a character\n"); 9 | scanf("%c", &ch); 10 | assert(isalpha(ch)); 11 | switch(ch) 12 | { 13 | case 'a': 14 | case 'e': 15 | case 'i': 16 | case 'o': 17 | case 'u': 18 | case 'A': 19 | case 'E': 20 | case 'I': 21 | case 'O': 22 | case 'U': 23 | printf("Entered character is a vowel\n"); 24 | break; 25 | default: 26 | printf("Entered character is a consonant\n"); 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /alphabetTriangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | /* 7 | A 8 | ABA 9 | ABCBA 10 | ABCDCBA 11 | ABCDEDCBA 12 | 13 | */ 14 | #define MAX 5 15 | 16 | int main(void) 17 | { 18 | 19 | char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 20 | char *cp = &alphabet[0]; 21 | int i, j, k, m; 22 | 23 | assert(MAX <= strlen(alphabet)); 24 | 25 | for(i = 1; i <= MAX; i++) 26 | { 27 | for(j = MAX; j >= i; j--) 28 | printf(" "); 29 | for(k = 1; k <= i; k++) 30 | printf("%c", *(cp++)); 31 | cp--; 32 | for(m = 1; m < i; m++) 33 | printf("%c", *(--cp)); 34 | printf("\n"); 35 | cp = alphabet; 36 | } 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /combine_calculator.c: -------------------------------------------------------------------------------- 1 | // add a program for a calculator in C 2 | #include 3 | #include 4 | 5 | void Input(void) 6 | { 7 | printf("Type number to choose the algorithm\n"); 8 | printf(" 1. + \n 2. - \n 3. *\n 4. / \n 5. pow \n 6. square \n "); 9 | printf("7. log \n 8.floor \n 9. ceil \n 10.Exit\n InputNum: "); 10 | } 11 | 12 | int main(void) 13 | { 14 | int InputNum = 0; 15 | float a = 0; 16 | float b = 0; 17 | float result = 0; 18 | do 19 | { 20 | Input(); 21 | scanf("%d", &InputNum); 22 | switch(InputNum) 23 | { 24 | case(1): 25 | printf("Enter First Number: "); 26 | scanf("%f", &a); 27 | printf("Enter Second Number: "); 28 | scanf("%f", &b); 29 | result = a + b; 30 | printf("%.2f + %.2f = %.2f\n", a, b, result); 31 | break; 32 | case(2): 33 | printf("Enter First Number: "); 34 | scanf("%f", &a); 35 | printf("Enter Second Number: "); 36 | scanf("%f", &b); 37 | result = a - b; 38 | printf("%.2f - %.2f = %.2f\n", a, b, result); 39 | break; 40 | case(3): 41 | printf("Enter First Number: "); 42 | scanf("%f", &a); 43 | printf("Enter Second Number: "); 44 | scanf("%f", &b); 45 | result = a * b; 46 | printf("%.2f * %.2f = %.2f\n", a, b, result); 47 | break; 48 | case(4): 49 | printf("Enter Dividend: "); 50 | scanf("%f", &a); 51 | printf("Enter Divisor: "); 52 | scanf("%f", &b); 53 | if(b == 0) 54 | { 55 | printf("Please check the divisor that can not be zero.\n"); 56 | } 57 | else 58 | { 59 | result = a / b; 60 | printf("%.2f / %.2f = %.2f\n", a, b, result); 61 | } 62 | break; 63 | case(5): 64 | printf("Enter the number "); 65 | scanf("%f", &a); 66 | printf("Enter the second number "); 67 | scanf("%f", &b); 68 | printf("first number to the power of second number is %.2f \n ", pow(a, b)); 69 | break; 70 | case(6): 71 | printf("Enter the radicand "); 72 | scanf("%f", &a); 73 | printf("square root is %.2f \n", sqrt(a)); 74 | break; 75 | 76 | case(7): 77 | printf("Enter the log "); 78 | scanf("%f", &a); 79 | printf("the log is %.2f \n", log(a)); 80 | break; 81 | case(8): 82 | printf("Enter the number "); 83 | scanf("%f", &a); 84 | printf("the float is %.2f \n", floor(a)); 85 | break; 86 | case(9): 87 | printf("Enter the number "); 88 | scanf("%f", &a); 89 | printf("the ceil is %.2f \n", ceil(a)); 90 | break; 91 | 92 | case(10): 93 | printf("Thank you for using code !!\n"); 94 | return 0; 95 | break; 96 | 97 | default: 98 | printf("Invalid number, please choose again.\n"); 99 | } 100 | } while(InputNum); 101 | return 0; 102 | } 103 | -------------------------------------------------------------------------------- /counting_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | long long *count_sort_naive(long long *arr, long n, long m) 5 | { 6 | long long *count_arr = malloc((m + 1) * sizeof(long long)); 7 | long long *sorted_arr = malloc(n * sizeof(long long)); 8 | 9 | long i_arr = 0; 10 | for(long i = 0; i <= m; ++i) 11 | { 12 | /* compute the frequency of the ith element */ 13 | for(long j = 0; j < n; ++j) 14 | { 15 | if(arr[j] == i) 16 | { 17 | count_arr[i] += 1; 18 | } 19 | } 20 | 21 | /* place the ith element count number of times */ 22 | for(long j = 0; j < count_arr[i]; ++j) 23 | { 24 | sorted_arr[i_arr] = i; 25 | i_arr += 1; 26 | } 27 | } 28 | free(arr); 29 | 30 | return sorted_arr; 31 | } 32 | 33 | int main(void) 34 | { 35 | /* Enter the size of the array */ 36 | long n = 0; 37 | printf("Enter the number of elements to be sorted: "); 38 | scanf("%ld", &n); 39 | 40 | /* Enter the range of the array [0 .. m] */ 41 | long m = 0; 42 | printf("Enter the maximum value of the numbers to be sorted: "); 43 | scanf("%ld", &m); 44 | 45 | /* Enter the values of the array */ 46 | printf("Enter the values to be sorted: "); 47 | long long *arr = malloc(n * sizeof(long long)); 48 | for(long i = 0; i < n; ++i) 49 | { 50 | scanf("%lld", &arr[i]); 51 | } 52 | 53 | arr = count_sort_naive(arr, n, m); 54 | 55 | printf("After sorting\n"); 56 | for(long i = 0; i < n; ++i) 57 | { 58 | printf("%lld ", arr[i]); 59 | } 60 | printf("\n"); 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /dynamicMemoryAllocation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define SIZE 0x100 5 | int main(void) 6 | { 7 | char *ptr = malloc(SIZE); 8 | snprintf(ptr, SIZE, "data : %s ", "hello world"); 9 | printf("%s\n", ptr); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /endian.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************/ 2 | // Since size of character is 1 byte when the character pointer is de-referenced 3 | // it will contain only first byte of integer. 4 | // If machine is little endian then *c will be 1 (because last byte is stored first) 5 | // if machine is big endian then *c will be 0. 6 | 7 | // higher memory 8 | // -----> 9 | // +----+----+----+----+ 10 | // |0x01|0x00|0x00|0x00| 11 | // +----+----+----+----+ 12 | // c 13 | // | 14 | // &i 15 | 16 | // +----+----+----+----+ 17 | // |0x00|0x00|0x00|0x01| 18 | // +----+----+----+----+ 19 | // c 20 | // | 21 | // &i 22 | /*************************************************************************************/ 23 | 24 | #include 25 | int main(void) 26 | { 27 | unsigned int i = 1u; 28 | unsigned char *c = (unsigned char *)&i; 29 | if(*c) 30 | printf("Little endian\n"); 31 | else 32 | printf("Big endian\n"); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /gcd.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | long long get_gcd_recursive(long long a, long long b) 4 | { 5 | if(a == 0) 6 | { 7 | return b; 8 | } 9 | else if(b == 0) 10 | { 11 | return a; 12 | } 13 | 14 | /* Swap the two numbers */ 15 | if(b > a) 16 | { 17 | a = a - b; 18 | b = a + b; 19 | a = b - a; 20 | } 21 | long long remainder = a % b; 22 | if(remainder == 0) 23 | { 24 | return b; 25 | } 26 | return get_gcd_recursive(b, remainder); 27 | } 28 | int main(void) 29 | { 30 | long long a = 0; 31 | long long b = 0; 32 | scanf("%lld", &a); 33 | scanf("%lld", &b); 34 | printf("gcd is %lld\n", get_gcd_recursive(a, b)); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /heap sort.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | void create(int[]); 5 | void down_adjust(int[], int); 6 | 7 | int main(void) 8 | { 9 | int heap[30], n, i, last, temp; 10 | printf("Enter no. of elements:"); 11 | scanf("%d", &n); 12 | printf("\nEnter elements:"); 13 | for(i = 1; i <= n; i++) 14 | scanf("%d", &heap[i]); 15 | 16 | // create a heap 17 | heap[0] = n; 18 | create(heap); 19 | 20 | // sorting 21 | while(heap[0] > 1) 22 | { 23 | // swap heap[1] and heap[last] 24 | last = heap[0]; 25 | temp = heap[1]; 26 | heap[1] = heap[last]; 27 | heap[last] = temp; 28 | heap[0]--; 29 | down_adjust(heap, 1); 30 | } 31 | 32 | // print sorted data 33 | printf("\nArray after sorting:\n"); 34 | for(i = 1; i <= n; i++) 35 | printf("%d ", heap[i]); 36 | 37 | return 0; 38 | } 39 | 40 | void create(int heap[]) 41 | { 42 | int i, n; 43 | n = heap[0]; // no. of elements 44 | for(i = n / 2; i >= 1; i--) 45 | down_adjust(heap, i); 46 | } 47 | 48 | void down_adjust(int heap[], int i) 49 | { 50 | int j, temp, n, flag = 1; 51 | n = heap[0]; 52 | 53 | while(2 * i <= n && flag == 1) 54 | { 55 | j = 2 * i; // j points to left child 56 | if(j + 1 <= n && heap[j + 1] > heap[j]) 57 | j = j + 1; 58 | if(heap[i] > heap[j]) 59 | flag = 0; 60 | else 61 | { 62 | temp = heap[i]; 63 | heap[i] = heap[j]; 64 | heap[j] = temp; 65 | i = j; 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /increment_number.c: -------------------------------------------------------------------------------- 1 | /* A C-Program that Increments a Number */ 2 | 3 | #include 4 | #include // This Header is used so we can gain access to the sleep() Function 5 | 6 | int main(void) 7 | { 8 | int num = 0; // This is the Number that will be incremented. 9 | int increment = 1; // This is what we'll be using to increment(add) onto num. 10 | 11 | // While num's Value is less than 100, we'll increment the Value of num by 1 every second. 12 | while(num < 100) 13 | { 14 | sleep(1); // Wait 1 second each time, num has it's Value incremented 15 | num += increment; // Increment num 16 | printf("%d\n", num); // Print the current Value of num 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /isInputLeapYear.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) 4 | { 5 | int input; 6 | printf("Please input a year and I will tell you if it's a leap year: "); 7 | scanf("%d", &input); 8 | if((input % 4 == 0 && input % 100 != 0) || input % 400 == 0) 9 | { 10 | printf("Your input (%d) IS a leap year.\n\n", input); 11 | } 12 | else 13 | { 14 | printf("Your input (%d) is NOT a leap year.\n\n", input); 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /large_factorial.c: -------------------------------------------------------------------------------- 1 | // c program to find factorial upto input of 5000 2 | 3 | #define SIZE 16500 4 | 5 | #include 6 | 7 | int main(void) 8 | { 9 | int ans[SIZE]; 10 | int testcases; 11 | printf("Enter no of test cases: "); 12 | scanf("%d", &testcases); 13 | while(testcases--) 14 | { 15 | int num; 16 | printf("Enter number:"); 17 | scanf("%d", &num); 18 | for(int i = 0; i < SIZE; i++) 19 | ans[i] = 0; 20 | ans[0] = 1; 21 | int m, carry; 22 | for(int i = 2; i <= num; i++) 23 | { 24 | carry = 0; 25 | for(int j = 0; j < SIZE; j++) 26 | { 27 | m = i * ans[j]; 28 | m += carry; 29 | ans[j] = m % 10; 30 | carry = m / 10; 31 | } 32 | } 33 | int i = SIZE - 1; 34 | while(ans[i] == 0 && i >= 0) 35 | { 36 | i--; 37 | } 38 | printf("\t%d! =", num); 39 | while(i >= 0) 40 | { 41 | printf("%d", ans[i]); 42 | i--; 43 | } 44 | printf("\n"); 45 | } 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /last-digit-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int last_digit_fib_optimized(const int index) 4 | { 5 | int first = 0; 6 | int second = 1; 7 | int current = 0; 8 | for(int i = 2; i <= index; ++i) 9 | { 10 | current = (first + second) % 10; 11 | first = second; 12 | second = current; 13 | } 14 | return (current); 15 | } 16 | 17 | /* This is the direct solution for reference */ 18 | int last_digit_fib_naive(const int index) 19 | { 20 | int *arr; 21 | arr = (int *)malloc((index + 1) * sizeof(int)); 22 | arr[0] = 0; 23 | arr[1] = 1; 24 | for(int i = 2; i <= index; ++i) 25 | { // store only the last digit of each index 26 | arr[i] = (arr[i - 1] + arr[i - 2]) % 10; 27 | } 28 | int res = arr[index]; 29 | return (res); 30 | } 31 | int main(int argc, char **argv) 32 | { 33 | int index = 0; 34 | scanf("%d", &index); 35 | printf("%d\n", last_digit_fib_optimized(index)); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /lcm.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | long get_gcd_euclidian(long d1, long d2) 5 | { 6 | /* swap the numbers if d2 is greater than d1 */ 7 | if(d2 > d1) 8 | { 9 | d1 = d1 - d2; 10 | d2 = d1 + d2; 11 | d1 = d2 - d1; 12 | } 13 | 14 | if(d2 == 0) 15 | { 16 | return d1; 17 | } 18 | 19 | long rem = d1 % d2; 20 | 21 | return get_gcd_euclidian(d2, rem); 22 | } 23 | 24 | long get_lcm_euclidian(long val1, long val2) 25 | { 26 | if(val1 == 0 || val2 == 0) 27 | { 28 | return 0; 29 | } 30 | 31 | long prod = (long)(val1 * val2); 32 | long gcd = get_gcd_euclidian(val1, val2); 33 | 34 | return (prod / gcd); 35 | } 36 | 37 | int main(void) 38 | { 39 | long ip1 = 0; 40 | long ip2 = 0; 41 | printf("Enter two numbers: "); 42 | scanf("%ld %ld", &ip1, &ip2); 43 | printf("LCM is %ld\n", get_lcm_euclidian(ip1, ip2)); 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /nCrCalculatorLargeNumbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define m 1000000007 3 | #include 4 | 5 | // This function calcualtes the large powers with O(log(n)) 6 | long fastexp(long x, long y) 7 | { 8 | // In the form of pow(x, y) % m 9 | if(y == 0) 10 | return 1; 11 | else if(y == 1) 12 | return x; 13 | else 14 | { 15 | long ans = fastexp(x, y / 2); 16 | if(y % 2 == 0) 17 | return (ans % m * ans % m) % m; 18 | else 19 | return ((ans % m * ans % m) % m * x % m) % m; 20 | } 21 | } 22 | 23 | long fact[2000001] = {1}; 24 | 25 | int main() 26 | { 27 | // FACTORIAL CALCULATION 28 | // Using (a * b) % m = (a % m * b % m) % m 29 | for(int i = 1; i < 2000001; i++) 30 | fact[i] = ((fact[i - 1] % m) * (i % m)) % m; 31 | 32 | // Number of test cases 33 | int T; 34 | long n, r, ans; 35 | printf("Enter the number of test cases: "); 36 | scanf("%d", &T); 37 | while(T--) 38 | { 39 | printf("Enter the two numbers in the form of xCy: "); 40 | scanf("%ld%ld", &n, &r); 41 | 42 | // Formula used: (n)!/ (r! * (n-r)!) 43 | // = (n+r)! * pow((r! * (n-r)!), -1) 44 | // Using FERMAT's LITTLE THEOREM (Google it!) 45 | // = (fact[n]%m * pow((r! * (n-r)!), m - 2) % m) % m 46 | // = (fact[n]%m * fastexp((fact[r] * fact[n-r])%m, m - 2)%m)%m 47 | 48 | ans = (fact[n] % m * fastexp((fact[r] % m * fact[n - r] % m) % m, m - 2) % m) % m; 49 | printf("%ld\n", ans); 50 | } 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /oddandeven.c: -------------------------------------------------------------------------------- 1 | // Program to tell if an integer number is even or odd 2 | 3 | #include 4 | 5 | int main(void) 6 | { 7 | 8 | int number; 9 | printf("Enter an integer number\n"); 10 | scanf("%d", &number); 11 | if(number % 2 == 0) 12 | printf("The number %d is even\n", number); 13 | else 14 | { 15 | printf("The number %d is odd\n", number); 16 | } 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /priority_queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int CAPACITY = 10; 6 | int size; 7 | 8 | typedef struct task 9 | { 10 | int id; 11 | int priority; 12 | } task; 13 | 14 | int getLeftChildIndex(int i); 15 | int getRightChildIndex(int i); 16 | int getParentIndex(int i); 17 | bool hasLeftChild(int i); 18 | bool hasRightChild(int i); 19 | bool hasParent(int i); 20 | task leftChild(int i, task *taskList); 21 | task rightChild(int i, task *taskList); 22 | task parent(int i, task *taskList); 23 | void swap(int i, int j, task *taskList); 24 | void ensureExtraCapacity(task **taskList); 25 | task peek(task *taskList); 26 | task poll(task *taskList); 27 | void addTask(task t, task *taskList); 28 | void heapifyUp(task *taskList); 29 | void heapifyDown(task *taskList); 30 | 31 | int main() 32 | { 33 | task *taskList = malloc(sizeof(task) * CAPACITY); 34 | task task; 35 | int op; 36 | printf("\tIn this program the greater the priority value of the task, the higher is the priority of the task\n"); 37 | for(int i = 0; i < 110; ++i) 38 | printf("-"); 39 | printf("\n"); 40 | do 41 | { 42 | printf("1...Enter a new task\n"); 43 | printf("2...Remove highest priority task\n"); 44 | printf("3...Show highest priority task\n"); 45 | printf("4...Exit\n"); 46 | printf("Enter you option below:\n"); 47 | printf("-> "); 48 | scanf("%d", &op); 49 | printf("\n"); 50 | 51 | switch(op) 52 | { 53 | case 1: 54 | printf("Enter the task ID:\n"); 55 | printf("-> "); 56 | scanf("%d", &task.id); 57 | 58 | printf("Enter the task Priority:\n"); 59 | printf("-> "); 60 | scanf("%d", &task.priority); 61 | 62 | addTask(task, taskList); 63 | break; 64 | case 2: 65 | task = poll(taskList); 66 | printf("Element Processed is:\n"); 67 | printf("ID: %d\n", task.id); 68 | printf("Priority: %d\n", task.priority); 69 | break; 70 | case 3: 71 | task = peek(taskList); 72 | printf("Element Ready to be Processed first is:\n"); 73 | printf("ID: %d\n", task.id); 74 | printf("Priority: %d\n", task.priority); 75 | break; 76 | case 4: 77 | // nothing to see here... 78 | break; 79 | default: 80 | printf("Invalid option entered, please try again !\n\n"); 81 | break; 82 | } 83 | for(int i = 0; i < 100; ++i) 84 | printf("-"); 85 | printf("\n"); 86 | } while(op != 4); 87 | free(taskList); 88 | return 0; 89 | } 90 | 91 | void heapifyDown(task *taskList) 92 | { 93 | int index = 0; 94 | int greaterChildIndex = getLeftChildIndex(index); 95 | while(hasLeftChild(index)) 96 | { 97 | if(hasRightChild(index) && rightChild(index, taskList).priority > leftChild(index, taskList).priority) 98 | { 99 | greaterChildIndex = getRightChildIndex(index); 100 | } 101 | if(taskList[index].priority > taskList[greaterChildIndex].priority) 102 | { 103 | break; 104 | } 105 | else 106 | { 107 | swap(index, greaterChildIndex, taskList); 108 | } 109 | index = greaterChildIndex; 110 | } 111 | } 112 | 113 | void heapifyUp(task *taskList) 114 | { 115 | int index = size - 1; 116 | while(hasParent(index) && parent(index, taskList).priority < taskList[index].priority) 117 | { 118 | swap(getParentIndex(index), index, taskList); 119 | index = getParentIndex(index); 120 | } 121 | } 122 | 123 | void addTask(task t, task *taskList) 124 | { 125 | ensureExtraCapacity(&taskList); 126 | taskList[size] = t; 127 | ++size; 128 | heapifyUp(taskList); 129 | } 130 | 131 | task poll(task *taskList) 132 | { 133 | if(!size) 134 | { 135 | printf("Error !\n"); 136 | exit(0); 137 | } 138 | task t = taskList[0]; 139 | taskList[0] = taskList[size - 1]; 140 | --size; 141 | heapifyDown(taskList); 142 | return t; 143 | } 144 | 145 | task peek(task *taskList) 146 | { 147 | if(!size) 148 | { 149 | printf("Error !\n"); 150 | exit(0); 151 | } 152 | return taskList[0]; 153 | } 154 | 155 | void ensureExtraCapacity(task **taskList) 156 | { 157 | if(size > 2 * CAPACITY / 3) 158 | *taskList = realloc(*taskList, CAPACITY *= 2); 159 | } 160 | 161 | void swap(int i, int j, task *taskList) 162 | { 163 | task temp = taskList[i]; 164 | taskList[i] = taskList[j]; 165 | taskList[j] = temp; 166 | } 167 | 168 | task parent(int i, task *taskList) 169 | { 170 | return taskList[getParentIndex(i)]; 171 | } 172 | 173 | task rightChild(int i, task *taskList) 174 | { 175 | return taskList[getRightChildIndex(i)]; 176 | } 177 | 178 | task leftChild(int i, task *taskList) 179 | { 180 | return taskList[getLeftChildIndex(i)]; 181 | } 182 | 183 | bool hasParent(int i) 184 | { 185 | return getParentIndex(i) >= 0; 186 | } 187 | 188 | bool hasRightChild(int i) 189 | { 190 | return getRightChildIndex(i) < size; 191 | } 192 | 193 | bool hasLeftChild(int i) 194 | { 195 | return getLeftChildIndex(i) < size; 196 | } 197 | 198 | int getLeftChildIndex(int i) 199 | { 200 | return 2 * i + 1; 201 | } 202 | 203 | int getRightChildIndex(int i) 204 | { 205 | return 2 * i + 2; 206 | } 207 | 208 | int getParentIndex(int i) 209 | { 210 | return (i - 1) / 2; 211 | } 212 | -------------------------------------------------------------------------------- /swappingwithoutthirdvariable.c: -------------------------------------------------------------------------------- 1 | // Swap Values Without using Third Variable. 2 | 3 | #include 4 | 5 | int main(void) 6 | { 7 | int a, b; /*declaration of variables */ 8 | printf("Enter Two Numbers\n"); 9 | scanf("%d %d", &a, &b); /*Taking input of Two Variables */ 10 | a = a + b; /*swapping */ 11 | b = a - b; /* the two */ 12 | a = a - b; /*numbers */ 13 | printf("the number after swapping are %d %d\n", a, b); /*printing */ 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /transposeOfMatrix.c: -------------------------------------------------------------------------------- 1 | // C program to input a matrix of order MxN and find its transpose 2 | 3 | #include 4 | 5 | int main(void) 6 | { 7 | static int array[10][10]; 8 | int i, j, row, col; 9 | 10 | printf("Enter the order of the matrix \n"); 11 | scanf("%d %d", &row, &col); 12 | printf("Enter the coefficients of the matrix\n"); 13 | for(i = 0; i < row; ++i) 14 | { 15 | for(j = 0; j < col; ++j) 16 | { 17 | scanf("%d", &array[i][j]); 18 | } 19 | } 20 | printf("The given matrix is \n"); 21 | for(i = 0; i < row; ++i) 22 | { 23 | for(j = 0; j < col; ++j) 24 | { 25 | printf(" %d", array[i][j]); 26 | } 27 | printf("\n"); 28 | } 29 | printf("Transpose of matrix is \n"); 30 | for(j = 0; j < col; ++j) 31 | { 32 | for(i = 0; i < row; ++i) 33 | { 34 | printf(" %d", array[i][j]); 35 | } 36 | printf("\n"); 37 | } 38 | 39 | return 0; 40 | } 41 | --------------------------------------------------------------------------------